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 | use Bookmarks; |
---|
11 | |
---|
12 | #TODO: allow a different config file on the command line, or set options from the command line |
---|
13 | |
---|
14 | -e 'conf.yml' or die "Missing required conf.yml config file\n"; |
---|
15 | |
---|
16 | my $config = YAML::LoadFile('conf.yml'); |
---|
17 | |
---|
18 | sub get_controller { |
---|
19 | my $req = shift; |
---|
20 | |
---|
21 | my $controller = BookmarkController->new({ |
---|
22 | request => $req, |
---|
23 | }); |
---|
24 | my $bookmarks = Bookmarks->new({ |
---|
25 | dbname => $config->{dbname}, |
---|
26 | base_uri => $controller->base_uri, |
---|
27 | }); |
---|
28 | $controller->bookmarks($bookmarks); |
---|
29 | return $controller; |
---|
30 | } |
---|
31 | |
---|
32 | my $router = router { |
---|
33 | resource '/' => sub { |
---|
34 | GET { |
---|
35 | my ($env) = @_; |
---|
36 | my $req = Plack::Request->new($env); |
---|
37 | my $controller = get_controller($req); |
---|
38 | |
---|
39 | # check for a uri param, and if there is one present, |
---|
40 | # see if a bookmark for that URI already exists; if so |
---|
41 | # redirect to that bookmark, and if not, show the form |
---|
42 | # to create a new bookmark |
---|
43 | if (defined $req->param('uri')) { |
---|
44 | return $controller->find_or_new; |
---|
45 | } |
---|
46 | |
---|
47 | # otherwise return a list |
---|
48 | return $controller->list; |
---|
49 | }; |
---|
50 | POST { |
---|
51 | my ($env) = @_; |
---|
52 | my $req = Plack::Request->new($env); |
---|
53 | my $controller = get_controller($req); |
---|
54 | |
---|
55 | # create the bookmark and redirect to the new bookmark's edit form |
---|
56 | return $controller->create; |
---|
57 | }; |
---|
58 | }; |
---|
59 | |
---|
60 | resource '/list' => sub { |
---|
61 | GET { |
---|
62 | my ($env) = @_; |
---|
63 | my $req = Plack::Request->new($env); |
---|
64 | my $controller = get_controller($req); |
---|
65 | |
---|
66 | return $controller->list; |
---|
67 | }; |
---|
68 | }; |
---|
69 | |
---|
70 | resource '/feed' => sub { |
---|
71 | GET { |
---|
72 | my ($env) = @_; |
---|
73 | my $req = Plack::Request->new($env); |
---|
74 | my $controller = get_controller($req); |
---|
75 | |
---|
76 | return $controller->feed; |
---|
77 | }; |
---|
78 | }; |
---|
79 | |
---|
80 | resource '/{id}' => sub { |
---|
81 | GET { |
---|
82 | my ($env, $params) = @_; |
---|
83 | my $req = Plack::Request->new($env); |
---|
84 | my $controller = get_controller($req); |
---|
85 | |
---|
86 | return $controller->view($params->{id}); |
---|
87 | }; |
---|
88 | POST { |
---|
89 | my ($env, $params) = @_; |
---|
90 | my $req = Plack::Request->new($env); |
---|
91 | my $controller = get_controller($req); |
---|
92 | |
---|
93 | return $controller->edit($params->{id}); |
---|
94 | |
---|
95 | return [200, ['Content-Type' => 'text/plain'], ['update ', $params->{id}]]; |
---|
96 | }; |
---|
97 | }; |
---|
98 | |
---|
99 | resource '/{id}/{field}' => sub { |
---|
100 | GET { |
---|
101 | my ($env, $params) = @_; |
---|
102 | my $req = Plack::Request->new($env); |
---|
103 | my $controller = get_controller($req); |
---|
104 | |
---|
105 | return $controller->view_field($params->{id}, $params->{field}); |
---|
106 | }; |
---|
107 | }; |
---|
108 | }; |
---|
109 | |
---|
110 | builder { |
---|
111 | enable_if { $_[0]->{REMOTE_ADDR} eq $config->{proxy_ip} } 'ReverseProxy'; |
---|
112 | enable_if { $_[0]->{REQUEST_METHOD} ne 'GET' } 'Auth::Digest', ( |
---|
113 | realm => 'Bookmarks', |
---|
114 | secret => $config->{digest_key}, |
---|
115 | password_hashed => 1, |
---|
116 | authenticator => sub { $config->{digest_password} } |
---|
117 | ); |
---|
118 | sub { $router->dispatch(shift); }; |
---|
119 | }; |
---|