source: bookmarks/trunk/app.psgi @ 61

Last change on this file since 61 was 61, checked in by peter, 11 years ago
  • wrap the creation of the Bookmarks object into the BookmarkController
  • bookmark is a lazy-loaded read-only attribute with a builder in BookmarkController
  • made dbname and request required attributes of BookmarkController
  • updated the api notes
  • Property svn:executable set to *
File size: 3.0 KB
Line 
1#!/usr/bin/perl -w
2use strict;
3
4use YAML;
5use Plack::Builder;
6use Plack::Request;
7use Router::Resource;
8
9use 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
15my $config = YAML::LoadFile('conf.yml');
16
17sub get_controller {
18    my $req = shift;
19
20    return BookmarkController->new({
21        request => $req,
22        dbname  => $config->{dbname},
23    });
24}
25
26my $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;
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->edit($params->{id});
88
89            return [200, ['Content-Type' => 'text/plain'], ['update ', $params->{id}]];
90        };
91    };
92
93    resource '/{id}/{field}' => sub {
94        GET {
95            my ($env, $params) = @_;
96            my $req = Plack::Request->new($env);
97            my $controller = get_controller($req);
98
99            return $controller->view_field($params->{id}, $params->{field});
100        };
101    };
102};
103
104builder {
105    enable_if { $_[0]->{REMOTE_ADDR} eq $config->{proxy_ip} } 'ReverseProxy';
106    enable_if { $_[0]->{REQUEST_METHOD} ne 'GET' } 'Auth::Digest', (
107        realm           => 'Bookmarks',
108        secret          => $config->{digest_key},
109        password_hashed => 1,
110        authenticator   => sub { $config->{digest_password} }
111    );
112    sub { $router->dispatch(shift); };
113};
Note: See TracBrowser for help on using the repository browser.