2 use Mojo::Base 'Mojolicious';
3 use Mojo::Util qw(dumper);
7 use constant CONFIG_FILE => '/etc/newslash/newslash.conf';
9 # This method will run once at server start
14 # first, check existence of /etc/newslash.conf
15 if ($app->mode eq 'production' && -e CONFIG_FILE) {
16 $app->plugin('Newslash::Plugin::YAMLConfig', file => CONFIG_FILE);
19 #$app->plugin('JSONConfig');
20 $app->plugin('Newslash::Plugin::YAMLConfig');
22 # TODO: load/save configs with database
24 if ($app->config->{Log} && $app->config->{Log}->{backtrace}) {
30 if ($app->config->{Log} && $app->config->{Log}->{system_log}) {
31 # check log is writable
32 my $pathname = $app->config->{Log}->{system_log};
35 $app->log->debug("logs will be outputed to $pathname ...");
36 $app->log(Mojo::Log->new);
37 $app->log->path($pathname);
40 $app->log->error("cannot write system log to file: $pathname");
44 if (open(my $fh, ">", $pathname)) {
46 $app->log->debug("logs will be outputed to $pathname ...");
47 $app->log(Mojo::Log->new);
48 $app->log->path($pathname);
51 $app->log->error("cannot create system log file: $pathname");
57 if ($app->config->{Log} && $app->config->{Log}->{level}) {
58 my $loglv = $app->config->{Log}->{level};
59 if (grep { $loglv eq $_ } qw(debug info warn error fatal)) {
60 $app->log->level($loglv);
63 $app->log->warn('invalid log level given in config file');
67 ############################################################
71 ############################################################
73 # when "test" mode, output debug logs.
74 $app->log->level('debug') if $app->mode eq 'test';
77 # $app->plugin('Newslash::Plugin::RendererHelper');
80 $app->plugin('Newslash::Plugin::AccessLog::Debug', $app->config->{Log} || {});
81 $app->plugin('Newslash::Plugin::AccessLog::LocalFile', $app->config->{Log} || {});
83 # secret key for hasing
84 $app->secrets([$app->config->{System}->{secret_key},]);
86 # Helpers for Newslash
87 $app->plugin('Newslash::Plugin::NewslashHelpers');
90 #$app->config->{_Plugins} = {};
93 $app->plugin('Newslash::Plugin::Epoch');
96 if ($app->config->{BasicAuth} && $app->config->{BasicAuth}->{enable}) {
97 $app->plugin('Newslash::Plugin::BasicAuth');
100 # use TimeLimitedCache ($app->cache)
101 $app->plugin('Newslash::Plugin::TimeLimitedCache');
103 # use KeyValue Store ($app->kvs)
104 $app->plugin('Newslash::Plugin::KeyValueStore');
107 my $model_opts = $app->config;
108 $model_opts->{Logger} = $app->log;
109 $app->helper(model => Newslash::Model::loader($model_opts));
110 Newslash::Model::startup($model_opts, $app);
112 # use Model Cache ($app->model_cache)
113 $app->plugin('Newslash::Plugin::ModelCache');
115 # use Template::Toolkit 2 render
116 $app->plugin('Newslash::Plugin::TT2Renderer');
119 #$app->plugin('Newslash::Plugin::ViewFunctions');
122 $app->plugin('Newslash::Plugin::CustomBoxes');
124 # use Analytics helper
125 $app->plugin('Newslash::Plugin::GoogleAnalytics');
127 # user AntiCsrf ($app->anti_csrf)
128 if ($app->mode ne 'test') {
129 # when test mode, disable AntiCsrf.
130 $app->plugin('Newslash::Plugin::AntiCsrf');
133 # contents preprocessor
134 $app->plugin('Newslash::Plugin::Preprocessor');
137 $app->plugin('Newslash::Plugin::JavaScriptLoader');
140 $app->plugin('Newslash::Plugin::UserAuth');
143 $app->plugin('Newslash::Plugin::AccessControl');
146 if ($app->mode ne 'test') {
147 $app->plugin('Newslash::Plugin::ReCaptcha');
150 # set canocal (for test.srad.jp)
151 $app->plugin('Newslash::Plugin::Canonical');
154 $app->plugin('Newslash::Plugin::DiscussionHelper');
157 $app->plugin('Newslash::Plugin::Hsts');
160 $app->plugin('Newslash::Plugin::EventQue');
163 $app->plugin('Newslash::Plugin::Statics');
165 # Request Body based routing condition
166 $app->plugin('Newslash::Plugin::RequestBodyCondition');
169 $app->plugin('Newslash::Plugin::NSRPC');
172 $app->plugin('Newslash::Plugin::ADRenderer');
174 ############################################################
176 # Generate site-global used javascript file
178 ############################################################
179 my $templ_name = "common/siteconfig.js";
180 my $mod_reasons = $app->model('moderations')->reasons();
181 my $topics = $app->model('tags')->get_topics;
182 my @acl2_types = $app->model('users')->acl2_types;
184 for my $topic (@$topics) {
185 my $lc_keyword = lc($topic->{keyword});
186 my $lc_textname = lc($topic->{textname});
187 $keywords->{$lc_keyword} = {keyword => $topic->{keyword},
188 textname => $topic->{textname},
189 image => $topic->{image}};
190 if ($lc_keyword ne $lc_textname) {
191 $keywords->{$lc_textname} = $keywords->{$lc_keyword};
195 moderate_reasons => $mod_reasons,
197 acl2_types => \@acl2_types,
199 my $siteconfig = $app->tt2renderer->render($templ_name, $vars);
200 $app->preprocessor->add_content("js/siteconfig.js", $siteconfig, "text/javascript; charset=utf-8");
202 ############################################################
206 ############################################################
208 my $r = $app->routes;
211 $r->get('/')->to('timeline#stories');
212 $r->get('/recent')->to('timeline#recent');
213 $r->get('/popular')->to('timeline#popular');
214 $r->get('/comments')->to('timeline#comments');
215 $r->get('/journals')->to('timeline#journals');
216 $r->get('/submissions')->to('timeline#submissions');
217 $r->get('/polls')->to('timeline#polls');
219 # siteconfig.js for global settings
220 $r->get('/siteconfig/:epoch/siteconfig.js')->to('site_config#site_config');
223 $r->get('/banned')->to('index#banned', noindex => 1);
226 $r->get('/login')->to('login#login');
227 $r->post('/login')->to('login#login');
228 $r->get('/logout')->to('login#logout');
231 $r->get('/my/newuser')->to('login#newuser');
232 $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
235 $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
236 ->to('story#single');
239 $r->get('/comment/:cid/')->to('comment#single');
242 $r->get('/journal/new')->to('journal#create', seclev => 1);
243 $r->get('/journal/:id/')->to('journal#single');
246 $r->get('/submission/new')->to('submission#create');
247 $r->get('/submission/:id/')->to('submission#single');
248 #$r->post('/submission')->to('submission#create');
251 $r->get('/poll/:qid')->to('poll#single');
252 $r->get('/vote/:qid')->to('poll#vote');
253 $r->post('/vote/:qid')->to('poll#vote_post', csrf_check_id => 'vote');
256 $r->get('/story/:year/:month/:day/')->to('archive#story');
257 $r->get('/story/:year/:month/')->to('archive#story');
258 $r->get('/story/')->to('archive#story');
260 $r->get('/journal/:year/:month/:day/')->to('archive#journal');
261 $r->get('/journal/:year/:month/')->to('archive#journal');
262 $r->get('/journal/')->to('archive#journal');
264 $r->get('/submission/:year/:month/:day/')->to('archive#submission');
265 $r->get('/submission/:year/:month/')->to('archive#submission');
266 $r->get('/submission/')->to('archive#submission');
268 $r->get('/poll/:year/:month/')->to('archive#poll');
269 $r->get('/poll/')->to('archive#poll');
272 $r->get('/tag/:tagname/:type/')->to('tag#list_tagged_items');
273 $r->get('/tag/:tagname/')->to('tag#list_tagged_items');
274 $r->get('/tag/')->to('tag#list_tags');
277 $r->get('/my/settings')->to('user#settings', seclev => 1);
278 $r->get('/my/sidebar')->to('user#sidebar', seclev => 1);
279 $r->get('/my/messages')->to('my#messages', seclev => 1);
280 $r->get('/my/')->to('user#home', seclev => 1);
283 $r->get('/search')->to('search#search');
286 # pages under /admin needs seclev equal or greater than 10000;
287 my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
289 $admin->get('/firehose/:id/')->to('admin-firehose#single');
290 $admin->get('/submissions')->to('admin-submissions#index');
292 $admin->get('/css')->to('admin-css#edit');
293 $admin->get('/story/edit')->to('admin-story#edit');
295 $admin->get('/users')->to('admin-users#index');
297 $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
299 $admin->get('/sidebar')->to('admin-sidebar#index');
300 $admin->get('/feed')->to('admin-feed#index');
301 $admin->get('/blocking')->to('admin-blocking#index');
302 $admin->get('/ad')->to('admin-ads#index');
304 $admin->get('/repository')->to('admin-repository#index');
305 $admin->get('/cache')->to('admin-config#cache');
308 # pages under /api/v1/admin needs seclev equal or greater than 10000;
309 my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
310 $admin_api->get('/feed')->to('API::Admin::Feed#get');
311 $admin_api->post('/feed')->to('API::Admin::Feed#post');
312 $admin_api->get('/blocking')->to('API::Admin::Blocking#get');
313 $admin_api->post('/blocking')->to('API::Admin::Blocking#post');
315 $admin_api->get('/repository/export')->to('API::Admin::Repository#export');
316 $admin_api->get('/repository/import')->to('API::Admin::Repository#import');
318 $admin_api->post('/sidebar')->to('API::Admin::Sidebar#post');
319 $admin_api->get('/sidebar')->to('API::Admin::Sidebar#get');
321 # ad codes management
322 $app->rpc->route_to_model($admin_api->get('/ad/code/'), 'ad_codes', 'select');
323 $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "create"}), 'ad_codes', 'create');
324 $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "update"}), 'ad_codes', 'update');
325 $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "delete"}), 'ad_codes', 'delete');
328 my $api = $r->under('/api/v1');
329 $api->post('/login')->to('API::Login#login');
331 $api->get('/sidebar/item')->to('API::SidebarItem#get', seclev => 1);
333 $api->get('/comment')->to('API::Comment#get');
334 $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
336 $api->get('/user')->to('API::User#get');
337 $api->post('/user')->to('API::User#post', seclev => 1);
339 $api->get('/journal')->to('API::Journal#get');
340 $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
342 $api->get('/submission')->to('API::Submission#get');
343 $api->get('/submissions')->to('API::Submission#list');
344 $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
346 $api->get('/story')->to('API::Story#get');
347 $api->post('/story')->to('API::Story#post');
349 $api->get('/poll')->to('API::Poll#get');
350 $api->post('/poll')->to('API::Poll#post');
351 $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
353 $api->get('/moderation')->to('API::Moderation#get');
354 $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
356 $api->get('/metamoderation')->to('API::Metamoderation#get');
357 $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
359 $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
361 $api->get('/token')->to('API::Token#get');
364 # warning: these pathes uses regexp matching, so must write in tail of route definitions.
365 my $user = $r->under('/:nickname');
366 $user->get('/' => [nickname => qr/~.*/])->to('user#home');
367 $user->get('/journals' => [nickname => qr/~.*/])->to('user#journals');
368 $user->get('/journal' => [nickname => qr/~.*/])->to('user#journals'); # for compatibility
369 $user->get('/comments' => [nickname => qr/~.*/])->to('user#comments');
370 $user->get('/submissions' => [nickname => qr/~.*/])->to('user#submissions');
371 $user->get('/friends' => [nickname => qr/~.*/])->to('user#friends');
372 $user->get('/foes' => [nickname => qr/~.*/])->to('user#foes');
373 $user->get('/fans' => [nickname => qr/~.*/])->to('user#fans');
374 $user->get('/freaks' => [nickname => qr/~.*/])->to('user#freaks');
375 #$user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
376 #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');