enable SSI
[scpubgit/App-SCS.git] / lib / App / SCS / Plugin / Generate / StaticServer.pm
1 package App::SCS::Plugin::Generate::StaticServer;
2
3 use Module::Runtime qw(use_module);
4 use Web::Simple;
5
6 has dir => (is => 'ro', required => 1);
7
8 has app => (is => 'ro', required => 1);
9
10 has _dir_handler => (is => 'lazy');
11
12 sub _build__dir_handler {
13   my ($self) = @_;
14   use_module('Plack::App::File')->new(
15     root => $self->dir
16   );
17 }
18
19 has _static_handler => (is => 'lazy', builder => sub {
20   my ($self) = @_;
21   use_module('Plack::App::File')->new(
22     root => $self->app->share_dir->catdir('static')
23   );
24 });
25
26 has _ssi_handler => (is => 'lazy', builder => sub {
27   use_module('Plack::Middleware::SSI')->new
28 });
29
30 sub dispatch_request {
31   my ($self) = @_;
32   sub { $self->_ssi_handler },
33   sub (/**.*) {
34     my $path = $_[1];
35     return unless $path =~ s/(?=\/|^)-//;
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
43 sub 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
55 1;