first cut at generation to file code
[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');
20
21 sub _build__static_handler {
22   my ($self) = @_;
23   use_module('Plack::App::File')->new(
24     root => $self->app->share_dir->catdir('static')
25   );
26 }
27
28 sub dispatch_request {
29   my ($self) = @_;
30   sub (/**.*) {
31     my $path = $_[1];
32     return unless $path =~ s/\/-/\//;
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
40 sub 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
52 1;