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 BookmarkController; |
---|
10 | |
---|
11 | #TODO: allow a different config file on the command line, or set options from the command line |
---|
12 | |
---|
13 | -e 'conf.yml' or die "Missing required conf.yml config file\n"; |
---|
14 | |
---|
15 | my $config = YAML::LoadFile('conf.yml'); |
---|
16 | |
---|
17 | sub get_controller { |
---|
18 | my $req = shift; |
---|
19 | |
---|
20 | return BookmarkController->new({ |
---|
21 | request => $req, |
---|
22 | dbname => $config->{dbname}, |
---|
23 | }); |
---|
24 | } |
---|
25 | |
---|
26 | my $router = router { |
---|
27 | resource '/' => sub { |
---|
28 | GET { |
---|
29 | my ($env) = @_; |
---|
30 | my $req = Plack::Request->new($env); |
---|
31 | my $controller = get_controller($req); |
---|
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 $req->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 $req = Plack::Request->new($env); |
---|
47 | my $controller = get_controller($req); |
---|
48 | |
---|
49 | # create the bookmark and redirect to the new bookmark's edit form |
---|
50 | return $controller->create_and_redirect; |
---|
51 | }; |
---|
52 | }; |
---|
53 | |
---|
54 | resource '/list' => sub { |
---|
55 | GET { |
---|
56 | my ($env) = @_; |
---|
57 | my $req = Plack::Request->new($env); |
---|
58 | my $controller = get_controller($req); |
---|
59 | |
---|
60 | return $controller->list; |
---|
61 | }; |
---|
62 | }; |
---|
63 | |
---|
64 | resource '/feed' => sub { |
---|
65 | GET { |
---|
66 | my ($env) = @_; |
---|
67 | my $req = Plack::Request->new($env); |
---|
68 | my $controller = get_controller($req); |
---|
69 | |
---|
70 | return $controller->feed; |
---|
71 | }; |
---|
72 | }; |
---|
73 | |
---|
74 | resource '/{id}' => sub { |
---|
75 | GET { |
---|
76 | my ($env, $params) = @_; |
---|
77 | my $req = Plack::Request->new($env); |
---|
78 | my $controller = get_controller($req); |
---|
79 | |
---|
80 | return $controller->view($params->{id}); |
---|
81 | }; |
---|
82 | POST { |
---|
83 | my ($env, $params) = @_; |
---|
84 | my $req = Plack::Request->new($env); |
---|
85 | my $controller = get_controller($req); |
---|
86 | |
---|
87 | return $controller->update_and_redirect($params->{id}); |
---|
88 | }; |
---|
89 | }; |
---|
90 | |
---|
91 | resource '/{id}/{field}' => sub { |
---|
92 | GET { |
---|
93 | my ($env, $params) = @_; |
---|
94 | my $req = Plack::Request->new($env); |
---|
95 | my $controller = get_controller($req); |
---|
96 | |
---|
97 | return $controller->view_field($params->{id}, $params->{field}); |
---|
98 | }; |
---|
99 | }; |
---|
100 | }; |
---|
101 | |
---|
102 | builder { |
---|
103 | enable_if { $_[0]->{REMOTE_ADDR} eq $config->{proxy_ip} } 'ReverseProxy'; |
---|
104 | enable_if { $_[0]->{REQUEST_METHOD} ne 'GET' } 'Auth::Digest', ( |
---|
105 | realm => 'Bookmarks', |
---|
106 | secret => $config->{digest_key}, |
---|
107 | password_hashed => 1, |
---|
108 | authenticator => sub { $config->{digest_password} } |
---|
109 | ); |
---|
110 | sub { $router->dispatch(shift); }; |
---|
111 | }; |
---|