handle /- paths
[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
19has _static_handler => (is => 'lazy');
20
21sub _build__static_handler {
22 my ($self) = @_;
23 use_module('Plack::App::File')->new(
24 root => $self->app->share_dir->catdir('static')
25 );
26}
27
28sub dispatch_request {
29 my ($self) = @_;
30 sub (/**.*) {
31 my $path = $_[1];
0ea3d9ae 32 return unless $path =~ s/(?=\/|^)-//;
2f1cc197 33 App::SCS::Web::redispatch_to("/static/${path}");
34 },
35 sub (/static/...) { $self->_static_handler },
36 sub (/favicon + .ico) { $self->_static_handler },
37 $self->_dir_handler
38}
39
40sub run_server {
41 my ($self, $env) = @_;
42 my @args = @{$env->{argv}};
43 my $r = use_module('Plack::Runner')->new(
44 server => 'Starman',
45 app => $self->app->web->to_psgi_app
46 );
47 $r->parse_options(@args);
48 $r->set_options(argv => \@args);
49 $r->run;
50}
51
521;