Changeset 81 in bookmarks for trunk/app.psgi


Ignore:
Timestamp:
05/27/15 17:13:40 (5 years ago)
Author:
peter
Message:

issue #1: moved BookmarksApp code into its own *.pm file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/app.psgi

    r79 r81  
    22use strict; 
    33 
     4use BookmarksApp; 
     5 
    46#TODO: allow individual options to be set via environment vars, too 
    57BookmarksApp->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}; 
    11  
    12 use YAML; 
    13 use Plack::Builder; 
    14 use Plack::Request; 
    15 use Router::Resource; 
    16  
    17 use Bookmarks::Controller; 
    18  
    19 sub prepare_app { 
    20     my $self = shift; 
    21  
    22     -e $self->config_file or die "Config file " . $self->config_file . " not found\n"; 
    23  
    24     my $config = YAML::LoadFile($self->config_file); 
    25     $self->config($config); 
    26  
    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                 } 
    37  
    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         }; 
    46  
    47         resource '/list' => sub { 
    48             GET { 
    49                 return $self->_controller->list; 
    50             }; 
    51         }; 
    52  
    53         resource '/feed' => sub { 
    54             GET { 
    55                 return $self->_controller->feed; 
    56             }; 
    57         }; 
    58  
    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             }; 
    68         }; 
    69  
    70         resource '/{id}/{field}' => sub { 
    71             GET { 
    72                 my ($env, $params) = @_; 
    73                 return $self->_controller->view_field($params->{id}, $params->{field}); 
    74             }; 
    75         }; 
    76     }; 
    77  
    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 } 
    91  
    92 sub call { 
    93     my $self = shift; 
    94     my $env = shift; 
    95  
    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     ); 
    103  
    104     # dispatch to the app 
    105     $self->_app->($env); 
    106 } 
Note: See TracChangeset for help on using the changeset viewer.