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 ############################################################
222 ############################################################
224 my $r = $app->routes;
227 $r->get('/')->to('timeline#stories');
228 $r->get('/recent')->to('timeline#recent');
229 $r->get('/popular')->to('timeline#popular');
230 $r->get('/comments')->to('timeline#comments');
231 $r->get('/journals')->to('timeline#journals');
232 $r->get('/submissions')->to('timeline#submissions');
233 $r->get('/polls')->to('timeline#polls');
236 $r->get('/banned')->to('index#banned', noindex => 1);
239 $r->get('/login')->to('login#login');
240 $r->post('/login')->to('login#login');
241 $r->get('/logout')->to('login#logout');
244 $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
245 ->to('story#single');
248 $r->get('/comment/:cid/')->to('comment#single');
251 $r->get('/journal/new')->to('journal#create', seclev => 1);
252 $r->get('/journal/:id/')->to('journal#single');
255 $r->get('/submission/new')->to('submission#create');
256 $r->get('/submission/:id/')->to('submission#single');
257 #$r->post('/submission')->to('submission#create');
260 $r->get('/poll/:qid')->to('poll#single');
261 $r->get('/vote/:qid')->to('poll#vote');
262 $r->post('/vote/:qid')->to('poll#vote_post', csrf_check_id => 'vote');
265 $r->get('/story/:year/:month/:day/')->to('archive#story');
266 $r->get('/story/:year/:month/')->to('archive#story');
267 $r->get('/story/')->to('archive#story');
269 $r->get('/journal/:year/:month/:day/')->to('archive#journal');
270 $r->get('/journal/:year/:month/')->to('archive#journal');
271 $r->get('/journal/')->to('archive#journal');
273 $r->get('/submission/:year/:month/:day/')->to('archive#submission');
274 $r->get('/submission/:year/:month/')->to('archive#submission');
275 $r->get('/submission/')->to('archive#submission');
277 $r->get('/poll/:year/:month/')->to('archive#poll');
278 $r->get('/poll/')->to('archive#poll');
281 $r->get('/tag/:tagname/:type/')->to('tag#list_tagged_items');
282 $r->get('/tag/:tagname/')->to('tag#list_tagged_items');
283 $r->get('/tag/')->to('tag#list_tags');
286 $r->get('/my/settings')->to('my#settings', seclev => 1);
287 $r->get('/my/sidebar')->to('my#sidebar', seclev => 1);
288 $r->get('/my/messages')->to('my#messages', seclev => 1);
289 $r->get('/my/')->to('user#home', seclev => 1);
292 $r->get('/my/newuser')->to('login#newuser');
293 $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
296 $r->get('/search')->to('search#search');
299 # pages under /admin needs seclev equal or greater than 10000;
300 my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
302 $admin->get('/firehose/:id/')->to('admin-firehose#single');
303 $admin->get('/submissions')->to('admin-submissions#index');
305 $admin->get('/css')->to('admin-css#edit');
306 $admin->get('/story/edit')->to('admin-story#edit');
308 $admin->get('/users')->to('admin-users#index');
310 $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
312 $admin->get('/sidebar')->to('admin-sidebar#index');
313 $admin->get('/feed')->to('admin-feed#index');
314 $admin->get('/blocking')->to('admin-blocking#index');
315 $admin->get('/ad')->to('admin-ads#index');
317 $admin->get('/repository')->to('admin-repository#index');
318 $admin->get('/cache')->to('admin-config#cache');
321 # pages under /api/v1/admin needs seclev equal or greater than 10000;
322 my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
323 $admin_api->get('/feed')->to('API::Admin::Feed#get');
324 $admin_api->post('/feed')->to('API::Admin::Feed#post');
325 $admin_api->get('/blocking')->to('API::Admin::Blocking#get');
326 $admin_api->post('/blocking')->to('API::Admin::Blocking#post');
328 $admin_api->get('/repository/export')->to('API::Admin::Repository#export');
329 $admin_api->get('/repository/import')->to('API::Admin::Repository#import');
331 $admin_api->post('/sidebar')->to('API::Admin::Sidebar#post');
332 $admin_api->get('/sidebar')->to('API::Admin::Sidebar#get');
334 # ad codes management
335 $app->rpc->route_to_model($admin_api->get('/ad/code/'), 'ad_codes', 'select');
336 $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "create"}), 'ad_codes', 'create');
337 $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "update"}), 'ad_codes', 'update');
338 $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "delete"}), 'ad_codes', 'delete');
341 my $api = $r->under('/api/v1');
342 $api->post('/login')->to('API::Login#login');
344 $api->get('/sidebar/item')->to('API::SidebarItem#get', seclev => 1);
346 $api->get('/comment')->to('API::Comment#get');
347 $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
349 $api->get('/user')->to('API::User#get');
350 $api->post('/user')->to('API::User#post', seclev => 1);
352 $api->get('/journal')->to('API::Journal#get');
353 $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
355 $api->get('/submission')->to('API::Submission#get');
356 $api->get('/submissions')->to('API::Submission#list');
357 $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
359 $api->get('/story')->to('API::Story#get');
360 $api->post('/story')->to('API::Story#post');
362 $api->get('/poll')->to('API::Poll#get');
363 $api->post('/poll')->to('API::Poll#post');
364 $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
366 $api->get('/moderation')->to('API::Moderation#get');
367 $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
369 $api->get('/metamoderation')->to('API::Metamoderation#get');
370 $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
372 $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
374 $api->get('/token')->to('API::Token#get');
377 # warning: these pathes uses regexp matching, so must write in tail of route definitions.
378 my $user = $r->under('/:nickname');
379 $user->get('/' => [nickname => qr/~.*/])->to('user#home');
380 $user->get('/journals' => [nickname => qr/~.*/])->to('user#journals');
381 $user->get('/journal' => [nickname => qr/~.*/])->to('user#journals'); # for compatibility
382 $user->get('/comments' => [nickname => qr/~.*/])->to('user#comments');
383 $user->get('/submissions' => [nickname => qr/~.*/])->to('user#submissions');
384 $user->get('/friends' => [nickname => qr/~.*/])->to('user#friends');
385 $user->get('/foes' => [nickname => qr/~.*/])->to('user#foes');
386 $user->get('/fans' => [nickname => qr/~.*/])->to('user#fans');
387 $user->get('/freaks' => [nickname => qr/~.*/])->to('user#freaks');
388 $user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
389 #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');