Changeset 59 in bookmarks for trunk/app.psgi
- Timestamp:
- 08/23/13 15:35:17 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/app.psgi
r55 r59 4 4 use YAML; 5 5 use Plack::Builder; 6 use Plack::Request; 7 use Router::Resource; 6 8 7 use BookmarkApp::Dispatch; 9 use BookmarkController; 10 use Bookmarks; 11 12 #TODO: allow a different config file on the command line, or set options from the command line 8 13 9 14 -e 'conf.yml' or die "Missing required conf.yml config file\n"; 10 15 11 16 my $config = YAML::LoadFile('conf.yml'); 12 my $app = BookmarkApp::Dispatch->as_psgi( 13 args_to_new => { 14 PARAMS => { dbname => $config->{dbname} }, 15 }, 16 ); 17 18 sub get_controller { 19 my $req = shift; 20 21 my $controller = BookmarkController->new({ 22 request => $req, 23 }); 24 my $bookmarks = Bookmarks->new({ 25 dbname => $config->{dbname}, 26 base_uri => $controller->base_uri, 27 }); 28 $controller->bookmarks($bookmarks); 29 return $controller; 30 } 31 32 my $router = router { 33 resource '/' => sub { 34 GET { 35 my ($env) = @_; 36 my $req = Plack::Request->new($env); 37 my $controller = get_controller($req); 38 39 # check for a uri param, and if there is one present, 40 # see if a bookmark for that URI already exists; if so 41 # redirect to that bookmark, and if not, show the form 42 # to create a new bookmark 43 if (defined $req->param('uri')) { 44 return $controller->find_or_new; 45 } 46 47 # otherwise return a list 48 return $controller->list; 49 }; 50 POST { 51 my ($env) = @_; 52 my $req = Plack::Request->new($env); 53 my $controller = get_controller($req); 54 55 # create the bookmark and redirect to the new bookmark's edit form 56 return $controller->create; 57 }; 58 }; 59 60 resource '/list' => sub { 61 GET { 62 my ($env) = @_; 63 my $req = Plack::Request->new($env); 64 my $controller = get_controller($req); 65 66 return $controller->list; 67 }; 68 }; 69 70 resource '/feed' => sub { 71 GET { 72 my ($env) = @_; 73 my $req = Plack::Request->new($env); 74 my $controller = get_controller($req); 75 76 return $controller->feed; 77 }; 78 }; 79 80 resource '/{id}' => sub { 81 GET { 82 my ($env, $params) = @_; 83 my $req = Plack::Request->new($env); 84 my $controller = get_controller($req); 85 86 return $controller->view($params->{id}); 87 }; 88 POST { 89 my ($env, $params) = @_; 90 my $req = Plack::Request->new($env); 91 my $controller = get_controller($req); 92 93 return $controller->edit($params->{id}); 94 95 return [200, ['Content-Type' => 'text/plain'], ['update ', $params->{id}]]; 96 }; 97 }; 98 99 resource '/{id}/{field}' => sub { 100 GET { 101 my ($env, $params) = @_; 102 my $req = Plack::Request->new($env); 103 my $controller = get_controller($req); 104 105 return $controller->view_field($params->{id}, $params->{field}); 106 }; 107 }; 108 }; 109 110 #builder { 111 # sub { $router->dispatch(shift); }; 112 #}; 17 113 18 114 builder { … … 24 120 authenticator => sub { $config->{digest_password} } 25 121 ); 26 $app;122 sub { $router->dispatch(shift); }; 27 123 };
Note: See TracChangeset
for help on using the changeset viewer.