|
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 22-25
sub startup {
Link Here
|
| 22 |
}); |
74 |
}); |
| 23 |
} |
75 |
} |
| 24 |
|
76 |
|
| 25 |
1; |
77 |
sub setKohaParamConfig { |
|
|
78 |
my $self = shift; |
| 79 |
#Enable the config-plugin. Loads the config file from $ENV{MOJO_CONFIG} by default. |
| 80 |
if ($ENV{MOJO_CONFIG}) { |
| 81 |
$self->plugin('Config'); |
| 82 |
} |
| 83 |
elsif (exists($ENV{MOJO_CONFIG})) { |
| 84 |
#Don't complain. |
| 85 |
} |
| 86 |
else { |
| 87 |
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"; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
sub setKohaParamLogging { |
| 92 |
my $self = shift; |
| 93 |
#Log to a filename with loglevel configured in environment variables |
| 94 |
if ($ENV{MOJO_LOGFILES}) { |
| 95 |
$self->app->log( Mojo::Log->new( path => $ENV{MOJO_LOGFILES}.'.log', level => ($ENV{MOJO_LOGLEVEL} || 'error') ) ); |
| 96 |
open(STDOUT,'>>',$ENV{MOJO_LOGFILES}.'.stdout') or die __PACKAGE__."::startup():> Couldn't open the STDOUT logfile for appending.\n".$!; |
| 97 |
open(STDERR,'>>',$ENV{MOJO_LOGFILES}.'.stderr') or die __PACKAGE__."::startup():> Couldn't open the STDERR logfile for appending.\n".$!; |
| 98 |
} |
| 99 |
#Stop complaining about missing logging config |
| 100 |
if (exists($ENV{MOJO_LOGFILES})) { |
| 101 |
$self->app->log(); |
| 102 |
} |
| 103 |
else { |
| 104 |
$self->app->log(); #Default to STDERR |
| 105 |
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"; |
| 106 |
} |
| 107 |
} |
| 108 |
1; |
| 26 |
- |
|
|