Changeset 79 in bookmarks for trunk/app.psgi
- Timestamp:
- 04/01/15 17:25:13 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/app.psgi
r70 r79 1 1 #!/usr/bin/perl -w 2 2 use strict; 3 4 #TODO: allow individual options to be set via environment vars, too 5 BookmarksApp->new({ config_file => $ENV{CONFIG_FILE} })->to_app; 6 7 package BookmarksApp; 8 9 use parent qw{Plack::Component}; 10 use Plack::Util::Accessor qw{config_file config _app _controller}; 3 11 4 12 use YAML; … … 9 17 use Bookmarks::Controller; 10 18 11 #TODO: allow individual options to be set via environment vars, too 19 sub prepare_app { 20 my $self = shift; 12 21 13 -e $ENV{CONFIG_FILE} or die "Config file $ENV{CONFIG_FILE}not found\n";22 -e $self->config_file or die "Config file " . $self->config_file . " not found\n"; 14 23 15 my $config = YAML::LoadFile($ENV{CONFIG_FILE}); 24 my $config = YAML::LoadFile($self->config_file); 25 $self->config($config); 16 26 17 sub get_controller { 18 my $env = shift; 19 my $req = Plack::Request->new($env); 27 my $router = router { 28 resource '/' => sub { 29 GET { 30 # check for a uri param, and if there is one present, 31 # see if a bookmark for that URI already exists; if so 32 # redirect to that bookmark, and if not, show the form 33 # to create a new bookmark 34 if (defined $self->_controller->request->param('uri')) { 35 return $self->_controller->find_or_new; 36 } 20 37 21 return Bookmarks::Controller->new({ 22 request => $req, 23 dbname => $config->{dbname}, 24 }); 25 } 38 # otherwise return a list 39 return $self->_controller->list; 40 }; 41 POST { 42 # create the bookmark and redirect to the new bookmark's edit form 43 return $self->_controller->create_and_redirect; 44 }; 45 }; 26 46 27 my $router = router{28 resource '/' => sub{29 GET {30 my ($env) = @_;31 my $controller = get_controller($env);47 resource '/list' => sub { 48 GET { 49 return $self->_controller->list; 50 }; 51 }; 32 52 33 # check for a uri param, and if there is one present, 34 # see if a bookmark for that URI already exists; if so 35 # redirect to that bookmark, and if not, show the form 36 # to create a new bookmark 37 if (defined $controller->request->param('uri')) { 38 return $controller->find_or_new; 39 } 53 resource '/feed' => sub { 54 GET { 55 return $self->_controller->feed; 56 }; 57 }; 40 58 41 # otherwise return a list 42 return $controller->list; 59 resource '/{id}' => sub { 60 GET { 61 my ($env, $params) = @_; 62 return $self->_controller->view($params->{id}); 63 }; 64 POST { 65 my ($env, $params) = @_; 66 return $self->_controller->update_and_redirect($params->{id}); 67 }; 43 68 }; 44 POST {45 my ($env) = @_;46 my $controller = get_controller($env);47 69 48 # create the bookmark and redirect to the new bookmark's edit form 49 return $controller->create_and_redirect; 70 resource '/{id}/{field}' => sub { 71 GET { 72 my ($env, $params) = @_; 73 return $self->_controller->view_field($params->{id}, $params->{field}); 74 }; 50 75 }; 51 76 }; 52 77 53 resource '/list' => sub { 54 GET { 55 my ($env) = @_; 56 my $controller = get_controller($env); 78 $self->_app( 79 builder { 80 enable_if { $_[0]->{REMOTE_ADDR} eq $config->{proxy_ip} } 'ReverseProxy'; 81 enable_if { $_[0]->{REQUEST_METHOD} ne 'GET' } 'Auth::Digest', ( 82 realm => 'Bookmarks', 83 secret => $config->{digest_key}, 84 password_hashed => 1, 85 authenticator => sub { $config->{digest_password} } 86 ); 87 sub { $router->dispatch(shift); }; 88 } 89 ); 90 } 57 91 58 return $controller->list; 59 };60 };92 sub call { 93 my $self = shift; 94 my $env = shift; 61 95 62 resource '/feed' => sub { 63 GET { 64 my ($env) = @_; 65 my $controller = get_controller($env); 96 # initialize the controller based on this request 97 $self->_controller( 98 Bookmarks::Controller->new({ 99 request => Plack::Request->new($env), 100 dbname => $self->config->{dbname}, 101 }) 102 ); 66 103 67 return $controller->feed; 68 }; 69 }; 70 71 resource '/{id}' => sub { 72 GET { 73 my ($env, $params) = @_; 74 my $controller = get_controller($env); 75 76 return $controller->view($params->{id}); 77 }; 78 POST { 79 my ($env, $params) = @_; 80 my $controller = get_controller($env); 81 82 return $controller->update_and_redirect($params->{id}); 83 }; 84 }; 85 86 resource '/{id}/{field}' => sub { 87 GET { 88 my ($env, $params) = @_; 89 my $controller = get_controller($env); 90 91 return $controller->view_field($params->{id}, $params->{field}); 92 }; 93 }; 94 }; 95 96 builder { 97 enable_if { $_[0]->{REMOTE_ADDR} eq $config->{proxy_ip} } 'ReverseProxy'; 98 enable_if { $_[0]->{REQUEST_METHOD} ne 'GET' } 'Auth::Digest', ( 99 realm => 'Bookmarks', 100 secret => $config->{digest_key}, 101 password_hashed => 1, 102 authenticator => sub { $config->{digest_password} } 103 ); 104 sub { $router->dispatch(shift); }; 105 }; 104 # dispatch to the app 105 $self->_app->($env); 106 }
Note: See TracChangeset
for help on using the changeset viewer.