source: bookmarks/trunk/app.psgi @ 59

Last change on this file since 59 was 59, checked in by peter, 11 years ago
  • converted app.psgi from using a CGI::Application::Dispatch dispatcher to a Path::Router dispatcher
  • created a more pure PSGI BookmarkController to replace the CGI::Application-based BookmarkApp
  • added an access log to the start script
  • Property svn:executable set to *
File size: 3.2 KB
Line 
1#!/usr/bin/perl -w
2use strict;
3
4use YAML;
5use Plack::Builder;
6use Plack::Request;
7use Router::Resource;
8
9use BookmarkController;
10use 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
16my $config = YAML::LoadFile('conf.yml');
17
18sub 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
32my $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#    sub { $router->dispatch(shift); };
112#};
113
114builder {
115    enable_if { $_[0]->{REMOTE_ADDR} eq $config->{proxy_ip} } 'ReverseProxy';
116    enable_if { $_[0]->{REQUEST_METHOD} ne 'GET' } 'Auth::Digest', (
117        realm           => 'Bookmarks',
118        secret          => $config->{digest_key},
119        password_hashed => 1,
120        authenticator   => sub { $config->{digest_password} }
121    );
122    sub { $router->dispatch(shift); };
123};
Note: See TracBrowser for help on using the repository browser.