Debian Codegraph @

bin/mrest

raw
1 #!/usr/bin/perl
2 # *************************************************************************
3 # Copyright (c) 2014-2017, SUSE LLC
4 #
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright notice,
11 # this list of conditions and the following disclaimer.
12 #
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 #
17 # 3. Neither the name of SUSE LLC nor the names of its contributors may be
18 # used to endorse or promote products derived from this software without
19 # specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 # *************************************************************************
33 #
34 # Web::MREST server executable
35 #
36 # -------------------------------------------------------------------------
37
38 use 5.014;
39 use strict;
40 use warnings;
41
42 use App::CELL qw( $log $site );
43 use Web::MREST;
44 use Data::Dumper;
45 use File::Path qw( remove_tree );
46 use Getopt::Long;
47 use Log::Any::Adapter;
48 use Module::Runtime qw( is_module_name require_module );
49 use Plack::Builder;
50 use Plack::Runner;
51 use Plack::Session::Store::File;
52 use Pod::Usage;
53 use Try::Tiny;
54 use Web::Machine;
55 use Web::MREST::CLI qw( normalize_filespec );
56
57
58
59 =head1 NAME
60
61 mrest - Web::MREST server startup script
62
63
64
65 =head1 SYNOPSIS
66
67 Possible invocations (any options after C<--> are parsed by
68 L<Plack::Runner>: basically, they can be anything accepted by C<plackup>:
69
70 $ mrest --distro My-Distro --module My::Module
71 $ mrest -d My::Distro -m My::Module
72 $ mrest --distro My-Distro --module My::Module --sitedir /etc/my-module
73 $ mrest -d My::Distro -m My::Module -s /etc/my-module
74 $ mrest -- --server Starman
75 $ mrest --help
76
77
78 =head1 DESCRIPTION
79
80 Run this script from the bash prompt to start the server.
81
82
83
84 =head1 PASSING OPTIONS TO PLACK
85
86 This script starts the server by instantiates a L<Plack::Runner> object and
87 calling its C<run> method. Before doing that, it calls the C<parse_options>
88 method with C<@ARGV> as its argument. However, the script does so B<after>
89 parsing its own options. Options intended to be parsed by L<Plack::Runner>
90 should be placed at the end of the command line, preceded by C<-->.
91
92
93
94 =head1 DEBUGGING OPTIONS
95
96 You can include an C<--early-debug> option on the command line, with
97 the name of a file in which L<Web::MREST> will capture "early" log
98 messages -- i.e. all log messages generated before the "official" MREST
99 log file (i.e. the one defined in the site parameters) is known.
100
101
102
103 =head1 FUNCTIONS
104
105 =cut
106
107 sub _welcome_message {
108 my $message = "Starting Web::MREST ver. $Web::MREST::VERSION";
109 print "$message\n";
110 $log->notice( $message );
111 }
112
113 sub _get_sharedir {
114 my ( $distro, $module ) = @_;
115
116 # for distro, we allow both formats My::Distro and My-Distro
117 # distro might be just Foo, or Foo::Bar, or Foo-Bar
118 my @tmp_c;
119 if ( $distro =~ m/\:/ ) {
120 @tmp_c = split( '::', $distro );
121 } elsif ( $distro =~ m/\-/ ) {
122 @tmp_c = split( '-', $distro );
123 } else {
124 @tmp_c = ( $distro );
125 }
126 my $app_distro = join( '-', @tmp_c );
127 my $app_module = $module;
128 die "$app_module is not a module name" unless is_module_name( $app_module );
129 print "App distro is $app_distro\n";
130 print "App module is $app_module\n";
131 my $distro_sharedir;
132 my $status;
133 try {
134 $distro_sharedir = File::ShareDir::dist_dir( $app_distro );
135 print "Distro sharedir is $distro_sharedir\n";
136 } catch {
137 $status = "Could not find a sharedir for distro $app_distro: $_";
138 };
139 if ( $status ) {
140 die $status;
141 }
142 return ( $app_distro, $app_module, $distro_sharedir );
143 }
144
145 sub _load_config_params {
146 my ( $app_distro, $sitedir, $early_debug ) = @_;
147 print "Loading configuration parameters from $sitedir\n";
148 my $status = Web::MREST::init( distro => $app_distro, sitedir => $sitedir, early_debug => $early_debug );
149 die $status->text unless $status->ok;
150 }
151
152 sub _set_up_logging {
153 #print "Setting up logging\n";
154 my $log_file = normalize_filespec( $site->MREST_LOG_FILE );
155 my $should_reset = $site->MREST_LOG_FILE_RESET;
156 unlink $log_file if $should_reset;
157 Log::Any::Adapter->set( 'File', $log_file );
158 my $message = "Logging to $log_file";
159 print "$message\n";
160 print "MREST_DEBUG_MODE is set to " . ( $site->MREST_DEBUG_MODE || 0 ) . "\n";
161 $log->info( $message );
162
163 if ( ! $site->MREST_APPNAME ) {
164 die "Site parameter MREST_APPNAME is undefined - please investigate!";
165 }
166 $log->init(
167 ident => $site->MREST_APPNAME,
168 debug_mode => ( $site->MREST_DEBUG_MODE || 0 ),
169 );
170 }
171
172 sub function_exists {
173 no strict 'refs';
174 my $funcname = shift;
175 return \&{$funcname} if defined &{$funcname};
176 return;
177 }
178
179 sub _call_init {
180 my $app_module = shift;
181 require_module( $app_module );
182 if ( my $subref = function_exists( $app_module . "::init" ) ) {
183 print "Calling $app_module" . "::init()\n";
184 &$subref();
185 }
186 }
187
188 sub _init_webmachine {
189 my $app_module = shift;
190 print "Starting server\n";
191 return Web::Machine->new(
192 resource => $app_module,
193 tracing => 1,
194 )->to_app;
195 }
196
197
198
199 =head1 MAIN
200
201 =cut
202
203 _welcome_message();
204
205 # process command-line options
206 my $help = 0;
207 my $distro = '';
208 my $module = '';
209 my $sitedir = '';
210 my $early_debug = '';
211 GetOptions(
212 'distro|d=s' => \$distro,
213 'early-debug|e=s' => \$early_debug,
214 'help|?' => \$help,
215 'module|m=s' => \$module,
216 'sitedir|s=s' => \$sitedir,
217 );
218 pod2usage(1) if $help;
219
220 print "Early debug mode activated\n" if $early_debug;
221
222 # distro and module are required
223 die "You must specify at least --distro and --module (with values). Bailing out"
224 unless $distro and $module;
225
226 my ( $app_distro, $app_module, $distro_sharedir ) = _get_sharedir( $distro, $module );
227 print "Local site configuration directory is $sitedir\n" if $sitedir;
228 _load_config_params( $app_distro, $sitedir, $early_debug ); # initializes $site
229 _set_up_logging(); # needs $site
230 _call_init( $app_module );
231 my $app = _init_webmachine( $app_module );
232 my $runner = Plack::Runner->new;
233 $runner->parse_options(@ARGV);
234 remove_tree('/tmp/mrest-sessions');
235 mkdir '/tmp/mrest-sessions';
236
237 $runner->run(
238 builder {
239 enable "LogErrors", logger => sub {
240 my $args = shift;
241 my $level = $args->{'level'};
242 my $message = $args->{'message'};
243 $log->$level( $message );
244 };
245 # enable "StackTrace", force => 1;
246 # enable "Session", store => 'File';
247 enable "Session", store => Plack::Session::Store::File->new(
248 dir => '/tmp/mrest-sessions'
249 );
250 enable "Static", path => qr{/favicon}, root => $distro_sharedir;
251 $app;
252 }
253 );
254