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