Commit | Line | Data |
99cee940 |
1 | package SCSite::DevMode; |
2 | |
3 | use Plack::App::File; |
4 | use Plack::Runner; |
9d4b4027 |
5 | use Try::Tiny; |
99cee940 |
6 | use Moo::Role; |
7 | |
8 | has _static_handler => (is => 'lazy'); |
9 | |
10 | sub _build__static_handler { |
11 | my ($self) = @_; |
12 | my $static_dir = $self->config->{static_dir}; |
7f2b58ea |
13 | Plack::App::File->new(root => $static_dir); |
99cee940 |
14 | } |
15 | |
16 | around dispatch_request => sub { |
17 | my ($orig, $self) = (shift, shift); |
18 | no warnings::illegalproto; |
19 | ( |
99295c1b |
20 | sub (/**.*) { |
21 | my ($self, $path) = @_; |
22 | return unless $path =~ s/\/-/\//; |
23 | SCSite::redispatch_to "/static/${path}"; |
24 | }, |
99cee940 |
25 | sub (/static/...) { $self->_static_handler }, |
f08b95b6 |
26 | sub (/favicon + .ico) { $self->_static_handler }, |
99cee940 |
27 | $self->$orig(@_) |
28 | ) |
29 | }; |
30 | |
31 | sub _run_dev_server { |
32 | my ($self, @args) = @_; |
33 | my $r = Plack::Runner->new(server => 'Starman', app => $self->to_psgi_app); |
34 | $r->parse_options(@args); |
35 | $r->set_options(argv => \@args); |
36 | $r->run; |
37 | } |
38 | |
9d4b4027 |
39 | sub _run_dev_checkall { |
40 | my ($self) = @_; |
41 | my $ps = $self->pages; |
42 | foreach my $path ($ps->all_paths) { |
43 | try { |
44 | $ps->get({ path => $path }); |
45 | print "OK ${path}\n"; |
46 | } catch { |
47 | print "ERROR ${path}\n"; |
48 | print " $_"; |
49 | }; |
50 | } |
51 | } |
52 | |
99cee940 |
53 | 1; |