1 | package BookmarksApp; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | |
---|
6 | use parent qw{Plack::Component}; |
---|
7 | use Plack::Util::Accessor qw{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 | my $config = $self->config; |
---|
20 | |
---|
21 | my $router = router { |
---|
22 | resource '/' => sub { |
---|
23 | GET { |
---|
24 | # check for a uri param, and if there is one present, |
---|
25 | # see if a bookmark for that URI already exists; if so |
---|
26 | # redirect to that bookmark, and if not, show the form |
---|
27 | # to create a new bookmark |
---|
28 | if (defined $self->_controller->request->param('uri')) { |
---|
29 | return $self->_controller->find_or_new; |
---|
30 | } |
---|
31 | |
---|
32 | # otherwise return a list |
---|
33 | return $self->_controller->list; |
---|
34 | }; |
---|
35 | POST { |
---|
36 | # create the bookmark and redirect to the new bookmark's edit form |
---|
37 | return $self->_controller->create_and_redirect; |
---|
38 | }; |
---|
39 | }; |
---|
40 | |
---|
41 | resource '/list' => sub { |
---|
42 | GET { |
---|
43 | return $self->_controller->list; |
---|
44 | }; |
---|
45 | }; |
---|
46 | |
---|
47 | resource '/feed' => sub { |
---|
48 | GET { |
---|
49 | return $self->_controller->feed; |
---|
50 | }; |
---|
51 | }; |
---|
52 | |
---|
53 | resource '/{id}' => sub { |
---|
54 | GET { |
---|
55 | my ($env, $params) = @_; |
---|
56 | return $self->_controller->view($params->{id}); |
---|
57 | }; |
---|
58 | POST { |
---|
59 | my ($env, $params) = @_; |
---|
60 | return $self->_controller->update_and_redirect($params->{id}); |
---|
61 | }; |
---|
62 | }; |
---|
63 | |
---|
64 | resource '/{id}/{field}' => sub { |
---|
65 | GET { |
---|
66 | my ($env, $params) = @_; |
---|
67 | return $self->_controller->view_field($params->{id}, $params->{field}); |
---|
68 | }; |
---|
69 | }; |
---|
70 | }; |
---|
71 | |
---|
72 | $self->_app( |
---|
73 | builder { |
---|
74 | enable_if { $_[0]->{REMOTE_ADDR} eq $config->{proxy_ip} } 'ReverseProxy' |
---|
75 | if $config->{proxy_ip}; |
---|
76 | enable_if { $_[0]->{REQUEST_METHOD} ne 'GET' } 'Auth::Digest', ( |
---|
77 | realm => 'Bookmarks', |
---|
78 | secret => $config->{digest_key}, |
---|
79 | password_hashed => 1, |
---|
80 | authenticator => sub { $config->{digest_password} } |
---|
81 | ) if $config->{auth}; |
---|
82 | sub { $router->dispatch(shift); }; |
---|
83 | } |
---|
84 | ); |
---|
85 | $self->_controller( |
---|
86 | Bookmarks::Controller->new({ |
---|
87 | dbname => $self->config->{dbname}, |
---|
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->request(Plack::Request->new($env)); |
---|
98 | |
---|
99 | # dispatch to the app |
---|
100 | $self->_app->($env); |
---|
101 | } |
---|
102 | |
---|
103 | # module return |
---|
104 | 1; |
---|