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

(-)a/Koha/REST/V1.pm (-1 / +83 lines)
Lines 2-11 package Koha::REST::V1; Link Here
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use Mojo::Base 'Mojolicious';
4
use Mojo::Base 'Mojolicious';
5
use Mojo::Log;
6
7
=head startup
8
9
Starts the Mojolicious application using whatever server is configured.
10
11
Use environmental values to control some aspects of Mojolicious:
12
This way we can have different settings for different servers running Mojolicious.
13
14
=head2 Configuration file
15
16
$ENV{MOJO_CONFIG} should be set in the system service (init) starting Mojolicious, eg:
17
export MOJO_CONFIG=/home/koha/kohaclone/api/v1/hypnotoad.conf
18
19
This configuration file read by the Mojolicious::Plugin::Config
20
http://mojolicio.us/perldoc/Mojolicious/Plugin/Config
21
22
If you don't want to use any config files, disable the alert notifications ny setting the
23
MOJO_CONFIG-environment variable to undef.
24
25
=head2 Logging
26
27
    #NOTE!!
28
    #There is a "feature" in Mojo::Server disabling STDOUT and STDERR, because such errors are not-suited-for-production?!?
29
    #This modification in Mojo::Server disables this and preserves the STD* handles for forked server threads
30
    #in Mojo::Server::daemonize(), comment out the following lines
31
    #
32
    #  # Close filehandles
33
    #  open STDOUT, '>/dev/null';
34
    #  open STDERR, '>&STDOUT';
35
36
Log to a filename configured in an environment variable $ENV{MOJO_LOGFILES} using loglevel $ENV{MOJO_LOGLEVEL}.
37
Actually you get 3 logfiles on $ENV{MOJO_LOGFILES}.
38
.log for Mojo::Log
39
.stdout for STDOUT
40
.stderr for STDERR
41
42
Defaults to STDERR and loglevel of 'error'
43
Examples:
44
export MOJO_LOGFILES=/home/koha/koha-dev/var/log/kohaapi.mojo
45
export MOJO_LOGLEVEL=debug
46
47
Logging is done by Mojo::Log
48
http://www.mojolicio.us/perldoc/Mojo/Log
49
50
If you want to get log output to STDOUT and STDERR normally, disable the alert notifications by setting the
51
MOJO_LOGFILES-environment variable to undef.
52
53
=cut
5
54
6
sub startup {
55
sub startup {
7
    my $self = shift;
56
    my $self = shift;
8
57
58
    $self->setKohaParamLogging();
59
    $self->setKohaParamConfig();
60
9
    my $route = $self->routes->under->to(
61
    my $route = $self->routes->under->to(
10
        cb => sub {
62
        cb => sub {
11
            my $c = shift;
63
            my $c = shift;
Lines 25-28 sub startup { Link Here
25
    });
77
    });
26
}
78
}
27
79
80
sub setKohaParamConfig {
81
    my $self = shift;
82
    #Enable the config-plugin. Loads the config file from $ENV{MOJO_CONFIG} by default.
83
    if ($ENV{MOJO_CONFIG}) {
84
        $self->plugin('Config');
85
    }
86
    elsif (exists($ENV{MOJO_CONFIG})) {
87
        #Don't complain.
88
    }
89
    else {
90
        print __PACKAGE__."::startup():> No config-file loaded. Define your config-file to the MOJO_CONFIG environmental variable. If you don't want to use a specific config-file, set the MOJO_CONFIG to undef.\n";
91
    }
92
}
93
94
sub setKohaParamLogging {
95
    my $self = shift;
96
    #Log to a filename with loglevel configured in environment variables
97
    if ($ENV{MOJO_LOGFILES}) {
98
        $self->app->log( Mojo::Log->new( path => $ENV{MOJO_LOGFILES}.'.log', level => ($ENV{MOJO_LOGLEVEL} || 'error') ) );
99
        open(STDOUT,'>>',$ENV{MOJO_LOGFILES}.'.stdout') or die __PACKAGE__."::startup():> Couldn't open the STDOUT logfile for appending.\n".$!;
100
        open(STDERR,'>>',$ENV{MOJO_LOGFILES}.'.stderr') or die __PACKAGE__."::startup():> Couldn't open the STDERR logfile for appending.\n".$!;
101
    }
102
    #Stop complaining about missing logging config
103
    if (exists($ENV{MOJO_LOGFILES})) {
104
        $self->app->log();
105
    }
106
    else {
107
        $self->app->log(); #Default to STDERR
108
        print __PACKAGE__."::startup():> No logfile given, defaulting to STDERR. Define your logfile and loglevel to the MOJO_LOGFILES and MOJO_LOGLEVEL environmental variables. If you want foreground logging, set the MOJO_LOGFILES as undef.\n";
109
    }
110
}
28
1;
111
1;
29
- 

Return to bug 14746