package SCSite::DevMode;

use Plack::App::File;
use Plack::Runner;
use Moo::Role;

has _static_handler => (is => 'lazy');

sub _build__static_handler {
  my ($self) = @_;
  my $static_dir = $self->config->{static_dir};
  Plack::App::File->new(root => $static_dir);
}

around dispatch_request => sub {
  my ($orig, $self) = (shift, shift);
  no warnings::illegalproto;
  (
    sub (/static/...) { $self->_static_handler },
    sub (/favicon + .ico) { $self->_static_handler },
    $self->$orig(@_)
  )
};

sub _run_dev_server {
  my ($self, @args) = @_;
  my $r = Plack::Runner->new(server => 'Starman', app => $self->to_psgi_app);
  $r->parse_options(@args);
  $r->set_options(argv => \@args);
  $r->run;
}

1;
