« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/Sentry.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/Devel/StackTrace.pm »
| 1 | package Dwarf; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use Dwarf::Error; | ||
| 4 | use Dwarf::Message; | ||
| 5 | use Dwarf::Request; | ||
| 6 | use Dwarf::Response; | ||
| 7 | use Dwarf::Trigger; | ||
| 8 | use Dwarf::Util qw/capitalize read_file filename installed load_class dwarf_log/; | ||
| 9 | use Cwd 'abs_path'; | ||
| 10 | use Data::Dumper; | ||
| 11 | use File::Basename 'dirname'; | ||
| 12 | use File::Spec::Functions 'catfile'; | ||
| 13 | use Module::Find; | ||
| 14 | use Router::Simple; | ||
| 15 | use Scalar::Util qw/weaken/; | ||
| 16 | |||
| 17 | our $VERSION = '1.83'; | ||
| 18 | |||
| 19 | use constant { | ||
| 20 | BEFORE_DISPATCH => 'before_dispatch', | ||
| 21 | DISPATCHING => 'dispatching', | ||
| 22 | AFTER_DISPATCH => 'after_dispatch', | ||
| 23 | FINISH_DISPATCHING => 'Dwarf Finish Dispatching Message', | ||
| 24 | ERROR => 'error', | ||
| 25 | NOT_FOUND => 'not_found', | ||
| 26 | SERVER_ERROR => 'server_error', | ||
| 27 | }; | ||
| 28 | |||
| 29 | use Dwarf::Accessor { | ||
| 30 | ro => [qw/namespace base_dir env config error request response router route handler handler_class ext models state/], | ||
| 31 | rw => [qw/stash request_handler_prefix request_handler_method/], | ||
| 32 | }; | ||
| 33 | |||
| 34 | sub _build_config { | ||
| 35 | my $self = shift; | ||
| 36 | $self->{config} ||= do { | ||
| 37 | my $class = join '::', $self->namespace, 'Config'; | ||
| 38 | $class .= '::' . ucfirst $self->config_name if $self->can('config_name'); | ||
| 39 | load_class($class); | ||
| 40 | my $config = $class->new(context => $self); | ||
| 41 | weaken($config->{context}); | ||
| 42 | $config; | ||
| 43 | }; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _build_error { | ||
| 47 | my $self = shift; | ||
| 48 | $self->{error} ||= Dwarf::Error->new; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _build_request { | ||
| 52 | my $self = shift; | ||
| 53 | $self->{request} ||= do { | ||
| 54 | my $req = Dwarf::Request->new($self->env); | ||
| 55 | |||
| 56 | if (defined $req->param('debug')) { | ||
| 57 | require CGI::Carp; | ||
| 58 | CGI::Carp->import('fatalsToBrowser'); | ||
| 59 | } | ||
| 60 | |||
| 61 | $req; | ||
| 62 | }; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _build_response { | ||
| 66 | my $self = shift; | ||
| 67 | $self->{response} ||= do { | ||
| 68 | my $res = Dwarf::Response->new(200); | ||
| 69 | $res->content_type('text/plain'); | ||
| 70 | $res; | ||
| 71 | }; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _build_router { Router::Simple->new } | ||
| 75 | |||
| 76 | sub new { | ||
| 77 | my $invocant = shift; | ||
| 78 | my $class = ref $invocant || $invocant; | ||
| 79 | my $self = bless { @_ }, $class; | ||
| 80 | dwarf_log 'new Dwarf'; | ||
| 81 | $self->init; | ||
| 82 | return $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub DESTROY { | ||
| 86 | my $self = shift; | ||
| 87 | dwarf_log 'DESTROY Dwarf'; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub init { | ||
| 91 | my $self = shift; | ||
| 92 | |||
| 93 | $self->{env} ||= {}; | ||
| 94 | $self->{namespace} ||= ref $self; | ||
| 95 | $self->{base_dir} ||= abs_path(catfile(dirname(filename($self)), '..')); | ||
| 96 | $self->{models} ||= {}; | ||
| 97 | $self->{state} ||= BEFORE_DISPATCH; | ||
| 98 | $self->{stash} ||= {}; | ||
| 99 | $self->{request_handler_prefix} ||= join '::', $self->namespace, 'Controller'; | ||
| 100 | $self->{request_handler_method} ||= 'any'; | ||
| 101 | |||
| 102 | $self->setup; | ||
| 103 | $self->add_routes; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub add_routes { | ||
| 107 | my $self = shift; | ||
| 108 | $self->router->connect("/dwarf/test/api/*", { controller => "Dwarf::Test::Controller::Api" }); | ||
| 109 | $self->router->connect("/api/*", { controller => "Api" }); | ||
| 110 | $self->router->connect("/cli/*", { controller => "Cli" }); | ||
| 111 | $self->router->connect("*", { controller => "Web" }); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub setup {} | ||
| 115 | |||
| 116 | sub is_production { 1 } | ||
| 117 | |||
| 118 | sub is_cli { | ||
| 119 | my $self = shift; | ||
| 120 | my $server_software = $self->env->{SERVER_SOFTWARE} || ''; | ||
| 121 | return $server_software eq 'Plack::Handler::CLI'; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub param { shift->request->param(@_) } | ||
| 125 | sub req { shift->request(@_) } | ||
| 126 | sub res { shift->response(@_) } | ||
| 127 | sub status { shift->res->status(@_) } | ||
| 128 | sub type { shift->res->content_type(@_) } | ||
| 129 | sub header { shift->res->header(@_) } | ||
| 130 | sub headers { shift->res->headers(@_) } | ||
| 131 | sub body { shift->res->body(@_) } | ||
| 132 | |||
| 133 | sub method { | ||
| 134 | my $self = shift; | ||
| 135 | return uc($self->param('_method') || $self->request->method(@_)) | ||
| 136 | } | ||
| 137 | |||
| 138 | sub conf { | ||
| 139 | my $self = shift; | ||
| 140 | return $self->config->get(@_) if @_ == 1; | ||
| 141 | return $self->config->set(@_); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub dump { | ||
| 145 | my $self = shift; | ||
| 146 | local $Data::Dumper::Indent = 1; | ||
| 147 | local $Data::Dumper::Terse = 1; | ||
| 148 | Data::Dumper->Dump([@_]); | ||
| 149 | } | ||
| 150 | |||
| 151 | sub to_psgi { | ||
| 152 | my $self = shift; | ||
| 153 | $self->{route} = $self->find_route; | ||
| 154 | $self->call_before_trigger; | ||
| 155 | $self->dispatch(@_); | ||
| 156 | $self->call_after_trigger; | ||
| 157 | return $self->finalize; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub dispatch { | ||
| 161 | my $self = shift; | ||
| 162 | |||
| 163 | dwarf_log 'dispatch Dwarf'; | ||
| 164 | |||
| 165 | eval { | ||
| 166 | eval { | ||
| 167 | my $controller = $self->route->{controller}; | ||
| 168 | |||
| 169 | return $self->handle_not_found unless $controller; | ||
| 170 | Dwarf::Util::load_class($controller); | ||
| 171 | |||
| 172 | $self->{handler_class} = $controller; | ||
| 173 | $self->{handler} = $controller->new(context => $self); | ||
| 174 | weaken($self->{handler}->{context}); | ||
| 175 | |||
| 176 | my $method = $self->find_method; | ||
| 177 | return $self->not_found unless $method; | ||
| 178 | |||
| 179 | $self->handler->init($self); | ||
| 180 | |||
| 181 | my $body = $self->handler->$method($self, @_); | ||
| 182 | $self->body($body); | ||
| 183 | |||
| 184 | $self->handler->did_dispatch($self, $body); | ||
| 185 | }; | ||
| 186 | if ($@) { | ||
| 187 | my $error = $@; | ||
| 188 | $@ = undef; | ||
| 189 | |||
| 190 | if ($error =~ /Can't locate .+\.pm in/) { | ||
| 191 | print STDERR $error . "\n"; | ||
| 192 | return $self->not_found; | ||
| 193 | } | ||
| 194 | |||
| 195 | if (ref $error eq 'Dwarf::Error') { | ||
| 196 | return $self->handle_error($error); | ||
| 197 | } | ||
| 198 | |||
| 199 | die $error; | ||
| 200 | } | ||
| 201 | }; | ||
| 202 | if ($@) { | ||
| 203 | my $error = $@; | ||
| 204 | $@ = undef; | ||
| 205 | |||
| 206 | if (ref $error eq 'Dwarf::Message') { | ||
| 207 | if ($error->name eq FINISH_DISPATCHING) { | ||
| 208 | return $self->body($error->data); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | return $self->handle_server_error($error); | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | sub finalize { | ||
| 217 | my $self = shift; | ||
| 218 | |||
| 219 | dwarf_log 'finalize Dwarf'; | ||
| 220 | |||
| 221 | if ($self->can('disconnect_db')) { | ||
| 222 | $self->disconnect_db; | ||
| 223 | } | ||
| 224 | |||
| 225 | my $res = ref $self->body eq 'CODE' | ||
| 226 | ? $self->body # ストリーミング | ||
| 227 | : $self->response->finalize; | ||
| 228 | |||
| 229 | return $res; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub finish { | ||
| 233 | my ($self, $body) = @_; | ||
| 234 | $body //= ''; | ||
| 235 | my $message = Dwarf::Message->new( | ||
| 236 | name => FINISH_DISPATCHING, | ||
| 237 | data => $body, | ||
| 238 | ); | ||
| 239 | die $message; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub redirect { | ||
| 243 | my ($self, $to, $code) = @_; | ||
| 244 | $code ||= 302; | ||
| 245 | $self->response->redirect($to, $code); | ||
| 246 | $self->finish; | ||
| 247 | return; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub not_found { | ||
| 251 | my $self = shift; | ||
| 252 | $self->handle_not_found(@_); | ||
| 253 | $self->finish; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub handle_not_found { | ||
| 257 | my ($self) = @_; | ||
| 258 | $self->{response} = $self->req->new_response; | ||
| 259 | $self->status(404); | ||
| 260 | |||
| 261 | my @code = $self->get_trigger_code('NOT_FOUND'); | ||
| 262 | for my $code (@code) { | ||
| 263 | my $body = $code->($self->_make_args); | ||
| 264 | next unless $body; | ||
| 265 | return $self->body($body); | ||
| 266 | } | ||
| 267 | |||
| 268 | my $body = "NOT FOUND"; | ||
| 269 | my $type = "text/plain"; | ||
| 270 | |||
| 271 | my $tmpl = $self->base_dir . '/tmpl/404.html'; | ||
| 272 | if (-f $tmpl) { | ||
| 273 | $type = 'text/html'; | ||
| 274 | $body = read_file($tmpl); | ||
| 275 | } | ||
| 276 | |||
| 277 | $self->type($type); | ||
| 278 | $self->body($body); | ||
| 279 | } | ||
| 280 | |||
| 281 | sub unauthorized { | ||
| 282 | my $self = shift; | ||
| 283 | $self->handle_unauthorized(@_); | ||
| 284 | $self->finish; | ||
| 285 | } | ||
| 286 | |||
| 287 | sub handle_unauthorized { | ||
| 288 | my ($self) = @_; | ||
| 289 | $self->{response} = $self->req->new_response; | ||
| 290 | $self->status(401); | ||
| 291 | |||
| 292 | my @code = $self->get_trigger_code('UNAUTHORIZED'); | ||
| 293 | for my $code (@code) { | ||
| 294 | my $body = $code->($self->_make_args); | ||
| 295 | next unless $body; | ||
| 296 | return $self->body($body); | ||
| 297 | } | ||
| 298 | |||
| 299 | my $body = "UNAUTHORIZED"; | ||
| 300 | my $type = "text/plain"; | ||
| 301 | |||
| 302 | my $tmpl = $self->base_dir . '/tmpl/401.html'; | ||
| 303 | if (-f $tmpl) { | ||
| 304 | $type = 'text/html'; | ||
| 305 | $body = read_file($tmpl); | ||
| 306 | } | ||
| 307 | |||
| 308 | $self->type($type); | ||
| 309 | $self->body($body); | ||
| 310 | } | ||
| 311 | |||
| 312 | sub handle_error { | ||
| 313 | my ($self, $error) = @_; | ||
| 314 | $self->status(400); | ||
| 315 | |||
| 316 | my @code = $self->get_trigger_code('ERROR'); | ||
| 317 | for my $code (@code) { | ||
| 318 | my $body = $code->($self->_make_args($error)); | ||
| 319 | next unless $body; | ||
| 320 | return $self->body($body); | ||
| 321 | } | ||
| 322 | |||
| 323 | $self->receive_error($error); | ||
| 324 | } | ||
| 325 | |||
| 326 | sub handle_server_error { | ||
| 327 | my ($self, $error) = @_; | ||
| 328 | $self->status(500); | ||
| 329 | |||
| 330 | my @code = $self->get_trigger_code('SERVER_ERROR'); | ||
| 331 | for my $code (@code) { | ||
| 332 | my $body = $code->($self->_make_args($error)); | ||
| 333 | next unless $body; | ||
| 334 | return $self->body($body); | ||
| 335 | } | ||
| 336 | |||
| 337 | $self->receive_server_error($error); | ||
| 338 | } | ||
| 339 | |||
| 340 | sub receive_error { die $_[1] } | ||
| 341 | sub receive_server_error { die $_[1] } | ||
| 342 | |||
| 343 | sub find_route { | ||
| 344 | my $self = shift; | ||
| 345 | my ($controller, $action, $splat); | ||
| 346 | |||
| 347 | my $p = $self->router->match($self->env); | ||
| 348 | #warn Dumper $p; | ||
| 349 | return ($controller, $action, $splat) unless $p; | ||
| 350 | |||
| 351 | $controller = delete $p->{controller}; | ||
| 352 | $action = delete $p->{action}; | ||
| 353 | $splat = delete $p->{splat}; | ||
| 354 | |||
| 355 | # 余ったパラメータを追加 | ||
| 356 | for my $k (keys %{ $p }) { | ||
| 357 | $self->request->parameters->add($k, $p->{$k}); | ||
| 358 | } | ||
| 359 | |||
| 360 | # prefix がなかったら補完する | ||
| 361 | if ($controller) { | ||
| 362 | ($controller) = $self->find_class($controller); | ||
| 363 | } | ||
| 364 | |||
| 365 | # splat があったら、splat から controller を組み立てる | ||
| 366 | if ($splat) { | ||
| 367 | my @a = grep { $_ ne "/" } @{ $splat }; | ||
| 368 | unshift @a, $controller if $controller; | ||
| 369 | my ($class, $ext) = $self->find_class(join "/", @a); | ||
| 370 | $controller = $class if $class; | ||
| 371 | } | ||
| 372 | |||
| 373 | return { | ||
| 374 | controller => $controller, | ||
| 375 | action => $action, | ||
| 376 | splat => $splat, | ||
| 377 | }; | ||
| 378 | } | ||
| 379 | |||
| 380 | sub find_class { | ||
| 381 | my ($self, $path, $prefix) = @_; | ||
| 382 | return if not defined $path or $path eq ''; | ||
| 383 | |||
| 384 | $path =~ s|^/||; | ||
| 385 | $path =~ s/\.(.*)$//; | ||
| 386 | my $ext = $1; | ||
| 387 | $self->{ext} = $1; | ||
| 388 | |||
| 389 | my $class = join '::', map { capitalize($_) } grep { $_ ne '' } split '\/', $path; | ||
| 390 | |||
| 391 | $prefix ||= $self->request_handler_prefix; | ||
| 392 | |||
| 393 | if (defined $prefix and $prefix ne '') { | ||
| 394 | if ($class !~ /^$prefix/) { | ||
| 395 | $class = join '::', $prefix, $class; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | return ($class, $ext); | ||
| 400 | } | ||
| 401 | |||
| 402 | sub find_method { | ||
| 403 | my ($self) = @_; | ||
| 404 | my $request_method = $self->method; | ||
| 405 | $request_method = lc $request_method if defined $request_method; | ||
| 406 | return unless $request_method =~ /^(get|post|put|delete|options|patch|trace|link|unlink)$/; | ||
| 407 | return sub {} if $request_method eq 'options'; # for preflight request (CORS) | ||
| 408 | return $self->handler->can($request_method) | ||
| 409 | || $self->handler->can($self->request_handler_method); | ||
| 410 | } | ||
| 411 | |||
| 412 | sub model { | ||
| 413 | my $self = shift; | ||
| 414 | my $package = shift; | ||
| 415 | |||
| 416 | my $prefix = $self->namespace . '::Model'; | ||
| 417 | unless ($package =~ m/^$prefix/) { | ||
| 418 | $package = $prefix . '::' . $package; | ||
| 419 | } | ||
| 420 | |||
| 421 | $self->models->{$package} //= $self->create_module($package, @_); | ||
| 422 | } | ||
| 423 | |||
| 424 | sub create_module { | ||
| 425 | my $self = shift; | ||
| 426 | my $package = shift; | ||
| 427 | |||
| 428 | die "package name must be specified to create module." | ||
| 429 | unless defined $package; | ||
| 430 | |||
| 431 | my $prefix = $self->namespace; | ||
| 432 | unless ($package =~ m/^$prefix/) { | ||
| 433 | $package = $prefix . '::' . $package; | ||
| 434 | } | ||
| 435 | |||
| 436 | load_class($package); | ||
| 437 | my $module = $package->new(context => $self, @_); | ||
| 438 | weaken $module->{context}; | ||
| 439 | $module->init($self); | ||
| 440 | return $module; | ||
| 441 | } | ||
| 442 | |||
| 443 | sub call_before_trigger { | ||
| 444 | my $self = shift; | ||
| 445 | if ($self->state eq BEFORE_DISPATCH) { | ||
| 446 | $self->call_trigger(BEFORE_DISPATCH => $self, $self->request); | ||
| 447 | $self->{state} = DISPATCHING; | ||
| 448 | } | ||
| 449 | } | ||
| 450 | |||
| 451 | sub call_after_trigger { | ||
| 452 | my $self = shift; | ||
| 453 | if ($self->state eq DISPATCHING) { | ||
| 454 | $self->call_trigger(AFTER_DISPATCH => $self, $self->response); | ||
| 455 | $self->{state} = AFTER_DISPATCH; | ||
| 456 | } | ||
| 457 | } | ||
| 458 | |||
| 459 | sub load_plugins { | ||
| 460 | my ($class, @args) = @_; | ||
| 461 | |||
| 462 | while (@args) { | ||
| 463 | my $module = shift @args; | ||
| 464 | my $conf = shift @args; | ||
| 465 | next unless defined $module; | ||
| 466 | $class->load_plugin($module, $conf); | ||
| 467 | } | ||
| 468 | } | ||
| 469 | |||
| 470 | sub load_plugin { | ||
| 471 | my ($class, $module, $conf) = @_; | ||
| 472 | if (installed($module, 'App::Plugin')) { | ||
| 473 | $module = load_class($module, 'App::Plugin'); | ||
| 474 | } else { | ||
| 475 | $module = load_class($module, 'Dwarf::Plugin'); | ||
| 476 | } | ||
| 477 | $module->init($class, $conf); | ||
| 478 | } | ||
| 479 | |||
| 480 | sub _make_args { | ||
| 481 | my $self = shift; | ||
| 482 | my @args; | ||
| 483 | push @args, $self->handler if defined $self->handler; | ||
| 484 | push @args, $self; | ||
| 485 | push @args, @_; | ||
| 486 | return @args; | ||
| 487 | } | ||
| 488 | |||
| 489 | 1; | ||
| 490 | __END__ |