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

(-)a/etc/nginx.conf (+15 lines)
Line 0 Link Here
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
}
(-)a/lib/Koha/App/Koha.pm (+67 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. They are only two defined (one for opac, one for intranet)
25
    # and they just display a basic html page.
26
    # We can't use them for anything serious right now because of the
27
    # authentication code that needs a full rewrite
28
29
    $self->plugin('TemplateToolkit', template => {PLUGIN_BASE => 'Koha::Template::Plugin'});
30
    $self->renderer->default_handler('tt2');
31
32
    $self->routes->namespaces(['Koha::Controller']);
33
34
    my $r = $self->routes;
35
36
    my $root = $r->under('/' => {
37
        KOHA_VERSION => C4::Context->preference('Version'),
38
        interface => '/opac-tmpl',
39
        theme => C4::Context->preference('opacthemes'),
40
    });
41
    my $intranet = $root->under('/intranet' => {
42
        interface => '/intranet-tmpl',
43
        theme => C4::Context->preference('template'),
44
    });
45
46
    $intranet->any('/:controller/:action')->to('index#index', namespace => 'Koha::Controller::Intranet');
47
    $root->any('/:controller/:action')->to('index#index');
48
}
49
50
sub _before_dispatch {
51
    my $c = shift;
52
53
    # URL rewriting in Koha! No Apache/Nginx conf needed
54
    my $url = $c->req->url->path->to_string;
55
56
    # Remove Koha version from URL
57
    $url =~ s/_\d{2}\.\d{7}\.(js|css)/.$1/;
58
59
    # See FIXME above
60
    if ($url =~ m|^/api/v|) {
61
        $url = '/api' . $url;
62
    }
63
64
    $c->req->url->parse($url);
65
}
66
67
1;
(-)a/lib/Koha/App/Plugin/CGIBinKoha.pm (+69 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
        my $script = $c->stash('name');
17
18
        # Remove trailing slash, if any (e.g. .../svc/config/systempreferences/)
19
        $script =~ s|/$||;
20
21
        # Guess if it's an OPAC script, or an Intranet script
22
        # FIXME: errors scripts (400.pl, 401.pl, ...) exist in both intranet and
23
        # opac. The intranet ones will never be executed
24
        if (-e $c->app->home->rel_file("opac/$script")) {
25
            $script = "opac/$script";
26
        }
27
28
        unless (-e $c->app->home->rel_file($script)) {
29
            return $c->reply->not_found;
30
        }
31
32
        my $sub = CGI::Compile->compile($script);
33
        my $app = CGI::Emulate::PSGI->handler($sub);
34
        my $response = $app->($self->_psgi_env($c));
35
36
        $c->res->code($response->[0]);
37
        $c->res->headers->from_hash({ @{ $response->[1] } });
38
        $c->res->body($response->[2][0]);
39
        $c->rendered;
40
    })->name('cgi');
41
}
42
43
sub _psgi_env {
44
    my ($self, $c) = @_;
45
46
    my $body = $c->req->body;
47
    my $env = {
48
        'psgi.input' => IO::Scalar->new(\$body),
49
        'psgi.errors' => *STDERR,
50
        REQUEST_METHOD => $c->req->method,
51
        QUERY_STRING => $c->req->url->query->to_string,
52
        SERVER_NAME => $c->req->url->to_abs->host,
53
        SERVER_PORT => $c->req->url->to_abs->port,
54
        SERVER_PROTOCOL => 'HTTP/1.1',
55
        CONTENT_LENGTH => $c->req->headers->content_length,
56
        CONTENT_TYPE => $c->req->headers->content_type,
57
        REMOTE_ADDR => $c->tx->remote_address,
58
    };
59
    for my $name (@{ $c->req->headers->names }) {
60
        my $value = $c->req->headers->header($name);
61
        $name =~ s/-/_/g;
62
        $name = 'HTTP_' . uc($name);
63
        $env->{$name} = $value;
64
    }
65
66
    return $env;
67
}
68
69
1;
(-)a/lib/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/lib/Koha/Controller/Index.pm (+10 lines)
Line 0 Link Here
1
package Koha::Controller::Index;
2
3
use Modern::Perl;
4
5
use Mojo::Base 'Mojolicious::Controller';
6
7
sub index {
8
}
9
10
1;
(-)a/lib/Koha/Controller/Intranet/Index.pm (+15 lines)
Line 0 Link Here
1
package Koha::Controller::Intranet::Index;
2
3
use Modern::Perl;
4
5
use Mojo::Base 'Mojolicious::Controller';
6
7
sub index {
8
    my ($c) = @_;
9
10
    # FIXME: It should be possible to tell Mojo to search templates in intranet/
11
    # for routes under /intranet
12
    $c->render('intranet/index/index');
13
}
14
15
1;
(-)a/script/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/../lib", "$FindBin::Bin/..";
8
9
use Mojolicious::Commands;
10
11
Mojolicious::Commands->start_app('Koha::App::Koha');
(-)a/script/koha.psgi (+15 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
    enable 'Debug';
11
12
    my $server = Mojo::Server::PSGI->new;
13
    $server->load_app("$FindBin::Bin/koha");
14
    $server->to_psgi_app;
15
};
(-)a/templates/index/index.html.tt2 (+13 lines)
Line 0 Link Here
1
<!DOCTYPE html>
2
<html>
3
    <head>
4
        <title>Default page for OPAC</title>
5
    </head>
6
    <body>
7
        <h1>Default page for OPAC</h1>
8
        <p>Koha version is [% KOHA_VERSION %]</p>
9
10
        [%# No need to hardcode '/cgi-bin/koha' %]
11
        [% link_to('Homepage', 'cgi', { name = "opac-main.pl" }) %]
12
    </body>
13
</html>
(-)a/templates/intranet/index/index.html.tt2 (-1 / +13 lines)
Line 0 Link Here
0
- 
1
<!DOCTYPE html>
2
<html>
3
    <head>
4
        <title>Default page for Intranet</title>
5
    </head>
6
    <body>
7
        <h1>Default page for Intranet</h1>
8
        <p>Koha version is [% KOHA_VERSION %]</p>
9
10
        [%# No need to hardcode '/cgi-bin/koha' %]
11
        [% link_to('Homepage', 'cgi', { name = "mainpage.pl" }) %]
12
    </body>
13
</html>

Return to bug 20582