89d69d910e40f91dcc5eb011f9645ebc7abbfe48
[scpubgit/SCS.git] / lib / SCSite / DevMode.pm
1 package SCSite::DevMode;
2
3 use Plack::App::File;
4 use Plack::Runner;
5 use Moo::Role;
6
7 has _static_handler => (is => 'lazy');
8
9 sub _build__static_handler {
10   my ($self) = @_;
11   my $static_dir = $self->config->{static_dir};
12   Plack::App::File->new(root => $static_dir)->to_app;
13 }
14
15 around dispatch_request => sub {
16   my ($orig, $self) = (shift, shift);
17   no warnings::illegalproto;
18   (
19     sub (/static/...) { $self->_static_handler },
20     sub (/favicon + .ico) { $self->_static_handler },
21     $self->$orig(@_)
22   )
23 };
24
25 sub _run_dev_server {
26   my ($self, @args) = @_;
27   my $r = Plack::Runner->new(server => 'Starman', app => $self->to_psgi_app);
28   $r->parse_options(@args);
29   $r->set_options(argv => \@args);
30   $r->run;
31 }
32
33 1;