| 1 | package Yukki::Web; | ||
| 2 | { | ||
| 3 | $Yukki::Web::VERSION = '0.140290'; | ||
| 4 | } | ||
| 5 | 3 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | extends qw( Yukki ); | ||
| 8 | |||
| 9 | 3 | ▶ | use Class::Load; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 0 | ▶ | use Yukki::Error qw( http_throw http_exception ); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use Yukki::Types qw( PluginList ); | ||
| 13 | use Yukki::Web::Context; | ||
| 14 | use Yukki::Web::Router; | ||
| 15 | use Yukki::Web::Settings; | ||
| 16 | |||
| 17 | use CHI; | ||
| 18 | use LWP::MediaTypes qw( add_type ); | ||
| 19 | use Plack::Session::Store::Cache; | ||
| 20 | use Scalar::Util qw( blessed ); | ||
| 21 | use Try::Tiny; | ||
| 22 | |||
| 23 | # ABSTRACT: the Yukki web server | ||
| 24 | |||
| 25 | |||
| 26 | has '+settings' => ( isa => 'Yukki::Web::Settings' ); | ||
| 27 | |||
| 28 | |||
| 29 | has router => ( | ||
| 30 | is => 'ro', | ||
| 31 | isa => 'Path::Router', | ||
| 32 | required => 1, | ||
| 33 | lazy_build => 1, | ||
| 34 | ); | ||
| 35 | |||
| 36 | sub _build_router { | ||
| 37 | my $self = shift; | ||
| 38 | Yukki::Web::Router->new( app => $self ); | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | has plugins => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => PluginList, | ||
| 45 | required => 1, | ||
| 46 | lazy_build => 1, | ||
| 47 | traits => [ 'Array' ], | ||
| 48 | handles => { | ||
| 49 | all_plugins => 'elements', | ||
| 50 | format_helper_plugins => [ grep => sub { | ||
| 51 | $_->does('Yukki::Web::Plugin::Role::FormatHelper') | ||
| 52 | } ], | ||
| 53 | formatter_plugins => [ grep => sub { | ||
| 54 | $_->does('Yukki::Web::Plugin::Role::Formatter') | ||
| 55 | } ], | ||
| 56 | }, | ||
| 57 | ); | ||
| 58 | |||
| 59 | sub _build_plugins { | ||
| 60 | my $self = shift; | ||
| 61 | |||
| 62 | my @plugins; | ||
| 63 | for my $plugin_settings (@{ $self->settings->plugins }) { | ||
| 64 | my $module = $plugin_settings->{module}; | ||
| 65 | |||
| 66 | my $class = $module; | ||
| 67 | $class = "Yukki::Web::Plugin::$class" unless $class =~ s/^\+//; | ||
| 68 | |||
| 69 | Class::Load::load_class($class); | ||
| 70 | |||
| 71 | push @plugins, $class->new(%$plugin_settings, app => $self); | ||
| 72 | } | ||
| 73 | |||
| 74 | return \@plugins; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | sub BUILD { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | my $types = $self->settings->media_types; | ||
| 82 | while (my ($mime_type, $ext) = each %$types) { | ||
| 83 | my @ext = ref $ext ? @$ext : ($ext); | ||
| 84 | add_type($mime_type, @ext); | ||
| 85 | } | ||
| 86 | }; | ||
| 87 | |||
| 88 | |||
| 89 | sub component { | ||
| 90 | my ($self, $type, $name) = @_; | ||
| 91 | my $class_name = join '::', 'Yukki::Web', $type, $name; | ||
| 92 | Class::Load::load_class($class_name); | ||
| 93 | return $class_name->new(app => $self); | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub controller { | ||
| 98 | my ($self, $name) = @_; | ||
| 99 | return $self->component(Controller => $name); | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | sub view { | ||
| 104 | my ($self, $name) = @_; | ||
| 105 | return $self->component(View => $name); | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | sub dispatch { | ||
| 110 | my ($self, $env) = @_; | ||
| 111 | |||
| 112 | $env->{'yukki.app'} = $self; | ||
| 113 | $env->{'yukki.settings'} = $self->settings; | ||
| 114 | |||
| 115 | my $ctx = Yukki::Web::Context->new(env => $env); | ||
| 116 | my $response; | ||
| 117 | |||
| 118 | try { | ||
| 119 | my $match = $self->router->match($ctx->request->path); | ||
| 120 | |||
| 121 | http_throw('No action found matching that URL.', { | ||
| 122 | status => 'NotFound', | ||
| 123 | }) unless $match; | ||
| 124 | |||
| 125 | $ctx->request->path_parameters($match->mapping); | ||
| 126 | |||
| 127 | my $access_level_needed = $match->access_level; | ||
| 128 | http_throw('You are not authorized to run this action.', { | ||
| 129 | status => 'Forbidden', | ||
| 130 | }) unless $self->check_access( | ||
| 131 | user => $ctx->session->{user}, | ||
| 132 | repository => $match->mapping->{repository} // '-', | ||
| 133 | needs => $access_level_needed, | ||
| 134 | ); | ||
| 135 | |||
| 136 | if ($ctx->session->{user}) { | ||
| 137 | $ctx->response->add_navigation_item(user => { | ||
| 138 | label => 'Sign out', | ||
| 139 | href => 'logout', | ||
| 140 | sort => 100, | ||
| 141 | }); | ||
| 142 | } | ||
| 143 | |||
| 144 | else { | ||
| 145 | $ctx->response->add_navigation_item(user => { | ||
| 146 | label => 'Sign in', | ||
| 147 | href => 'login', | ||
| 148 | sort => 100, | ||
| 149 | }); | ||
| 150 | } | ||
| 151 | |||
| 152 | for my $repository (keys %{ $self->settings->repositories }) { | ||
| 153 | my $config = $self->settings->repositories->{$repository}; | ||
| 154 | |||
| 155 | my $name = $config->name; | ||
| 156 | $ctx->response->add_navigation_item(repository => { | ||
| 157 | label => $name, | ||
| 158 | href => join('/', 'page/view', $repository), | ||
| 159 | sort => $config->sort, | ||
| 160 | }); | ||
| 161 | } | ||
| 162 | |||
| 163 | my $controller = $match->target; | ||
| 164 | |||
| 165 | $controller->fire($ctx); | ||
| 166 | $response = $ctx->response->finalize; | ||
| 167 | } | ||
| 168 | |||
| 169 | catch { | ||
| 170 | |||
| 171 | if (blessed $_ and $_->isa('Moose::Object') and $_->does('HTTP::Throwable')) { | ||
| 172 | |||
| 173 | if ($_->does('HTTP::Throwable::Role::Status::Forbidden') | ||
| 174 | and not $ctx->session->{user}) { | ||
| 175 | |||
| 176 | $response = http_exception('Please login first.', { | ||
| 177 | status => 'Found', | ||
| 178 | location => ''.$ctx->rebase_url('login'), | ||
| 179 | })->as_psgi($env); | ||
| 180 | } | ||
| 181 | |||
| 182 | else { | ||
| 183 | $response = $_->as_psgi($env); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | else { | ||
| 188 | warn "ISE: $_"; | ||
| 189 | |||
| 190 | $response = http_exception($_, { | ||
| 191 | status => 'InternalServerError', | ||
| 192 | show_stack_trace => 0, | ||
| 193 | })->as_psgi($env); | ||
| 194 | } | ||
| 195 | }; | ||
| 196 | |||
| 197 | return $response; | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | sub session_middleware { | ||
| 202 | my $self = shift; | ||
| 203 | |||
| 204 | # TODO Make this configurable | ||
| 205 | return ('Session', | ||
| 206 | store => Plack::Session::Store::Cache->new( | ||
| 207 | cache => CHI->new(driver => 'FastMmap'), | ||
| 208 | ), | ||
| 209 | ); | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | sub munge_label { | ||
| 214 | my ($self, $link) = @_; | ||
| 215 | |||
| 216 | $link =~ m{([^/]+)$}; | ||
| 217 | |||
| 218 | $link =~ s{([a-zA-Z])'([a-zA-Z])}{$1$2}g; # foo's -> foos, isn't -> isnt | ||
| 219 | $link =~ s{[^a-zA-Z0-9-_./]+}{-}g; | ||
| 220 | $link =~ s{-+}{-}g; | ||
| 221 | $link =~ s{^-}{}; | ||
| 222 | $link =~ s{-$}{}; | ||
| 223 | |||
| 224 | $link .= '.yukki'; | ||
| 225 | |||
| 226 | return $link; | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | 1; | ||
| 231 | |||
| 232 | __END__ | ||