Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 69 lines (60 sloc) 1.91 kB
b8ff039 @simoncozens first commit
authored
1 package MicroMaypole;
2 use Template;
713e809 @simoncozens Ditch HTTP::Engine, go pure-PSGI.
authored
3 use Plack::Request;
4 use Plack::Response;
b8ff039 @simoncozens first commit
authored
5
6 sub app {
7 my ($self, %args) = @_;
8 if (!$args{model_prefix}) { die "You didn't pass a model prefix!" }
9 my $t = Template->new({
10 INCLUDE_PATH => $args{templates} || "templates",
11 #PRE_PROCESS => "header",
12 #POST_PROCESS => "footer",
13 COMPILE_DIR => $args{compiled_templates},
14 COMPILE_EXT => ".ttc"
15 });
16
713e809 @simoncozens Ditch HTTP::Engine, go pure-PSGI.
authored
17 sub {
18 my $req = Plack::Request->new(shift);
19 my $m = $self->new(%args);
20 $m->{template_engine} = $t;
21 $m->handler($req)->finalize;
22 }
b8ff039 @simoncozens first commit
authored
23 }
24
25 sub new { my $self = shift; return bless {@_} , $self }
26
35f8820 @simoncozens Move stuff into Canary.pm, allow authentication phase, force user to …
authored
27 sub authenticate {}
28
b8ff039 @simoncozens first commit
authored
29 sub do404 {
30 my $res = shift->respond("404handler");
31 $res->status(404);
32 $res;
33 }
34
35 sub handler {
36 my ($self, $req) = @_;
37 $self->{req} = $req;
35f8820 @simoncozens Move stuff into Canary.pm, allow authentication phase, force user to …
authored
38 if ($resp = $self->authenticate()) { return $resp }
b8ff039 @simoncozens first commit
authored
39 my (undef, $noun, $verb, @args) = split /\//, $req->path;
6a6eec8 @simoncozens MM should have its own, subclassable way of manipulating the path. Co…
authored
40 if (!$noun) { $noun = "mail"; $verb = "recent" } # XXX shouldn't be here
b03f15f @simoncozens A variety of hacks and restructurings which makes more of the links w…
authored
41 $self->{req}{noun} = $noun;
42 $self->{req}{verb} = $verb;
b8ff039 @simoncozens first commit
authored
43 # Convert "noun" to model prefix
b03f15f @simoncozens A variety of hacks and restructurings which makes more of the links w…
authored
44 $req->{template} = "$noun/$verb";
b8ff039 @simoncozens first commit
authored
45 $noun =~ s/_(\w)/\U\1/g; my $class = $self->{model_prefix}."::".ucfirst($noun);
46 # Does this class even exist?
47 if (!$class->isa("UNIVERSAL")) { return $self->do404(); }
48 if (!$class->can($verb)) {
49 warn "Can't call method $verb on class $class" ;
50 return $self->do404();
51 }
52 $class->$verb($self, @args);
53 }
54
55 sub respond {
56 my ($self, $template, @args) = @_;
57 my $out;
b03f15f @simoncozens A variety of hacks and restructurings which makes more of the links w…
authored
58 $template ||= $self->{req}->{template};
713e809 @simoncozens Ditch HTTP::Engine, go pure-PSGI.
authored
59 my $res = Plack::Response->new(200);
60 $res->content_type("text/html");
b8ff039 @simoncozens first commit
authored
61 $self->{template_engine}->process($template, {
62 self => $self,
63 @args,
64 }, \$out) ? $res->body($out) : $res->body(die $self->{template_engine}->error);
65 return $res;
66 }
67
68 1;
Something went wrong with that request. Please try again.