source: bookmarks/trunk/app.psgi @ 79

Last change on this file since 79 was 79, checked in by peter, 9 years ago

issue #1: started to convert app.psgi into a Plack::Component-based module

  • Property svn:executable set to *
File size: 3.0 KB
RevLine 
[53]1#!/usr/bin/perl -w
2use strict;
3
[79]4#TODO: allow individual options to be set via environment vars, too
5BookmarksApp->new({ config_file => $ENV{CONFIG_FILE} })->to_app;
6
7package BookmarksApp;
8
9use parent qw{Plack::Component};
10use Plack::Util::Accessor qw{config_file config _app _controller};
11
[53]12use YAML;
13use Plack::Builder;
[59]14use Plack::Request;
15use Router::Resource;
[53]16
[70]17use Bookmarks::Controller;
[53]18
[79]19sub prepare_app {
20    my $self = shift;
[59]21
[79]22    -e $self->config_file or die "Config file " . $self->config_file . " not found\n";
[53]23
[79]24    my $config = YAML::LoadFile($self->config_file);
25    $self->config($config);
[53]26
[79]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                }
[59]37
[79]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            };
[59]45        };
46
[79]47        resource '/list' => sub {
48            GET {
49                return $self->_controller->list;
50            };
[59]51        };
52
[79]53        resource '/feed' => sub {
54            GET {
55                return $self->_controller->feed;
56            };
[59]57        };
58
[79]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            };
[59]68        };
69
[79]70        resource '/{id}/{field}' => sub {
71            GET {
72                my ($env, $params) = @_;
73                return $self->_controller->view_field($params->{id}, $params->{field});
74            };
[59]75        };
76    };
77
[79]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}
[59]91
[79]92sub call {
93    my $self = shift;
94    my $env = shift;
[59]95
[79]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        })
[53]102    );
[79]103
104    # dispatch to the app
105    $self->_app->($env);
106}
Note: See TracBrowser for help on using the repository browser.