View | Details | Raw Unified | Return to bug 20582
Collapse All | Expand All

(-)a/Koha/App/Koha.pm (+56 lines)
Line 0 Link Here
1
package Koha::App::Koha;
2
3
use Modern::Perl;
4
5
use Mojo::Base 'Mojolicious';
6
7
sub startup {
8
    my ($self) = @_;
9
10
    push @{$self->plugins->namespaces}, 'Koha::App::Plugin';
11
    push @{$self->static->paths}, $self->home->rel_file('koha-tmpl');
12
13
    # Create route for all CGI scripts, need to be loaded first because of
14
    # CGI::Compile
15
    $self->plugin('CGIBinKoha');
16
17
    # Create routes for API
18
    # FIXME This generates routes like this: /api/api/v1/...
19
    $self->plugin('RESTV1');
20
21
    $self->hook(before_dispatch => \&_before_dispatch);
22
23
    # Everything after this comment is only needed to use Mojolicious
24
    # controllers.
25
    # We can't use them for anything serious right now because of the
26
    # authentication code that needs a full rewrite
27
28
    $self->plugin('TemplateToolkit', template => {PLUGIN_BASE => 'Koha::Template::Plugin'});
29
    $self->renderer->default_handler('tt2');
30
31
    $self->routes->namespaces(['Koha::Controller']);
32
33
    my $r = $self->routes;
34
35
    $r->any('/')->to(cb => sub { shift->redirect_to('/cgi-bin/koha/opac-main.pl') });
36
    $r->any('/intranet')->to(cb => sub { shift->redirect_to('/cgi-bin/koha/mainpage.pl') });
37
}
38
39
sub _before_dispatch {
40
    my $c = shift;
41
42
    # URL rewriting in Koha! No Apache/Nginx conf needed
43
    my $url = $c->req->url->path->to_string;
44
45
    # Remove Koha version from URL
46
    $url =~ s/_\d{2}\.\d{7}\.(js|css)/.$1/;
47
48
    # See FIXME above
49
    if ($url =~ m|^/api/v|) {
50
        $url = '/api' . $url;
51
    }
52
53
    $c->req->url->parse($url);
54
}
55
56
1;
(-)a/Koha/App/Plugin/CGIBinKoha.pm (+75 lines)
Line 0 Link Here
1
package Koha::App::Plugin::CGIBinKoha;
2
3
use Modern::Perl;
4
5
use Mojo::Base 'Mojolicious::Plugin';
6
7
use CGI::Compile;
8
use CGI::Emulate::PSGI;
9
use IO::Scalar;
10
11
sub register {
12
    my ($self, $app) = @_;
13
14
    $app->routes->any('/cgi-bin/koha/*name' => sub {
15
        my ($c) = @_;
16
17
        my $script = $c->stash('name');
18
19
        # Special case for installer which can takes a long time
20
        $c->inactivity_timeout(300) if $script eq 'installer/install.pl';
21
22
        # Remove trailing slash, if any (e.g. .../svc/config/systempreferences/)
23
        $script =~ s|/$||;
24
25
        # Guess if it's an OPAC script, or an Intranet script
26
        # FIXME: errors scripts (400.pl, 401.pl, ...) exist in both intranet and
27
        # opac. The intranet ones will never be executed
28
        # Same for svc/club/cancel_enrollment, svc/club/enroll and svc/report
29
        if (-e $c->app->home->rel_file("opac/$script")) {
30
            $script = "opac/$script";
31
        }
32
33
        unless (-e $c->app->home->rel_file($script)) {
34
            return $c->reply->not_found;
35
        }
36
37
        my $sub = CGI::Compile->compile($script);
38
        my $app = CGI::Emulate::PSGI->handler($sub);
39
        my $response = $app->($self->_psgi_env($c));
40
41
        $c->res->code($response->[0]);
42
        $c->res->headers->from_hash({ @{ $response->[1] } });
43
        $c->res->body(join('', @{$response->[2]}));
44
        $c->rendered;
45
    })->name('cgi');
46
}
47
48
sub _psgi_env {
49
    my ($self, $c) = @_;
50
51
    my $body = $c->req->body;
52
    my $env = {
53
        'psgi.input' => IO::Scalar->new(\$body),
54
        'psgi.errors' => *STDERR,
55
        REQUEST_METHOD => $c->req->method,
56
        QUERY_STRING => $c->req->url->query->to_string,
57
        SERVER_NAME => $c->req->url->to_abs->host,
58
        SERVER_PORT => $c->req->url->to_abs->port,
59
        SERVER_PROTOCOL => 'HTTP/1.1',
60
        CONTENT_LENGTH => $c->req->headers->content_length,
61
        CONTENT_TYPE => $c->req->headers->content_type,
62
        REMOTE_ADDR => $c->tx->remote_address,
63
        SCRIPT_NAME => $c->req->url->path->to_string,
64
    };
65
    for my $name (@{ $c->req->headers->names }) {
66
        my $value = $c->req->headers->header($name);
67
        $name =~ s/-/_/g;
68
        $name = 'HTTP_' . uc($name);
69
        $env->{$name} = $value;
70
    }
71
72
    return $env;
73
}
74
75
1;
(-)a/Koha/App/Plugin/RESTV1.pm (+15 lines)
Line 0 Link Here
1
package Koha::App::Plugin::RESTV1;
2
3
use Modern::Perl;
4
5
use Mojo::Base 'Mojolicious::Plugin';
6
7
use Koha::REST::V1;
8
9
sub register {
10
    my ($self, $app) = @_;
11
12
    $app->routes->any('/api')->detour(app => Koha::REST::V1->new);
13
}
14
15
1;
(-)a/bin/koha (+11 lines)
Line 0 Link Here
1
#!/usr/bin/env perl
2
3
use Modern::Perl;
4
5
# No need for PERL5LIB
6
use FindBin;
7
use lib "$FindBin::Bin/..";
8
9
use Mojolicious::Commands;
10
11
Mojolicious::Commands->start_app('Koha::App::Koha');
(-)a/bin/koha.psgi (+13 lines)
Line 0 Link Here
1
#!/usr/bin/env starman
2
3
use Modern::Perl;
4
5
use FindBin;
6
use Mojo::Server::PSGI;
7
use Plack::Builder;
8
9
builder {
10
    my $server = Mojo::Server::PSGI->new;
11
    $server->load_app("$FindBin::Bin/koha");
12
    $server->to_psgi_app;
13
};
(-)a/etc/nginx.conf (-1 / +15 lines)
Line 0 Link Here
0
- 
1
upstream koha {
2
    server 127.0.0.1:3000;
3
}
4
5
server {
6
    listen 80;
7
    listen [::]:80;
8
9
    server_name koha-dev; # CHANGEME
10
11
    location / {
12
        include proxy_params;
13
        proxy_pass http://koha;
14
    }
15
}

Return to bug 20582