2 use Mojo::Base 'Mojolicious';
3 use Mojo::Util qw(dumper);
4 use List::Util qw(any);
6 use constant CONFIG_FILE => '/etc/newslash/newslash.conf';
9 # This method will run once at server start
14 $app->subcommand($ARGV[0]);
17 # add commands in Newslash::Command
18 push @{$app->commands->namespaces}, 'Newslash::Command';
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);
26 if ($app->subcommand eq "configdump") {
27 $app->plugin('Newslash::Plugin::YAMLConfig', default => {});
30 $app->plugin('Newslash::Plugin::YAMLConfig');
34 # load default configuration values
35 $app->plugin('Newslash::Plugin::DefaultConfig');
37 # TODO: load/save configs with database
39 if ($app->config->{Log} && $app->config->{Log}->{backtrace}) {
45 if ($app->config->{Log} && $app->config->{Log}->{system_log}) {
46 my $cnf = $app->config->{Log}->{system_log};
50 my $loglv = $cnf->{level};
51 if (grep { $loglv eq $_ } qw(debug info warn error fatal)) {
52 $app->log->level($loglv);
55 $app->log->warn('invalid log level given in config file');
59 # file output settings
60 if ($cnf->{mode} eq "local_file") {
61 my $pathname = $cnf->{local_file};
62 # check log is writable
64 $app->log->error("cannot write system log to file: filename not given");
66 elsif (-e $pathname) {
68 $app->log->debug("logs will be outputed to $pathname ...");
69 $app->log(Mojo::Log->new);
70 $app->log->path($pathname);
73 $app->log->error("cannot write system log to file: $pathname");
77 if (open(my $fh, ">", $pathname)) {
79 $app->log->debug("logs will be outputed to $pathname ...");
80 $app->log(Mojo::Log->new);
81 $app->log->path($pathname);
84 $app->log->error("cannot create system log file: $pathname");
90 # add static contents directories
91 if ($app->config->{System} && $app->config->{System}->{static_dir}) {
92 for my $dir (@{$app->config->{System}->{static_dir}}) {
93 push @{$app->static->paths}, $dir;
97 # secret key for hasing
98 $app->secrets([$app->config->{System}->{secret_key},]);
100 # when "test" mode, output debug logs.
101 $app->log->level('debug') if $app->mode eq 'test';
103 # check if 'maintenance' mode
104 my $maintenance_mode = 0;
106 && any { $app->subcommand eq $_ } qw[configdump configimport databaseinit
107 testdatainsert useradd usermod
109 $maintenance_mode = 1;
112 ############################################################
116 ############################################################
119 if ($app->mode eq 'development'
120 && $app->config->{Profiler}
121 && $app->config->{Profiler}->{enable}) {
122 if ($app->config->{Profiler}->{profiler} eq 'nytprof') {
123 $app->plugin('Mojolicious::Plugin::NYTProf', { nytprof => {} });
127 # enable access logging
128 $app->plugin('Newslash::Plugin::AccessLog');
130 # Helpers for Newslash
131 $app->plugin('Newslash::Plugin::NewslashHelpers');
134 $app->plugin('Newslash::Plugin::Epoch');
137 $app->plugin('Newslash::Plugin::BasicAuth');
139 # use TimeLimitedCache ($app->cache)
140 $app->plugin('Newslash::Plugin::TimeLimitedCache');
142 # use KeyValue Store ($app->kvs)
143 $app->plugin('Newslash::Plugin::KeyValueStore');
146 $app->plugin('Newslash::Plugin::Model', { bypass_startup => $maintenance_mode });
148 # use Easy Cache ($app->ezcache)
149 $app->plugin('Newslash::Plugin::EasyCache');
151 # use Template::Toolkit 2 render
152 $app->plugin('Newslash::Plugin::TT2Renderer');
154 # use ResponseFilter (Faculities)
155 $app->plugin('Newslash::Plugin::ResponseFilter');
158 $app->plugin('Newslash::Plugin::CustomBoxes');
161 $app->plugin('Newslash::Plugin::AntiCsrf');
163 # contents preprocessor
164 $app->plugin('Newslash::Plugin::Preprocessor');
165 if ($maintenance_mode) {
166 $app->log->info("bypassing preprocessor...");
169 $app->preprocessor->generate_all;
173 $app->plugin('Newslash::Plugin::JavaScriptLoader');
174 if ($maintenance_mode) {
175 $app->log->info("bypassing javascriptloader...");
178 $app->javascript_loader->load_all_bundles;
182 $app->plugin('Newslash::Plugin::UserAuth');
185 $app->plugin('Newslash::Plugin::AccessControl');
188 $app->plugin('Newslash::Plugin::ReCaptcha');
190 # set canocal (for test.srad.jp)
191 $app->plugin('Newslash::Plugin::Canonical');
194 $app->plugin('Newslash::Plugin::DiscussionHelper');
197 $app->plugin('Newslash::Plugin::Hsts');
200 $app->plugin('Newslash::Plugin::EventQue');
203 $app->plugin('Newslash::Plugin::Statics');
205 # Request Body based routing condition
206 $app->plugin('Newslash::Plugin::RequestBodyCondition');
209 $app->plugin('Newslash::Plugin::NSRPC');
212 $app->plugin('Newslash::Plugin::ADRenderer');
215 $app->plugin('Newslash::Plugin::HttpCompression');
218 $app->plugin('Newslash::Plugin::Users');
221 $app->plugin('Newslash::Plugin::Sendmail');
223 ############################################################
227 ############################################################
229 my $r = $app->routes;
232 $r->get('/')->to('timeline#stories');
233 $r->get('/recent')->to('timeline#recent');
234 $r->get('/popular')->to('timeline#popular');
235 $r->get('/comments')->to('timeline#comments');
236 $r->get('/journals')->to('timeline#journals');
237 $r->get('/submissions')->to('timeline#submissions');
238 $r->get('/polls')->to('timeline#polls');
241 $r->get('/banned')->to('index#banned', noindex => 1);
244 $r->get('/login')->to('login#login');
245 $r->post('/login')->to('login#login');
246 $r->get('/logout')->to('login#logout');
249 $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
250 ->to('story#single');
253 $r->get('/comment/:cid/')->to('comment#single');
256 $r->get('/journal/new')->to('journal#create', seclev => 1);
257 $r->get('/journal/:id/')->to('journal#single');
260 $r->get('/submission/new')->to('submission#create');
261 $r->get('/submission/:id/')->to('submission#single');
262 #$r->post('/submission')->to('submission#create');
265 $r->get('/poll/:qid')->to('poll#single');
266 $r->get('/vote/:qid')->to('poll#vote');
267 $r->post('/vote/:qid')->to('poll#vote_post', csrf_check_id => 'vote');
270 $r->get('/story/:year/:month/:day/')->to('archive#story');
271 $r->get('/story/:year/:month/')->to('archive#story');
272 $r->get('/story/')->to('archive#story');
274 $r->get('/journal/:year/:month/:day/')->to('archive#journal');
275 $r->get('/journal/:year/:month/')->to('archive#journal');
276 $r->get('/journal/')->to('archive#journal');
278 $r->get('/submission/:year/:month/:day/')->to('archive#submission');
279 $r->get('/submission/:year/:month/')->to('archive#submission');
280 $r->get('/submission/')->to('archive#submission');
282 $r->get('/poll/:year/:month/')->to('archive#poll');
283 $r->get('/poll/')->to('archive#poll');
286 $r->get('/tag/:tagname/:type/')->to('tag#list_tagged_items');
287 $r->get('/tag/:tagname/')->to('tag#list_tagged_items');
288 $r->get('/tag/')->to('tag#list_tags');
291 $r->get('/my/settings')->to('my#settings', seclev => 1);
292 $r->get('/my/sidebar')->to('my#sidebar', seclev => 1);
293 $r->get('/my/messages')->to('my#messages', seclev => 1);
294 $r->get('/my/')->to('user#home', seclev => 1);
297 $r->get('/my/newuser')->to('login#newuser');
298 $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
299 $r->get('/my/activation')->to('login#activation');
300 $r->get('/my/resetpassword')->to('login#reset_password');
301 $r->post('/my/resetpassword')->to('login#reset_password');
304 $r->get('/search')->to('search#search');
307 # pages under /admin needs seclev equal or greater than 10000;
308 my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
310 $admin->get('/firehose/:id/')->to('admin-firehose#single');
311 $admin->get('/submissions')->to('admin-submissions#index');
313 $admin->get('/css')->to('admin-css#edit');
314 $admin->get('/story/edit')->to('admin-story#edit');
316 $admin->get('/users')->to('admin-users#index');
318 $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
320 $admin->get('/sidebar')->to('admin-sidebar#index');
321 $admin->get('/feed')->to('admin-feed#index');
322 $admin->get('/blocking')->to('admin-blocking#index');
323 $admin->get('/ad')->to('admin-ads#index');
325 $admin->get('/repository')->to('admin-repository#index');
326 $admin->get('/cache')->to('admin-config#cache');
329 # pages under /api/v1/admin needs seclev equal or greater than 10000;
330 my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
331 $admin_api->get('/feed')->to('API::Admin::Feed#get');
332 $admin_api->post('/feed')->to('API::Admin::Feed#post');
333 $admin_api->get('/blocking')->to('API::Admin::Blocking#get');
334 $admin_api->post('/blocking')->to('API::Admin::Blocking#post');
336 $admin_api->get('/repository/export')->to('API::Admin::Repository#export');
337 $admin_api->get('/repository/import')->to('API::Admin::Repository#import');
339 $admin_api->post('/sidebar')->to('API::Admin::Sidebar#post');
340 $admin_api->get('/sidebar')->to('API::Admin::Sidebar#get');
342 # ad codes management
343 $app->rpc->route_to_model($admin_api->get('/ad/code/'), 'ad_codes', 'select');
344 $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "create"}), 'ad_codes', 'create');
345 $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "update"}), 'ad_codes', 'update');
346 $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "delete"}), 'ad_codes', 'delete');
349 my $api = $r->under('/api/v1');
350 $api->post('/login')->to('API::Login#login');
352 $api->post('/newuser/validate')->to('API::User#validate_new_user');
353 $api->post('/newuser/create')->to('API::User#create_new_user');
354 $api->post('/newuser/password')->to('API::User#update_password');
356 $api->get('/sidebar/item')->to('API::SidebarItem#get', seclev => 1);
358 $api->get('/comment')->to('API::Comment#get');
359 $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
361 $api->get('/user')->to('API::User#get');
362 $api->post('/user')->to('API::User#post', seclev => 1);
364 $api->get('/journal')->to('API::Journal#get');
365 $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
367 $api->get('/submission')->to('API::Submission#get');
368 $api->get('/submissions')->to('API::Submission#list');
369 $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
371 $api->get('/story')->to('API::Story#get');
372 $api->post('/story')->to('API::Story#post');
374 $api->get('/poll')->to('API::Poll#get');
375 $api->post('/poll')->to('API::Poll#post');
376 $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
378 $api->get('/moderation')->to('API::Moderation#get');
379 $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
381 $api->get('/metamoderation')->to('API::Metamoderation#get');
382 $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
384 $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
386 $api->get('/token')->to('API::Token#get');
389 # warning: these pathes uses regexp matching, so must write in tail of route definitions.
390 my $user = $r->under('/:nickname');
391 $user->get('/' => [nickname => qr/~.*/])->to('user#home');
392 $user->get('/journals' => [nickname => qr/~.*/])->to('user#journals');
393 $user->get('/journal' => [nickname => qr/~.*/])->to('user#journals'); # for compatibility
394 $user->get('/comments' => [nickname => qr/~.*/])->to('user#comments');
395 $user->get('/submissions' => [nickname => qr/~.*/])->to('user#submissions');
396 $user->get('/friends' => [nickname => qr/~.*/])->to('user#friends');
397 $user->get('/foes' => [nickname => qr/~.*/])->to('user#foes');
398 $user->get('/fans' => [nickname => qr/~.*/])->to('user#fans');
399 $user->get('/freaks' => [nickname => qr/~.*/])->to('user#freaks');
400 $user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
401 #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');