OSDN Git Service

Web: print log level setting on startup
[newslash/newslash.git] / src / newslash_web / lib / Newslash / Web.pm
1 package Newslash::Web;
2 use Mojo::Base 'Mojolicious';
3 use Mojo::Util qw(dumper);
4 use List::Util qw(any);
5
6 use constant CONFIG_FILE => '/etc/newslash/newslash.conf';
7 has subcommand => "";
8
9 # This method will run once at server start
10 sub startup {
11     my $app = shift;
12
13     if ($ARGV[0]) {
14         $app->subcommand($ARGV[0]);
15     }
16
17     # add commands in Newslash::Command
18     push @{$app->commands->namespaces}, 'Newslash::Command';
19
20     # load config file
21     # first, check existence of /etc/newslash.conf
22     if ($app->mode eq 'production' && -e CONFIG_FILE) {
23         $app->plugin('Newslash::Plugin::YAMLConfig', file => CONFIG_FILE);
24     }
25     else {
26         if ($app->subcommand eq "configdump") {
27             $app->plugin('Newslash::Plugin::YAMLConfig', default => {});
28         }
29         else {
30             $app->plugin('Newslash::Plugin::YAMLConfig');
31         }
32     }
33
34     # load default configuration values
35     $app->plugin('Newslash::Plugin::DefaultConfig');
36
37     # TODO: load/save configs with database
38
39     if ($app->config->{Log} && $app->config->{Log}->{backtrace}) {
40         require Carp::Always;
41         Carp::Always->import;
42     }
43
44     # system log config
45     if ($app->config->{Log} && $app->config->{Log}->{system_log}) {
46         my $cnf = $app->config->{Log}->{system_log};
47
48         # set log level
49         if ($cnf->{level}) {
50             my $loglv = $cnf->{level};
51             if (grep { $loglv eq $_ } qw(debug info warn error fatal)) {
52                 $app->log->level($loglv);
53                 $app->log->info("set log level to $loglv");
54             }
55             else {
56                 $app->log->warn('invalid log level given in config file');
57             }
58         }
59
60         # file output settings
61         if ($cnf->{mode} eq "local_file") {
62             my $pathname = $cnf->{local_file};
63             # check log is writable
64             if (!$pathname) {
65                 $app->log->error("cannot write system log to file: filename not given");
66             }
67             elsif (-e $pathname) {
68                 if (-w $pathname) {
69                     $app->log->debug("logs will be outputed to $pathname ...");
70                     $app->log(Mojo::Log->new);
71                     $app->log->path($pathname);
72                 }
73                 else {
74                     $app->log->error("cannot write system log to file: $pathname");
75                 }
76             }
77             else {
78                 if (open(my $fh, ">", $pathname)) {
79                     close($fh);
80                     $app->log->debug("logs will be outputed to $pathname ...");
81                     $app->log(Mojo::Log->new);
82                     $app->log->path($pathname);
83                 }
84                 else {
85                     $app->log->error("cannot create system log file: $pathname");
86                 }
87             }
88         }
89     }
90
91     # add static contents directories
92     if ($app->config->{System} && $app->config->{System}->{static_dir}) {
93         for my $dir (@{$app->config->{System}->{static_dir}}) {
94             push @{$app->static->paths}, $dir;
95         }
96     }
97
98     # secret key for hasing
99     $app->secrets([$app->config->{System}->{secret_key},]);
100
101     # when "test" mode, output debug logs.
102     $app->log->level('debug') if $app->mode eq 'test';
103
104     # check if 'maintenance' mode
105     my $maintenance_mode = 0;
106     if ($app->subcommand
107         && any { $app->subcommand eq $_ } qw[configdump configimport databaseinit
108                                              testdatainsert useradd usermod
109                                            ]) {
110         $maintenance_mode = 1;
111     }
112
113     ############################################################
114     #
115     # Plugin Settings
116     #
117     ############################################################
118
119     # profiler settings
120     if ($app->mode eq 'development'
121         && $app->config->{Profiler}
122         && $app->config->{Profiler}->{enable}) {
123         if ($app->config->{Profiler}->{profiler} eq 'nytprof') {
124             $app->plugin('Mojolicious::Plugin::NYTProf', { nytprof => {} });
125         }
126     }
127
128     # enable access logging
129     $app->plugin('Newslash::Plugin::AccessLog');
130
131     # Helpers for Newslash
132     $app->plugin('Newslash::Plugin::NewslashHelpers');
133
134     # use Epoch
135     $app->plugin('Newslash::Plugin::Epoch');
136
137     # support BasicAuth
138     $app->plugin('Newslash::Plugin::BasicAuth');
139
140     # use TimeLimitedCache ($app->cache)
141     $app->plugin('Newslash::Plugin::TimeLimitedCache');
142
143     # use KeyValue Store ($app->kvs)
144     $app->plugin('Newslash::Plugin::KeyValueStore');
145
146     # add Model Loader
147     $app->plugin('Newslash::Plugin::Model', { bypass_startup => $maintenance_mode });
148
149     # use Easy Cache ($app->ezcache)
150     #$app->plugin('Newslash::Plugin::EasyCache');
151
152     # use CompositeCache Cache ($app->ccache)
153     $app->plugin('Newslash::Plugin::CompositeCache');
154
155     # use Template::Toolkit 2 render
156     $app->plugin('Newslash::Plugin::TT2Renderer');
157
158     # use ResponseFilter (Faculities)
159     $app->plugin('Newslash::Plugin::ResponseFilter');
160
161     # use CustomBoxes
162     $app->plugin('Newslash::Plugin::CustomBoxes');
163
164     # use AntiCsrf
165     $app->plugin('Newslash::Plugin::AntiCsrf');
166
167     # contents preprocessor
168     $app->plugin('Newslash::Plugin::Preprocessor');
169     if ($maintenance_mode) {
170         $app->log->info("bypassing preprocessor...");
171     }
172     else {
173         $app->preprocessor->generate_all;
174     }
175
176     # javascript loader
177     $app->plugin('Newslash::Plugin::JavaScriptLoader');
178     if ($maintenance_mode) {
179         $app->log->info("bypassing javascriptloader...");
180     }
181     else {
182         $app->javascript_loader->load_all_bundles;
183     }
184
185     # user authorization
186     $app->plugin('Newslash::Plugin::UserAuth');
187
188     # access control
189     $app->plugin('Newslash::Plugin::AccessControl');
190
191     # ReCaptcha control
192     $app->plugin('Newslash::Plugin::ReCaptcha');
193
194     # Post Filter
195     $app->plugin('Newslash::Plugin::PostFilter');
196
197     # set canocal (for test.srad.jp)
198     $app->plugin('Newslash::Plugin::Canonical');
199
200     # DiscussionHelper
201     $app->plugin('Newslash::Plugin::DiscussionHelper');
202
203     # use HSTS
204     $app->plugin('Newslash::Plugin::Hsts');
205
206     # Event Que
207     $app->plugin('Newslash::Plugin::EventQue');
208
209     # Statics Logger
210     $app->plugin('Newslash::Plugin::Statics');
211
212     # Request Body based routing condition
213     $app->plugin('Newslash::Plugin::RequestBodyCondition');
214
215     # NS-RPC
216     $app->plugin('Newslash::Plugin::NSRPC');
217
218     # AD renderer
219     $app->plugin('Newslash::Plugin::ADRenderer');
220
221     # HTTP Compression
222     $app->plugin('Newslash::Plugin::HttpCompression');
223
224     # Users helper
225     $app->plugin('Newslash::Plugin::Users');
226
227     # Stories helper
228     $app->plugin('Newslash::Plugin::Stories');
229
230     # Sendmail helper
231     $app->plugin('Newslash::Plugin::Sendmail');
232
233     ############################################################
234     #
235     # Routing Settings
236     #
237     ############################################################
238
239     my $r = $app->routes;
240
241     # index page
242     $r->get('/')->to('timeline#stories');
243     $r->get('/recent')->to('timeline#recent');
244     $r->get('/popular')->to('timeline#popular');
245     $r->get('/comments')->to('timeline#comments');
246     $r->get('/journals')->to('timeline#journals');
247     $r->get('/submissions')->to('timeline#submissions');
248     $r->get('/polls')->to('timeline#polls');
249
250     # RSS
251     $r->get('/:rss_type' => [format => ['rss', 'xml']])->to('rss#stories');
252     $r->get('/journals/rss')->to('rss#journals');
253
254     # Banned page
255     $r->get('/banned')->to('index#banned', noindex => 1);
256
257     # Login / Logout
258     $r->get('/login')->to('login#login');
259     $r->post('/login')->to('login#login');
260     $r->get('/logout')->to('login#logout');
261
262     # story page
263     $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
264       ->to('story#single');
265
266     # comment page
267     $r->get('/comment/:cid/')->to('comment#single');
268
269     # journal page
270     $r->get('/journal/new')->to('journal#create', seclev => 1);
271     $r->get('/journal/:id/')->to('journal#single');
272
273     # submission page
274     $r->get('/submission/new')->to('submission#create');
275     $r->get('/submission/:id/')->to('submission#single');
276     #$r->post('/submission')->to('submission#create');
277
278     # polls page
279     $r->get('/poll/:qid')->to('poll#single');
280     $r->get('/vote/:qid')->to('poll#vote');
281     $r->post('/vote/:qid')->to('poll#vote_post', csrf_check_id => 'vote');
282
283     # archive page
284     $r->get('/story/:year/:month/:day/')->to('archive#story');
285     $r->get('/story/:year/:month/')->to('archive#story');
286     $r->get('/story/')->to('archive#story');
287
288     $r->get('/journal/:year/:month/:day/')->to('archive#journal');
289     $r->get('/journal/:year/:month/')->to('archive#journal');
290     $r->get('/journal/')->to('archive#journal');
291
292     $r->get('/submission/:year/:month/:day/')->to('archive#submission');
293     $r->get('/submission/:year/:month/')->to('archive#submission');
294     $r->get('/submission/')->to('archive#submission');
295
296     $r->get('/poll/:year/:month/')->to('archive#poll');
297     $r->get('/poll/')->to('archive#poll');
298
299     # tag page
300     $r->get('/tag/:tagname/:type/')->to('tag#list_tagged_items');
301     $r->get('/tag/:tagname/')->to('tag#list_tagged_items');
302     $r->get('/tag/')->to('tag#list_tags');
303
304     # my page
305     $r->get('/my/settings')->to('my#settings', seclev => 1);
306     $r->get('/my/sidebar')->to('my#sidebar', seclev => 1);
307     $r->get('/my/messages')->to('my#messages', seclev => 1);
308     $r->get('/my/')->to('user#home', seclev => 1);
309
310     # User Register
311     $r->get('/my/newuser')->to('login#newuser');
312     $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
313     $r->get('/my/activation')->to('login#activation');
314     $r->get('/my/resetpassword')->to('login#reset_password');
315     $r->post('/my/resetpassword')->to('login#reset_password', captcha_check => 1);
316
317     # Change Email
318     $r->get('/my/change_email')->to('my#change_email', seclev => 1);
319
320     # search page
321     $r->get('/search')->to('search#search');
322
323     # Admin
324     # pages under /admin needs seclev equal or greater than 10000;
325     my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
326
327     $admin->get('/firehose/:id/')->to('admin-firehose#single');
328     $admin->get('/submissions')->to('admin-submissions#index');
329
330     $admin->get('/css')->to('admin-css#edit');
331     $admin->get('/story/edit')->to('admin-story#edit');
332
333     $admin->get('/users')->to('admin-users#index');
334
335     $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
336
337     $admin->get('/sidebar')->to('admin-sidebar#index');
338     $admin->get('/feed')->to('admin-feed#index');
339     $admin->get('/filter')->to('admin-filter#index');
340     $admin->get('/blocking')->to('admin-blocking#index');
341     $admin->get('/ad')->to('admin-ads#index');
342
343     $admin->get('/repository')->to('admin-repository#index');
344     $admin->get('/cache')->to('admin-config#cache');
345
346     # Admin API
347     # pages under /api/v1/admin needs seclev equal or greater than 10000;
348     my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
349
350     $admin_api->get('/feed')->to('API::Admin::Feed#get');
351     $admin_api->post('/feed')->to('API::Admin::Feed#post');
352     $admin_api->get('/filter')->to('API::Admin::Filter#get');
353     $admin_api->post('/filter')->to('API::Admin::Filter#post');
354     $admin_api->get('/blocking')->to('API::Admin::Blocking#get');
355     $admin_api->post('/blocking')->to('API::Admin::Blocking#post');
356
357     $admin_api->get('/repository/export')->to('API::Admin::Repository#export');
358     $admin_api->get('/repository/import')->to('API::Admin::Repository#import');
359
360     $admin_api->post('/sidebar')->to('API::Admin::Sidebar#post');
361     $admin_api->get('/sidebar')->to('API::Admin::Sidebar#get');
362
363     # ad codes management
364     $app->rpc->route_to_model($admin_api->get('/ad/code/'), 'ad_codes', 'select');
365     $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "create"}), 'ad_codes', 'create');
366     $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "update"}), 'ad_codes', 'update');
367     $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "delete"}), 'ad_codes', 'delete');
368
369     # API
370     my $api = $r->under('/api/v1');
371     $api->post('/login')->to('API::Login#login');
372
373     $api->post('/newuser/validate')->to('API::User#validate_new_user');
374     $api->post('/newuser/create')->to('API::User#create_new_user');
375     $api->post('/newuser/password')->to('API::User#newuser_password');
376
377     $api->get('/sidebar/item')->to('API::SidebarItem#get', seclev => 1);
378
379     $api->get('/comment')->to('API::Comment#get');
380     $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
381
382     $api->get('/user')->to('API::User#get');
383     $api->post('/user')->to('API::User#post', seclev => 1);
384
385     $api->get('/journal')->to('API::Journal#get');
386     $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
387
388     $api->get('/submission')->to('API::Submission#get');
389     $api->get('/submissions')->to('API::Submission#list');
390     $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
391
392     $api->get('/story')->to('API::Story#get');
393     $api->post('/story')->to('API::Story#post');
394
395     $api->get('/poll')->to('API::Poll#get');
396     $api->post('/poll')->to('API::Poll#post');
397     $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
398
399     $api->get('/moderation')->to('API::Moderation#get');
400     $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
401
402     $api->get('/metamoderation')->to('API::Metamoderation#get');
403     $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
404
405     $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
406
407     $api->get('/token')->to('API::Token#get');
408
409     # user page
410     # warning: these pathes uses regexp matching, so must write in tail of route definitions.
411     my $user = $r->under('/~:nickname');
412     $user->get('/'             => [nickname => qr/.*/])->to('user#home');
413     $user->get('/journals'     => [nickname => qr/.*/])->to('user#journals');
414     $user->get('/journal'      => [nickname => qr/.*/])->to('user#journals'); # for compatibility
415     $user->get('/comments'     => [nickname => qr/.*/])->to('user#comments');
416     $user->get('/submissions'  => [nickname => qr/.*/])->to('user#submissions');
417     $user->get('/friends'      => [nickname => qr/.*/])->to('user#friends');
418     $user->get('/foes'         => [nickname => qr/.*/])->to('user#foes');
419     $user->get('/fans'         => [nickname => qr/.*/])->to('user#fans');
420     $user->get('/freaks'       => [nickname => qr/.*/])->to('user#freaks');
421     $user->get('/achievements' => [nickname => qr/.*/])->to('user#achievements');
422     #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');
423
424 }
425
426 1;