blib/lib/Ukigumo/Server/Launcher.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 101 CC 6 SCAR 22.4
CC
6
Cov
0%
set_config
30.0
to_app
6.0
session_dir
2.0
CRAP 9.4
1 package Ukigumo::Server::Launcher;
2 use strict;
3 use warnings;
4 use feature ':5.10';
5  
6 use Carp ();
7 use File::Spec;
8 use File::Path qw(mkpath);
9 use Plack::Builder;
10 use Plack::Session::Store::File;
11  
12 use Ukigumo::Server;
13 use Ukigumo::Server::API;
14 use Ukigumo::Server::Web;
15  
16 sub session_dir {
17 state $session_dir = do {
18 my $env = $ENV{PLACK_ENV} // 'cli';
19 my $session_dir = File::Spec->catdir(File::Spec->tmpdir(), "ukigumo-session-$env");
20 mkpath($session_dir);
21 $session_dir;
22 };
23 }
24  
25 sub setup {
26 my $app = Ukigumo::Server->new();
27 $app->setup_schema();
28 }
29  
30 sub set_config {
31 my ($class, $file) = @_;
32  
33 my $config;
34 if (ref $file && ref $file eq 'HASH') {
35 $config = $file;
36 }
37 else {
38 $file ||= File::Spec->catfile(Ukigumo::Server->share_dir, qw/config development.pl/);
39  
40 $config = do $file;
41 Carp::croak("$file: $@") if $@;
42 Carp::croak("$file: $!") unless defined $config;
43 unless ( ref($config) eq 'HASH' ) {
44 Carp::croak("$file does not return HashRef.");
45 }
46 }
47 Ukigumo::Server->config($config);
48 }
49  
50 sub to_app {
51 my $class = shift;
52 builder {
53 enable 'Plack::Middleware::ReverseProxy';
54  
55 my $api = Ukigumo::Server::API->to_app();
56 my $ui = builder {
57 enable 'Plack::Middleware::Static',
58 path => qr{^(?:/static/|/robot\.txt$|/favicon.ico$)},
59 root => File::Spec->catdir( Ukigumo::Server->share_dir );
60 enable 'Plack::Middleware::Session',
61 store => Plack::Session::Store::File->new(
62 dir => $class->session_dir,
63 );
64 Ukigumo::Server::Web->to_app();
65 };
66  
67 sub {
68 my $env = shift;
69 if ($env->{PATH_INFO} =~ m{^/api/}) {
70 $api->($env);
71 } else {
72 $ui->($env);
73 }
74 };
75 };
76 }
77  
78 1;