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