summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--app.psgi2
-rw-r--r--lib/Narwhal.pm43
-rw-r--r--lib/Narwhal/Component/Redirect.pm31
-rw-r--r--lib/Narwhal/Component/Wiki.pm65
-rw-r--r--lib/Narwhal/Page.pm12
-rw-r--r--root/templates/edit.tt12
-rw-r--r--root/templates/page.tt9
8 files changed, 175 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 9fe80d8..79fc2db 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,3 +11,4 @@ MANIFEST.bak
*.sw[po]
.build
Narwhal-*
+narwhal.db
diff --git a/app.psgi b/app.psgi
new file mode 100644
index 0000000..56e4f22
--- /dev/null
+++ b/app.psgi
@@ -0,0 +1,2 @@
+use Narwhal;
+Narwhal->new->to_app;
diff --git a/lib/Narwhal.pm b/lib/Narwhal.pm
index e69de29..240b504 100644
--- a/lib/Narwhal.pm
+++ b/lib/Narwhal.pm
@@ -0,0 +1,43 @@
+package Narwhal;
+use OX;
+
+config kioku_dsn => 'dbi:SQLite:narwhal.db';
+config kioku_extra_args => sub { { create => 1 } };
+config template_root => sub {
+ shift->param('app_root')->subdir('root', 'templates')
+}, (app_root => depends_on('/app_root'));
+
+component Redirect => 'Narwhal::Component::Redirect';
+
+component Wiki => 'Narwhal::Component::Wiki', (
+ kioku => depends_on('/Component/Kioku'),
+ tt => depends_on('/Component/TT'),
+);
+
+# turn these two into specialized classes later
+component TT => 'Template', (
+ INCLUDE_PATH => depends_on('/Config/template_root'),
+);
+
+component Kioku => 'KiokuX::Model', (
+ dsn => depends_on('/Config/kioku_dsn'),
+ extra_args => depends_on('/Config/kioku_extra_args'),
+);
+
+router as {
+ route '/' => 'redirect.permanent', (
+ to => '/page/main',
+ );
+ route '/page/:page_name' => 'wiki.page', (
+ page_name => { isa => 'Str' },
+ );
+ route '/edit/:page_name' => 'wiki.edit', (
+ page_name => { isa => 'Str' },
+ );
+}, (
+ redirect => depends_on('/Component/Redirect'),
+ wiki => depends_on('/Component/Wiki'),
+);
+
+no OX;
+1;
diff --git a/lib/Narwhal/Component/Redirect.pm b/lib/Narwhal/Component/Redirect.pm
new file mode 100644
index 0000000..58a8478
--- /dev/null
+++ b/lib/Narwhal/Component/Redirect.pm
@@ -0,0 +1,31 @@
+package Narwhal::Component::Redirect;
+use Moose;
+
+sub permanent {
+ my $self = shift;
+ my ($req) = @_;
+ my $res = $req->new_response(301);
+ $res->location($self->_get_location($req));
+ return $res;
+}
+
+sub temporary {
+ my $self = shift;
+ my ($req) = @_;
+ my $res = $req->new_response(302);
+ $res->location($self->_get_location($req));
+ return $res;
+}
+
+sub _get_location {
+ my $self = shift;
+ my ($req) = @_;
+ my $to = $req->env->{'plack.router.match'}->route->defaults->{to};
+ die "must supply a location to redirect to" unless $to;
+ return $to;
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
diff --git a/lib/Narwhal/Component/Wiki.pm b/lib/Narwhal/Component/Wiki.pm
new file mode 100644
index 0000000..5b89816
--- /dev/null
+++ b/lib/Narwhal/Component/Wiki.pm
@@ -0,0 +1,65 @@
+package Narwhal::Component::Wiki;
+use Moose;
+
+use Narwhal::Page;
+
+has kioku => (
+ isa => 'KiokuX::Model',
+ required => 1,
+ handles => 'KiokuDB::Role::API',
+);
+
+has tt => (
+ isa => 'Template',
+ required => 1,
+ handles => ['process'],
+);
+
+has scope => (
+ is => 'ro',
+ isa => 'KiokuDB::LiveObjects::Scope',
+ lazy => 1,
+ default => sub { shift->new_scope },
+);
+
+sub BUILD {
+ my $self = shift;
+ $self->scope;
+}
+
+sub page {
+ my $self = shift;
+ my ($req, $page) = @_;
+ my $page_obj = $self->lookup("page:$page");
+ return $req->new_response(404)
+ unless $page_obj;
+ my $out;
+ $self->process('page.tt', { uri_for => sub { '/' . $req->uri_for({@_}) }, text => $page_obj->text, page => $page }, \$out);
+ return $req->new_response(200, [], $out);
+}
+
+sub edit {
+ my $self = shift;
+ my ($req, $page) = @_;
+ if ($req->method eq 'POST') {
+ my $page_obj = Narwhal::Page->new(text => $req->param('text'));
+ $self->txn_do(sub {
+ $self->delete("page:$page");
+ $self->insert("page:$page" => $page_obj);
+ });
+ my $res = $req->new_response(303);
+ $res->location('/' . $req->uri_for({controller => 'wiki', action => 'page', page_name => $page}));
+ return $res;
+ }
+ else {
+ my $page_obj = $self->lookup("page:$page");
+ my $out;
+ $self->process('edit.tt', { uri_for => sub { '/' . $req->uri_for({@_}) }, text => ($page_obj ? $page_obj->text : ''), page => $page }, \$out);
+ return $req->new_response(200, [], $out);
+ }
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
diff --git a/lib/Narwhal/Page.pm b/lib/Narwhal/Page.pm
new file mode 100644
index 0000000..d3e0a4d
--- /dev/null
+++ b/lib/Narwhal/Page.pm
@@ -0,0 +1,12 @@
+package Narwhal::Page;
+use Moose;
+
+has text => (
+ is => 'ro',
+ isa => 'Str',
+);
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
diff --git a/root/templates/edit.tt b/root/templates/edit.tt
new file mode 100644
index 0000000..2b8f37e
--- /dev/null
+++ b/root/templates/edit.tt
@@ -0,0 +1,12 @@
+<html>
+<body>
+<h1>[% page %]</h1>
+<p><a href="[% uri_for('controller', 'wiki', 'action', 'page', 'page_name', page) %]">View</a></p>
+<p>
+<form method="POST" action="[% uri_for('controller', 'wiki', 'action', 'edit', 'page_name', page) %]">
+<input type="text" value="[% text %]" name="text" />
+<input type="submit" value="Submit" />
+</form>
+</p>
+</body>
+</html>
diff --git a/root/templates/page.tt b/root/templates/page.tt
new file mode 100644
index 0000000..43b111f
--- /dev/null
+++ b/root/templates/page.tt
@@ -0,0 +1,9 @@
+<html>
+<body>
+<h1>[% page %]</h1>
+<p><a href="[% uri_for('controller', 'wiki', 'action', 'edit', 'page_name', page) %]">Edit</a></p>
+<p>
+[% text %]
+</p>
+</body>
+</html>