enable SSI
[scpubgit/App-SCS.git] / lib / App / SCS / Plugin / Generate / StaticServer.pm
CommitLineData
2f1cc197 1package App::SCS::Plugin::Generate::StaticServer;
2
3use Module::Runtime qw(use_module);
4use Web::Simple;
5
6has dir => (is => 'ro', required => 1);
7
8has app => (is => 'ro', required => 1);
9
10has _dir_handler => (is => 'lazy');
11
12sub _build__dir_handler {
13 my ($self) = @_;
14 use_module('Plack::App::File')->new(
15 root => $self->dir
16 );
17}
18
bb6a4cec 19has _static_handler => (is => 'lazy', builder => sub {
2f1cc197 20 my ($self) = @_;
21 use_module('Plack::App::File')->new(
22 root => $self->app->share_dir->catdir('static')
23 );
bb6a4cec 24});
25
26has _ssi_handler => (is => 'lazy', builder => sub {
27 use_module('Plack::Middleware::SSI')->new
28});
2f1cc197 29
30sub dispatch_request {
31 my ($self) = @_;
bb6a4cec 32 sub { $self->_ssi_handler },
2f1cc197 33 sub (/**.*) {
34 my $path = $_[1];
0ea3d9ae 35 return unless $path =~ s/(?=\/|^)-//;
2f1cc197 36 App::SCS::Web::redispatch_to("/static/${path}");
37 },
38 sub (/static/...) { $self->_static_handler },
39 sub (/favicon + .ico) { $self->_static_handler },
40 $self->_dir_handler
41}
42
43sub run_server {
44 my ($self, $env) = @_;
45 my @args = @{$env->{argv}};
46 my $r = use_module('Plack::Runner')->new(
47 server => 'Starman',
48 app => $self->app->web->to_psgi_app
49 );
50 $r->parse_options(@args);
51 $r->set_options(argv => \@args);
52 $r->run;
53}
54
551;