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