| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Plack::Middleware::Camelcadedb; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | =head1 NAME | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | Plack::Middleware::Camelcadedb - interactive debugging for Plack applications | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | # should be the first/one of the first modules to be loaded | 
| 10 |  |  |  |  |  |  | use Plack::Middleware::Camelcadedb ( | 
| 11 |  |  |  |  |  |  | remote_host => "localhost:9000", | 
| 12 |  |  |  |  |  |  | ); | 
| 13 |  |  |  |  |  |  | use Plack::Builder; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | builder { | 
| 16 |  |  |  |  |  |  | enable "Camelcadedb"; | 
| 17 |  |  |  |  |  |  | $app; | 
| 18 |  |  |  |  |  |  | }; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | You will also need to configure remote debugging in IntelliJ as described in the | 
| 21 |  |  |  |  |  |  | L | 
| 22 |  |  |  |  |  |  | section of the L | 
| 23 |  |  |  |  |  |  | wiki. | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | This module provides a thin integration layer on top of L. | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | At the beginning of each request, the middleware connects to IntelliJ to | 
| 30 |  |  |  |  |  |  | allow remote debugging. If IntelliJ is not running or not set up for debugging, | 
| 31 |  |  |  |  |  |  | execution continues as normal. | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =cut | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 1 |  |  | 1 |  | 612 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 26 |  | 
| 36 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 44 |  | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | use constant { | 
| 41 | 1 |  |  |  |  | 114 | DEBUG_SINGLE_STEP_ON        =>  0x20, | 
| 42 |  |  |  |  |  |  | DEBUG_USE_SUB_ADDRESS       =>  0x40, | 
| 43 |  |  |  |  |  |  | DEBUG_REPORT_GOTO           =>  0x80, | 
| 44 |  |  |  |  |  |  | DEBUG_ALL                   => 0x7ff, | 
| 45 | 1 |  |  | 1 |  | 5 | }; | 
|  | 1 |  |  |  |  | 2 |  | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | use constant { | 
| 48 | 1 |  |  |  |  | 427 | DEBUG_OFF                   => 0x0, | 
| 49 |  |  |  |  |  |  | DEBUG_PREPARE_FLAGS         => # 0x73c | 
| 50 |  |  |  |  |  |  | DEBUG_ALL & ~(DEBUG_USE_SUB_ADDRESS|DEBUG_REPORT_GOTO|DEBUG_SINGLE_STEP_ON), | 
| 51 | 1 |  |  | 1 |  | 6 | }; | 
|  | 1 |  |  |  |  | 2 |  | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | our @ISA; | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub import { | 
| 56 | 0 |  |  | 0 |  |  | my ($class, %args) = @_; | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | die "Specify 'remote_host'" | 
| 59 | 0 | 0 |  |  |  |  | unless $args{remote_host}; | 
| 60 | 0 |  |  |  |  |  | my ($host, $port) = split /:/, $args{remote_host}, 2; | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 0 |  |  |  |  |  | $ENV{PERL5_DEBUG_HOST} = $host; | 
| 63 | 0 |  |  |  |  |  | $ENV{PERL5_DEBUG_PORT} = $port; | 
| 64 | 0 |  |  |  |  |  | $ENV{PERL5_DEBUG_ROLE} = 'client'; | 
| 65 | 0 |  |  |  |  |  | $ENV{PERL5_DEBUG_AUTOSTART} = 0; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 0 | 0 |  |  |  |  | if ($args{enbugger}) { | 
| 68 | 0 |  |  |  |  |  | require Enbugger; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 |  |  |  |  |  | Enbugger->VERSION(2.014); | 
| 71 | 0 |  |  |  |  |  | Enbugger->load_source; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 |  |  |  |  |  | my $inc_path = $args{debug_client_path}; | 
| 75 | 0 | 0 |  |  |  |  | unshift @INC, ref $inc_path ? @$inc_path : $inc_path | 
|  |  | 0 |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | if $inc_path; | 
| 77 | 0 |  |  |  |  |  | require Devel::Camelcadedb; | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 0 |  |  |  |  |  | $^P = DEBUG_PREPARE_FLAGS; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 |  |  |  |  |  | require Plack::Middleware; | 
| 82 | 0 |  |  |  |  |  | require Plack::Request; | 
| 83 | 0 |  |  |  |  |  | require Plack::Response; | 
| 84 | 0 |  |  |  |  |  | require Plack::Util; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 0 |  |  |  |  |  | @ISA = qw(Plack::Middleware); | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | sub reopen_camelcadedb_connection { | 
| 90 | 0 |  |  | 0 | 0 |  | DB::connect_or_reconnect(); | 
| 91 | 0 | 0 |  |  |  |  | DB::enable() if DB::is_connected(); | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub close_camelcadedb_connection { | 
| 95 | 0 |  |  | 0 | 0 |  | DB::disconnect(); | 
| 96 | 0 |  |  |  |  |  | DB::disable(); | 
| 97 |  |  |  |  |  |  | # this works around uWSGI bug fixed by | 
| 98 |  |  |  |  |  |  | # https://github.com/unbit/uwsgi/commit/c6f61719106908b82ba2714fd9d2836fb1c27f22 | 
| 99 | 0 |  |  |  |  |  | $^P = DEBUG_OFF; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | sub call { | 
| 103 | 0 |  |  | 0 | 0 |  | my($self, $env) = @_; | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 0 |  |  |  |  |  | reopen_camelcadedb_connection(); | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 0 |  |  |  |  |  | my $res = $self->app->($env); | 
| 108 |  |  |  |  |  |  | Plack::Util::response_cb($res, sub { | 
| 109 |  |  |  |  |  |  | return sub { | 
| 110 |  |  |  |  |  |  | # use $_[0] to try to avoid a copy | 
| 111 | 0 | 0 | 0 |  |  |  | if (!defined $_[0] && DB::is_connected()) { | 
| 112 | 0 |  |  |  |  |  | close_camelcadedb_connection(); | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 0 |  |  |  |  |  | return $_[0]; | 
| 116 | 0 |  |  | 0 |  |  | }; | 
| 117 | 0 |  |  |  |  |  | }); | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | 1; | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | __END__ |