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 | 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 | |
---|
98 | sub 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 |
---|
115 | 1; |
---|