| 1 | package Slick; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.036; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Types::Standard qw(HashRef ArrayRef Int Str); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Module::Runtime qw(require_module); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use URI::Query; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Plack::Builder qw(builder enable); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Slick::Context; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Slick::Events qw(EVENTS BEFORE_DISPATCH AFTER_DISPATCH); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Slick::Error; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Slick::Database; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Slick::Cache; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Slick::Methods qw(METHODS); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Slick::Route; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use Slick::Router; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Slick::RouteMap; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Slick::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use experimental qw(try); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | no warnings qw(experimental::try); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | our $VERSION = '0.007'; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | with 'Slick::EventHandler'; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | with 'Slick::RouteManager'; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | has port => ( | ||||||||||||||||||||||||||||||||||||||||||
| 32 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 33 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | default => sub { return 8000; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | has addr => ( | ||||||||||||||||||||||||||||||||||||||||||
| 39 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 41 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | default => sub { return '127.0.0.1'; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | has timeout => ( | ||||||||||||||||||||||||||||||||||||||||||
| 46 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 47 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | default => sub { return 120; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | has env => ( | ||||||||||||||||||||||||||||||||||||||||||
| 53 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 54 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | return $ENV{SLICK_ENV} // $ENV{PLACK_ENV} // 'dev'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
($ENV{"SLICK_ENV"} // $ENV{"PLACK_ENV"}) // "dev"
$ENV{"SLICK_ENV"} // $ENV{"PLACK_ENV"}
MC/DC0%0/2
($ENV{"SLICK_ENV"} // $ENV{"PLACK_ENV"}) // "dev"
$ENV{"SLICK_ENV"}
$ENV{"PLACK_ENV"}
Truth table33%1/3
A // B
A= $ENV{"SLICK_ENV"}B= $ENV{"PLACK_ENV"}
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | has server => ( | |||||||||||||||||||||||||||||||||||||||||
| 61 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 62 | lazy => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | has dbs => ( | ||||||||||||||||||||||||||||||||||||||||||
| 66 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 67 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | isa => HashRef, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | default => sub { return {}; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | has caches => ( | ||||||||||||||||||||||||||||||||||||||||||
| 73 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 74 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | isa => HashRef, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | default => sub { return {}; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | has helpers => ( | ||||||||||||||||||||||||||||||||||||||||||
| 80 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 81 | isa => HashRef, | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | default => sub { return {}; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | has middlewares => ( | ||||||||||||||||||||||||||||||||||||||||||
| 86 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 87 | isa => ArrayRef, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | default => sub { return []; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | has banner => ( | ||||||||||||||||||||||||||||||||||||||||||
| 92 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | return <<'EOB'; | |||||||||||||||||||||||||||||||||||||||||
| 95 | _____ ___ __ | ||||||||||||||||||||||||||||||||||||||||||
| 96 | / ___// (_)____/ /__ a'! _,,_ | ||||||||||||||||||||||||||||||||||||||||||
| 97 | \__ \/ / / ___/ //_/ \\_/ \ | ||||||||||||||||||||||||||||||||||||||||||
| 98 | ___/ / / / /__/ ,< \, /-( /'-, | ||||||||||||||||||||||||||||||||||||||||||
| 99 | /____/_/_/\___/_/|_| //\ //\\ | ||||||||||||||||||||||||||||||||||||||||||
| 100 | EOB | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 12 | ▶ | sub _dispatch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dispatch: called CC=10 SCAR=26.0
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 12 | my $request = Plack::Request->new(shift); | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 12 | my $context = Slick::Context->new( request => $request, ); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 12 | my $method = lc( $request->method ); | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 12 | for ( @{ $self->event_handlers->{ BEFORE_DISPATCH() } } ) { | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | try { $_->( $self, $context ) // goto DONE; } | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
&$_($self, $context) // (goto DONE)
MC/DC0%0/1
&$_($self, $context) // (goto DONE)
&$_($self, $context)
Truth table0%0/2
A
A= &$_($self, $context)
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | catch ($e) { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | push $context->stash->{'slick.errors'}->@*, Slick::Error->new($e) | |||||||||||||||||||||||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 12 | my $route = | |||||||||||||||||||||||||||||||||||||||||
| 119 | $self->handlers->get( $context->request->request_uri, $method, $context ); | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 12 | ▶ | if ( defined $route ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 11 | try { $route->dispatch( $self, $context ); } | |||||||||||||||||||||||||||||||||||||||||
| 123 | catch ($e) { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | push $context->stash->{'slick.errors'}->@*, Slick::Error->new($e) | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | $context->status(405); | |||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | $context->body('405 Method Not Supported'); | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 12 | for ( @{ $self->event_handlers->{ AFTER_DISPATCH() } } ) { | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | try { $_->( $self, $context ) // goto DONE; } | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
&$_($self, $context) // (goto DONE)
MC/DC0%0/1
&$_($self, $context) // (goto DONE)
&$_($self, $context)
Truth table0%0/2
A
A= &$_($self, $context)
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | catch ($e) { | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | push $context->stash->{'slick.errors'}->@*, Slick::Error->new($e); | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | DONE: | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 12 | ▶ | if ( $context->stash->{'slick.errors'}->@* && ( $self->env eq 'dev' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@{$context->stash->{"slick.errors"}} and $self->env eq "dev"
MC/DC50%1/2
@{$context->stash->{"slick.errors"}} and $self->env eq "dev"
@{$context->stash->{"slick.errors"}}
$self->env eq "dev"
Truth table66%2/3
A and B
A= @{$context->stash->{"slick.errors"}}B= $self->env eq "dev"
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | say $_->error for $context->stash->{'slick.errors'}->@*; | |||||||||||||||||||||||||||||||||||||||||
| 143 | $context->json( | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | [ map { $_->to_hash } $context->stash->{'slick.errors'}->@* ] ); | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | # HEAD requests only want headers. | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 12 | ▶ | $context->body = [] if $method eq 'head'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 12 | return $context->to_psgi; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub helper { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
helper: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my ( $self, $name, $helper ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | if ( exists $self->helpers->{$name} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | return $self->helpers->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | return $self->helpers->{$name} = $helper; | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | sub middleware { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
middleware: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | push $self->middlewares->@*, [@args]; | |||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | # Add a database or access an existing database: | ||||||||||||||||||||||||||||||||||||||||||
| 172 | # | ||||||||||||||||||||||||||||||||||||||||||
| 173 | # $slick->database('foo'); # Get's an existing database labeled 'foo' | ||||||||||||||||||||||||||||||||||||||||||
| 174 | # | ||||||||||||||||||||||||||||||||||||||||||
| 175 | # $slick->database(foo => 'postgresql://foo@bar:5432/mydb'); # Attempts to create a database labeled foo | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub database { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
database: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my ( $self, $name, $conn ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if ( defined $conn ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | return $self->dbs->{$name} = Slick::Database->new( conn => $conn ); | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | return $self->dbs->{$name} // undef; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->dbs->{$name} // undef
MC/DC0%0/1
$self->dbs->{$name} // undef
$self->dbs->{$name}
Truth table0%0/2
A
A= $self->dbs->{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub cache { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cache: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | my ( $self, $name, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | if (@args) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | return $self->caches->{$name} = Slick::Cache->new(@args); | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | return $self->caches->{$name} // undef; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->caches->{$name} // undef
MC/DC0%0/1
$self->caches->{$name} // undef
$self->caches->{$name}
Truth table0%0/2
A
A= $self->caches->{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | # Runs the application with the server | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | my $server = $args{server} // 'HTTP::Server::PSGI'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"server"} // "HTTP::Server::PSGI"
MC/DC0%0/1
$args{"server"} // "HTTP::Server::PSGI"
$args{"server"}
Truth table0%0/2
A
A= $args{"server"}
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | $self->{port} = $args{port} // 8000; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"port"} // 8000
MC/DC0%0/1
$args{"port"} // 8000
$args{"port"}
Truth table0%0/2
A
A= $args{"port"}
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | $self->{addr} = $args{addr} // '127.0.0.1'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"addr"} // "127.0.0.1"
MC/DC0%0/1
$args{"addr"} // "127.0.0.1"
$args{"addr"}
Truth table0%0/2
A
A= $args{"addr"}
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | require_module $server; | |||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | if ( $self->env eq 'dev' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$self->env eq "dev" or $self->env ne "dev" and not $self->server
$self->env ne "dev" and not $self->server
MC/DC0%0/3
$self->env eq "dev" or $self->env ne "dev" and not $self->server
$self->env eq "dev"
$self->env ne "dev"
not $self->server
Truth table0%0/4
A or B and not(C)
A= $self->env eq "dev"B= $self->env ne "dev"C= not $self->server
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | || ( $self->env ne 'dev' && !$self->server ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 208 | { | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | $self->{server} = $server->new( | |||||||||||||||||||||||||||||||||||||||||
| 210 | host => $self->addr, | ||||||||||||||||||||||||||||||||||||||||||
| 211 | port => $self->port, | ||||||||||||||||||||||||||||||||||||||||||
| 212 | timeout => $self->timeout, | ||||||||||||||||||||||||||||||||||||||||||
| 213 | server_software => "Slick v$VERSION + $server" | ||||||||||||||||||||||||||||||||||||||||||
| 214 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | say "\n" . $self->banner; | |||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | my ( $addr, $port ) = ( $self->addr, $self->port ); | |||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | say "Slick is listening on: http://$addr:$port\n"; | |||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | return $self->server->run( $self->app ); | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | 1 | ▶ | sub register { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 1 | my $router = shift; | |||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 1 | ▶ | croak qq{Router cannot be undef.} unless $router; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | 1 | $self->handlers->merge( $router->handlers, $router->event_handlers ); | |||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | # This is for users who want to use plackup | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | sub app { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
app: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | return builder { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | enable 'Plack::Middleware::AccessLog' => format => 'combined'; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | enable $_->@* for $self->middlewares->@*; | |||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | return $self->_dispatch(@_); | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | Slick | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | Slick is an Object-Oriented Perl web-framework for building performant, and easy to refactor REST API's. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | Slick is built on top of L<DBI>, L<Plack>, and L<Moo> and fits somewhere in-between the realms of L<Dancer> and L<Mojolicious>. | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | Slick has everything you need to build a Database driven REST API, including built in support | ||||||||||||||||||||||||||||||||||||||||||
| 263 | for Database connections, Migrations, Event logic, and soon, route based Caching via Redis or Memcached. Since Slick is a Plack application, | ||||||||||||||||||||||||||||||||||||||||||
| 264 | you can also take advantage of swappable backends and Plack middlewares extremely simply. | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | Currently, Slick supports MySQL and PostgreSQL but there are plans to implement Oracle and MS SQL Server as well. | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | =head2 Examples | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | =head3 A Simple App | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | This is a simple example app that takes advantage of 2 databases, | ||||||||||||||||||||||||||||||||||||||||||
| 273 | has a migration, and also serves some JSON. | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | use 5.036; | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | use Slick; | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | my $s = Slick->new; | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | # Both MySQL and Postgres are supported databases | ||||||||||||||||||||||||||||||||||||||||||
| 282 | # Slick will create the correct DB object based on the connection URI | ||||||||||||||||||||||||||||||||||||||||||
| 283 | # [{mysql,postgres,postgresql}://][user[:[password]]@]host[:port][/schema] | ||||||||||||||||||||||||||||||||||||||||||
| 284 | $s->database(my_db => 'postgresql://user:password@127.0.0.1:5432/schema'); | ||||||||||||||||||||||||||||||||||||||||||
| 285 | $s->database(corporate_db => 'mysql://corporate:secure_password@127.0.0.1:3306/schema'); | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | $s->database('my_db')->migration( | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 'create_user_table', # id | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 'CREATE TABLE user ( id SERIAL PRIMARY KEY AUTOINCREMENT, name TEXT, age INT );', #up | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 'DROP TABLE user;' # down | ||||||||||||||||||||||||||||||||||||||||||
| 291 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | $s->database('my_db')->migrate_up; # Migrates all pending migrations | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | $s->get('/users/{id}' => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 296 | my $app = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 297 | my $context = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | # Queries follow SQL::Abstract's notations | ||||||||||||||||||||||||||||||||||||||||||
| 300 | my $user = $app->database('my_db')->select_one('user', { id => $context->params('id') }); | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | # Render the user hashref as JSON. | ||||||||||||||||||||||||||||||||||||||||||
| 303 | $context->json($user); | ||||||||||||||||||||||||||||||||||||||||||
| 304 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | $s->post('/users' => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 307 | my $app = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 308 | my $context = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | my $new_user = $context->content; # Will be decoded from JSON, YAML, or URL encoded (See JSON::Tiny, YAML::Tiny, and URL::Encode) | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | $app->database('my_db')->insert('user', $new_user); | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | $context->json($new_user); | ||||||||||||||||||||||||||||||||||||||||||
| 315 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | $s->run; # Run the application. | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | =head3 A Multi-file application | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | This is a multi-file application that uses L<Slick::Router>. | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | ### INSIDE OF YOUR ROUTER MODULE lib/MyApp/ItemRouter.pm | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | package MyApp::ItemRouter; | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | use base qw(Slick::Router); | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | my $router = __PACKAGE__->new(base => '/items'); | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | $router->get('/{id}' => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 332 | my ($app, $context) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 333 | my $item = $app->database('items')->select_one({ id => $context->param('id') }); | ||||||||||||||||||||||||||||||||||||||||||
| 334 | $context->json($item); | ||||||||||||||||||||||||||||||||||||||||||
| 335 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | $router->post('' => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 338 | my ($app, $context) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 339 | my $new_item = $context->content; | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | # Do some sort of validation | ||||||||||||||||||||||||||||||||||||||||||
| 342 | if (not $app->helper('item_validator')->($new_item)) { | ||||||||||||||||||||||||||||||||||||||||||
| 343 | $context->status(400)->json({ error => 'Bad Request' }); | ||||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||
| 345 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 346 | $app->database('items')->insert('items', $new_item); | ||||||||||||||||||||||||||||||||||||||||||
| 347 | $context->json($new_item); | ||||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | sub router { | ||||||||||||||||||||||||||||||||||||||||||
| 352 | return $router; | ||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | ### INSIDE OF YOUR RUN SCRIPT 'app.pl' | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | use 5.036; | ||||||||||||||||||||||||||||||||||||||||||
| 360 | use lib 'lib'; | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | use Slick; | ||||||||||||||||||||||||||||||||||||||||||
| 363 | use MyApp::ItemRouter; | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | my $slick = Slick->new; | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | $slick->helper(item_validator => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 368 | return exists shift->{name}; | ||||||||||||||||||||||||||||||||||||||||||
| 369 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | # Register as many routers as you want! | ||||||||||||||||||||||||||||||||||||||||||
| 372 | $slick->register(MyApp::ItemRouter->router); | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | $slick->run; | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | =head3 Running with plackup | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | If you wish to use C<plackup> you can change the final call to C<run> to a call to C<app>: | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | $s->app; | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | Then simply run with plackup (substitue `my_app.psgi` with whatever your app is called): | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | plackup -a my_app.psgi | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | =head3 Changing PSGI backend | ||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | Will run on the default L<HTTP::Server::PSGI>. | ||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | $s->run; | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | Or, | ||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | In this example, running Slick with a L<Gazelle> backend on port C<8888> and address C<0.0.0.0>. | ||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||
| 396 | $s->run(server => 'Gazelle', port => 8888, addr => '0.0.0.0'); | ||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | =head3 Using Plack Middlewares | ||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | You can register more Plack middlewares with your application using the L<"middleware"> method. | ||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | my $s = Slick->new; | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | $s->middleware('Deflater') | ||||||||||||||||||||||||||||||||||||||||||
| 405 | ->middleware('Session' => store => 'file') | ||||||||||||||||||||||||||||||||||||||||||
| 406 | ->middleware('Debug', panels => [ qw(DBITrace Memory) ]); | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | $s->run; # or $s->app depending on if you want to use plackup. | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | =head1 API | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | =head2 app | ||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | $s->app; | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | Converts the L<Slick> application to a PSGI runnable app. | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | =head2 register | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | my $router = Slick::Router->new(base => '/foo'); | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | $s->register($router); | ||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | Registers a L<Slick::Router> to the application. This includes calling C<merge> on the app's L<Slick::RouteMap>. | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | =head2 banner | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | $s->banner; | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | Returns the L<Slick> banner. | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | You can overwrite the banner with something else if you like via: | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | $s->{banner} = 'My Cool Banner!'; | ||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | =head2 database | ||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | $s->database(my_db => 'postgresql://foo:bar@127.0.0.1:5432/database'); | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | Creates and registers a database to the L<Slick> instance. The connection string should | ||||||||||||||||||||||||||||||||||||||||||
| 441 | be a fully-qualified URI based DSN. | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | $s->database('my_db'); | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | Retrieves the database if it exists, otherwise returns C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | =head2 cache | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | # See Redis and Cache::Memcached on CPAN for arguments | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | # Create a Redis instance | ||||||||||||||||||||||||||||||||||||||||||
| 452 | $s->cache( | ||||||||||||||||||||||||||||||||||||||||||
| 453 | my_redis => type => 'redis', # Slick Arguments | ||||||||||||||||||||||||||||||||||||||||||
| 454 | server => '127.0.0.1:6379' # Redis arguments | ||||||||||||||||||||||||||||||||||||||||||
| 455 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | # Create a Memcached instance | ||||||||||||||||||||||||||||||||||||||||||
| 458 | $s->cache( | ||||||||||||||||||||||||||||||||||||||||||
| 459 | my_memcached => type => 'memcached', # Slick Arguments | ||||||||||||||||||||||||||||||||||||||||||
| 460 | servers => ['127.0.0.1'] => debug => 1 # Cache::Memcached arguments | ||||||||||||||||||||||||||||||||||||||||||
| 461 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | Retrieves the cache if it exists, otherwise returns C<undef>. Basically L<"database"> but for caches. | ||||||||||||||||||||||||||||||||||||||||||
| 464 | Note type is a required argument, it should either be C<'redis'> or C<'memcached'>. | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | =head2 helper | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | $s->helper(printer => sub { print "Hi!"; }); | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | Registers a Perl object with the L<Slick> instance. | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | $s->helper('printer')->(); | ||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | Retrieves the helper from the L<Slick> instance if it exists, otherwise returns C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | =head2 middleware | ||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | $s->middleware('Deflater'); | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | Registers a L<Plack::Middleware> with the L<Slick> instance. Always returns the L<Slick> instance | ||||||||||||||||||||||||||||||||||||||||||
| 481 | so you can chain many middlewares in one sitting. | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | $s->middleware('Deflater') | ||||||||||||||||||||||||||||||||||||||||||
| 484 | ->middleware('Session' => store => 'file') | ||||||||||||||||||||||||||||||||||||||||||
| 485 | ->middleware('Debug', panels => [ qw(DBITrace Memory) ]); | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | =head2 run | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | $s->run; | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | Runs the L<Slick> application on port C<8000> and address C<127.0.0.1> atop L<HTTP::Server::PSGI> by default. | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | You can change this via parameters passed to the C<run> method: | ||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | $s->run( | ||||||||||||||||||||||||||||||||||||||||||
| 496 | server => 'Gazelle', | ||||||||||||||||||||||||||||||||||||||||||
| 497 | port => 9999, | ||||||||||||||||||||||||||||||||||||||||||
| 498 | addr => '0.0.0.0' | ||||||||||||||||||||||||||||||||||||||||||
| 499 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | =head2 get, post, put, patch, delete | ||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | Methods to handle requests to your server. Takes a location, and a C<CodeRef> that expects | ||||||||||||||||||||||||||||||||||||||||||
| 504 | two arguments, app (L<Slick>) and context (L<Slick::Context>). | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | $s->get('/foo', sub { | ||||||||||||||||||||||||||||||||||||||||||
| 507 | my ($app, $context) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 508 | # ... do stuff | ||||||||||||||||||||||||||||||||||||||||||
| 509 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | You can also have path parameters that are replaced with the values in the URI: | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | $s->get('/foo/{bar}', sub { | ||||||||||||||||||||||||||||||||||||||||||
| 514 | my ($app, $context) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 515 | $context->params('bar'); # Resolves to the parameters value. | ||||||||||||||||||||||||||||||||||||||||||
| 516 | # ... do stuff | ||||||||||||||||||||||||||||||||||||||||||
| 517 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | You may also register local events for the route like so: | ||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | $s->get('/foo/{bar}', sub { | ||||||||||||||||||||||||||||||||||||||||||
| 522 | my ($app, $context) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 523 | $context->params('bar'); # Resolves to the parameters value. | ||||||||||||||||||||||||||||||||||||||||||
| 524 | # ... do stuff | ||||||||||||||||||||||||||||||||||||||||||
| 525 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 526 | { before_dispatch => [ | ||||||||||||||||||||||||||||||||||||||||||
| 527 | sub { | ||||||||||||||||||||||||||||||||||||||||||
| 528 | my ($app, $context) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 529 | # Do some before dispatch logic | ||||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 532 | after_dispatch => [ | ||||||||||||||||||||||||||||||||||||||||||
| 533 | sub { | ||||||||||||||||||||||||||||||||||||||||||
| 534 | my ($app, $context) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 535 | # Do some after dispatch logic | ||||||||||||||||||||||||||||||||||||||||||
| 536 | return 1; | ||||||||||||||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||||||||||||
| 538 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | See L<Slick::Events> for a list of all of the events. | ||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||
| 542 | See L<Slick::Route> C<on> method for more information about route event handling. | ||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | See L<Slick::Context> for more ways to handle routing and HTTP lifecycles. | ||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | =head2 on | ||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | $s->on(before_dispatch => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 549 | my ($app, $context) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 550 | # do stuff before routing logic has been handled | ||||||||||||||||||||||||||||||||||||||||||
| 551 | return 1; | ||||||||||||||||||||||||||||||||||||||||||
| 552 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||
| 554 | $s->on(after_dispatch => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 555 | my ($app, $context) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 556 | # do stuff after routing logic has been handled | ||||||||||||||||||||||||||||||||||||||||||
| 557 | return 1; | ||||||||||||||||||||||||||||||||||||||||||
| 558 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||
| 560 | Registers an event listener globally across the entire application. If a registered event handler | ||||||||||||||||||||||||||||||||||||||||||
| 561 | returns a C<falsy> value, the route chain will stop and the current context will be returned as the response. | ||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||
| 563 | See L<Slick::Events> for a list of available events. | ||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | Inherited from L<Slick::EventHandler>. | ||||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||
| 567 | =head1 See also | ||||||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||||||
| 569 | =over 2 | ||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | =item * L<Slick::Router> | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | =item * L<Slick::Context> | ||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | =item * L<Slick::Database> | ||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | =item * L<Slick::DatabaseExecutor> | ||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||
| 579 | =item * L<Slick::DatabaseExecutor::MySQL> | ||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | =item * L<Slick::DatabaseExecutor::Pg> | ||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||
| 583 | =item * L<Slick::EventHandler> | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | =item * L<Slick::Events> | ||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||
| 587 | =item * L<Slick::Methods> | ||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||
| 589 | =item * L<Slick::RouteMap> | ||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||
| 591 | =item * L<Slick::Util> | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | =item * L<Slick::Error> | ||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||||||
| 597 | =cut | ||||||||||||||||||||||||||||||||||||||||||