Permalink
Browse files

Amon2をこんな感じに使ったらいいのでは?という例

  • Loading branch information...
0 parents commit de9b516432031e45c438cb4232826f87bfcbdb13 @kentaro committed Feb 9, 2012
@@ -0,0 +1,14 @@
+use strict;
+use warnings;
+
+use Plack::Builder;
+
+use lib 'lib';
+use lib glob 'modules/*/lib';
+
+use MyApp::Web;
+
+builder {
+ enable 'Plack::Middleware::XFramework' => framework => 'Amon2';
+ MyApp::Web->to_app;
+};
@@ -0,0 +1,13 @@
+package MyApp;
+use strict;
+use warnings;
+use parent qw(Amon2);
+
+our $VERSION = '0.01';
+
+# Amon2#configは使わない
+use MyApp::Config;
+sub load_config { MyApp::Config->current }
+sub config { MyApp::Config->current }
+
+!!1;
@@ -0,0 +1,22 @@
+package MyApp::Config;
+use strict;
+use warnings;
+
+use Config::ENV MYAPP_ENV => default => 'default';
+
+use Path::Class qw(file);
+my $root = file(__FILE__)->dir->parent->parent;
+sub root { $root }
+
+common +{
+ title => 'MyApp',
+ view => {},
+};
+
+# ここに環境変数ごとのdsnとかを書く
+config default => +{};
+config test => +{};
+config development => +{};
+config production => +{};
+
+!!1;
@@ -0,0 +1,9 @@
+package MyApp::Config::Route;
+use strict;
+use warnings;
+
+use MyApp::Router;
+
+route '/' => 'Root#index';
+
+!!1;
@@ -0,0 +1,6 @@
+package MyApp::Request;
+use strict;
+use warnings;
+use parent qw(Amon2::Web::Request);
+
+!!1;
@@ -0,0 +1,6 @@
+package MyApp::Response;
+use strict;
+use warnings;
+use parent qw(Amon2::Web::Response);
+
+!!1;
@@ -0,0 +1,59 @@
+package MyApp::Router;
+use strict;
+use warnings;
+use parent qw(Exporter);
+
+our @EXPORT = qw(route);
+our @EXPORT_OK = @EXPORT;
+
+use MyApp::Util;
+
+use Router::Simple;
+my $router = Router::Simple->new;
+sub router () { $router }
+
+sub route ($$) {
+ my ($src, $dist) = @_;
+
+ # ex: route '/' => 'Root#index'
+ if (@_ == 2 && !ref $_[1]) {
+ my ($path, $dest_str) = @_;
+ my ($controller, $action) = split('#', $dest_str);
+ my %dest;
+
+ $dest{controller} = $controller;
+ $dest{action} = $action if defined $action;
+
+ $router->connect($path, \%dest);
+ }
+
+ # ex: route '/' => { controller => 'Root', action => 'index' }
+ else {
+ $router->connect(@_);
+ }
+}
+
+my %CLASS_LOADED;
+sub dispatch {
+ my ($class, $c) = @_;
+ my $req = $c->request;
+
+ if (my $matched = $router->match($req->env)) {
+ my $type = $ENV{MYAPP_WEB} ? 'Web' : 'CLI';
+ my $controller = sprintf 'MyApp::%s::%s', $type, $matched->{controller};
+ my $action = $matched->{action};
+
+ $CLASS_LOADED{$controller} //= do {
+ MyApp::Util::load_class($controller);
+ $controller;
+ };
+
+ $c->{args} = $matched;
+ $controller->new->$action($c, $matched);
+ }
+ else {
+ $c->res_404();
+ }
+}
+
+!!1;
@@ -0,0 +1,10 @@
+package MyApp::Util;
+use strict;
+use warnings;
+
+use Class::Load qw(load_class);
+
+use MyApp::Config;
+sub root () { MyApp::Config->root }
+
+!!1;
@@ -0,0 +1,61 @@
+package MyApp::Web;
+use strict;
+use warnings;
+use parent qw(MyApp Amon2::Web);
+
+BEGIN { $ENV{MYAPP_WEB} = 1 }
+
+use MyApp::Util;
+use MyApp::Request;
+use MyApp::Response;
+
+use MyApp::Router;
+use MyApp::Config::Route;
+
+# CSRFがどうのとかは入れといた方がいいでしょう
+__PACKAGE__->load_plugins(qw());
+
+__PACKAGE__->add_trigger(
+ AFTER_DISPATCH => sub {
+ my ($c, $res) = @_;
+
+ $res->header('X-Content-Type-Options' => 'nosniff');
+ $res->header('X-Frame-Options' => 'DENY');
+ $res->header('Cache-Control' => 'private');
+ },
+);
+
+__PACKAGE__->add_trigger(
+ BEFORE_DISPATCH => sub {
+ my ($c) = @_;
+ },
+);
+
+# Amon2 hook points
+sub create_request { MyApp::Request->new($_[1]) }
+sub create_response { shift; MyApp::Response->new(@_) }
+sub dispatch { MyApp::Router->dispatch($_[0]) or die "response is not generated" }
+
+use Text::Xslate;
+
+my $view_conf = __PACKAGE__->config->{'view'} || {};
+
+if (!exists $view_conf->{path}) {
+ $view_conf->{path} = MyApp::Util->root->subdir('templates')->stringify;
+}
+
+my $view = Text::Xslate->new({
+ syntax => 'TTerse',
+ module => [ 'Text::Xslate::Bridge::Star' ],
+ function => {
+ c => sub { Amon2->context() },
+ uri_with => sub { Amon2->context()->req->uri_with(@_) },
+ uri_for => sub { Amon2->context()->uri_for(@_) },
+ },
+
+ %$view_conf
+});
+
+sub create_view { $view }
+
+!!1;
@@ -0,0 +1,7 @@
+package MyApp::Web::Base;
+use strict;
+use warnings;
+
+sub new { bless {}, shift }
+
+!!1;
@@ -0,0 +1,11 @@
+package MyApp::Web::Root;
+use strict;
+use warnings;
+use parent qw(MyApp::Web::Base);
+
+sub index {
+ my ($self, $c, $args) = @_;
+ $c->render('index.tt');
+}
+
+!!1;
@@ -0,0 +1 @@
+Hello, [% c().config().title %]!!1

0 comments on commit de9b516

Please sign in to comment.