source: bookmarks/trunk/app.psgi @ 79

Last change on this file since 79 was 79, checked in by peter, 9 years ago

issue #1: started to convert app.psgi into a Plack::Component-based module

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