Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add some CODES
  • Loading branch information
lukec committed Oct 10, 2010
1 parent 9abc010 commit c705838
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 8 deletions.
14 changes: 8 additions & 6 deletions app.psgi
@@ -1,13 +1,15 @@
#!perl
use lib 'lib';
use Plack::Builder;
use Plack::App::Directory;
use Plack::App::File;
use MakeYourOwnAdventure::Stories;

builder {
mount "/static" => builder {
$app = Plack::App::Directory->new({ root => "static" })->to_app;
};
mount "/" => builder {
$app = Plack::App::File->new(file => "static/index.html");
}
mount "/static" =>
Plack::App::Directory->new({ root => "static" })->to_app;
mount "/" =>
Plack::App::File->new(file => "static/index.html")->to_app;
mount "/stories" =>
MakeYourOwnAdventure::Stories->new()->to_app;
};
4 changes: 2 additions & 2 deletions design.txt
Expand Up @@ -14,8 +14,8 @@ Story Page
* Vote


APIs(democracy.local:5000):
* PUT /stories
APIs:
* POST /stories (Create a new story)
** title, story_so_far
* GET /stories
* GET /stories/:name
Expand Down
66 changes: 66 additions & 0 deletions lib/MakeYourOwnAdventure/Controller.pm
@@ -0,0 +1,66 @@
package MakeYourOwnAdventure::Controller;
use Moose::Role;
use TokyoCabinet;
use Guard;
use JSON qw/decode_json encode_json/;

has 'db' => (is => 'ro', isa => 'Object', lazy_build => 1);

sub _build_db {
my $self = shift;
my $db = TokyoCabinet::HDB->new;
return $db;
}

sub read_db {
my $self = shift;
my $db = $self->db;
if(!$db->open("data/casket.tch", $db->OREADER | $db->OCREAT)){
my $ecode = $db->ecode();
printf STDERR ("open error: %s\n", $db->errmsg($ecode));
}
return $db, guard { $db->close; $self->clear_db };
}

sub write_db {
my $self = shift;
my $db = $self->db;
if(!$db->open("data/casket.tch", $db->OWRITER | $db->OCREAT)){
my $ecode = $db->ecode();
die "open error: ".$db->errmsg($ecode);
}
return $db, guard { $db->close; $self->clear_db };
}

sub get_stories {
my $self = shift;
my ($db,$g) = $self->read_db;
$db->iterinit();
my @stories;
while (defined(my $key = $db->iternext())) {
my $value = $db->get($key);
next unless (defined($value));
push @stories, json_decode($value);
}
return \@stories;
}

sub get_story {
my ($self, $key) = @_;
my ($db,$g) = $self->read_db;
my $value = $db->get($key);
return unless defined($value);
return json_decode($value);
}

sub put_story {
my ($self, $key, $hash) = @_;
my ($db,$g) = $self->write_db;
my $value = json_encode($hash);
unless ($db->put($key,$value)) {
my $ecode = $db->ecode();
die "put error: ".$db->errmsg($ecode);
}
}

1;
43 changes: 43 additions & 0 deletions lib/MakeYourOwnAdventure/Stories.pm
@@ -0,0 +1,43 @@
package MakeYourOwnAdventure::Stories;
use Moose;
use JSON qw/decode_json encode_json/;
use Plack::Request;
use namespace::clean -except => 'meta';

with 'MakeYourOwnAdventure::Controller';

sub to_app {
my $self = shift;

return sub {
my $env = shift;
my $req = Plack::Request->new($env);

# GET or PUT?
if ($req->method eq 'GET') {
my $content = encode_json($self->get_stories);
return [200, ['Content-Type' => 'application/json'], [\$content]];
}
elsif ($req->method eq 'POST') {
my $title = $req->param('title');
my $story_so_far = $req->param('story_so_far');

unless ($title and $story_so_far) {
return [400, ['Content-Type' => 'text/plain'], ["Bad params"]];
}

(my $name = $title) =~ s/[^\w]+/_/g;
my $story = {
name => $name,
title => $title,
story => $story_so_far,
};
$self->put_story($name, $story);
return [201, ['Location' => "/stories/$name", 'Content-Type' => 'application/json'], [encode_json($story)]];
}
};
}


__PACKAGE__->meta->make_immutable;

0 comments on commit c705838

Please sign in to comment.