source: bookmarks/trunk/lib/BookmarksApp.pm @ 95

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

issue #10: added basic test of the web app and the Bookmark class

To make the application testable, BookmarksApp now does not require a
config_file to be set, and can just be configured via a config hash in the
constructor. In addition, authentication and reverse proxy rewriting are now
conditionally enabled based on whether there is an auth and proxy_ip key in
the config, respecitvely.

Added Plack 1.0036 to the cpanfile dependencies, since that is the version that
contains Plack::Test.

Also fixed the Bookmarks::create_tables() method to load the
bookmarks.sql file from the correct location.

File size: 3.3 KB
Line 
1package BookmarksApp;
2
3use strict;
4use warnings;
5
6use parent qw{Plack::Component};
7use Plack::Util::Accessor qw{config_file config _app _controller};
8
9use YAML;
10use Plack::Builder;
11use Plack::Request;
12use Router::Resource;
13
14use Bookmarks::Controller;
15
16sub prepare_app {
17    my $self = shift;
18
19    my $config = $self->config;
20
21    # if the config_file is set, try to load the config from there
22    # note that currently, if there is a config_file and a config,
23    # all of the values in config are ignored in favor of those in
24    # the config_file
25    if ($self->config_file) {
26        -e $self->config_file or die "Config file " . $self->config_file . " not found\n";
27        $config = YAML::LoadFile($self->config_file);
28        #TODO: merge the configs instead of overwriting?
29        $self->config($config);
30    }
31
32    my $router = router {
33        resource '/' => sub {
34            GET {
35                # check for a uri param, and if there is one present,
36                # see if a bookmark for that URI already exists; if so
37                # redirect to that bookmark, and if not, show the form
38                # to create a new bookmark
39                if (defined $self->_controller->request->param('uri')) {
40                    return $self->_controller->find_or_new;
41                }
42
43                # otherwise return a list
44                return $self->_controller->list;
45            };
46            POST {
47                # create the bookmark and redirect to the new bookmark's edit form
48                return $self->_controller->create_and_redirect;
49            };
50        };
51
52        resource '/list' => sub {
53            GET {
54                return $self->_controller->list;
55            };
56        };
57
58        resource '/feed' => sub {
59            GET {
60                return $self->_controller->feed;
61            };
62        };
63
64        resource '/{id}' => sub {
65            GET {
66                my ($env, $params) = @_;
67                return $self->_controller->view($params->{id});
68            };
69            POST {
70                my ($env, $params) = @_;
71                return $self->_controller->update_and_redirect($params->{id});
72            };
73        };
74
75        resource '/{id}/{field}' => sub {
76            GET {
77                my ($env, $params) = @_;
78                return $self->_controller->view_field($params->{id}, $params->{field});
79            };
80        };
81    };
82
83    $self->_app(
84        builder {
85            enable_if { $_[0]->{REMOTE_ADDR} eq $config->{proxy_ip} } 'ReverseProxy'
86                if $config->{proxy_ip};
87            enable_if { $_[0]->{REQUEST_METHOD} ne 'GET' } 'Auth::Digest', (
88                realm           => 'Bookmarks',
89                secret          => $config->{digest_key},
90                password_hashed => 1,
91                authenticator   => sub { $config->{digest_password} }
92            ) if $config->{auth};
93            sub { $router->dispatch(shift); };
94        }
95    );
96}
97
98sub call {
99    my $self = shift;
100    my $env = shift;
101
102    # initialize the controller based on this request
103    $self->_controller(
104        Bookmarks::Controller->new({
105            request => Plack::Request->new($env),
106            dbname  => $self->config->{dbname},
107        })
108    );
109
110    # dispatch to the app
111    $self->_app->($env);
112}
113
114# module return
1151;
Note: See TracBrowser for help on using the repository browser.