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},]);
87 #$app->config->{_Plugins} = {};
90 $app->plugin('Newslash::Plugin::Epoch');
93 if ($app->config->{BasicAuth} && $app->config->{BasicAuth}->{enable}) {
94 $app->plugin('Newslash::Plugin::BasicAuth');
97 # use TimeLimitedCache ($app->cache)
98 $app->plugin('Newslash::Plugin::TimeLimitedCache');
100 # use KeyValue Store ($app->kvs)
101 $app->plugin('Newslash::Plugin::KeyValueStore');
104 my $model_opts = $app->config;
105 $model_opts->{Logger} = $app->log;
106 $app->helper(model => Newslash::Model::loader($model_opts));
107 Newslash::Model::startup($model_opts, $app);
109 # use Model Cache ($app->model_cache)
110 $app->plugin('Newslash::Plugin::ModelCache');
112 # use Template::Toolkit 2 render
113 $app->plugin('Newslash::Plugin::TT2Renderer');
116 $app->plugin('Newslash::Plugin::ViewFunctions');
119 $app->plugin('Newslash::Plugin::CustomBoxes');
121 # use Analytics helper
122 $app->plugin('Newslash::Plugin::GoogleAnalytics');
124 # user AntiCsrf ($app->anti_csrf)
125 if ($app->mode ne 'test') {
126 # when test mode, disable AntiCsrf.
127 $app->plugin('Newslash::Plugin::AntiCsrf');
131 #$app->plugin('Newslash::Plugin::CSSCompile');
133 # quasi-static content
134 $app->plugin('Newslash::Plugin::QuasiStaticContent');
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 ############################################################
167 # Generate site-global used javascript file
169 ############################################################
170 my $templ_name = "common/siteconfig.js";
171 my $mod_reasons = $app->model('moderations')->reasons();
172 my $topics = $app->model('tags')->get_topics;
173 my @acl2_types = $app->model('users')->acl2_types;
175 for my $topic (@$topics) {
176 my $lc_keyword = lc($topic->{keyword});
177 my $lc_textname = lc($topic->{textname});
178 $keywords->{$lc_keyword} = {keyword => $topic->{keyword},
179 textname => $topic->{textname},
180 image => $topic->{image}};
181 if ($lc_keyword ne $lc_textname) {
182 $keywords->{$lc_textname} = $keywords->{$lc_keyword};
186 moderate_reasons => $mod_reasons,
188 acl2_types => \@acl2_types,
190 my $siteconfig = $app->tt2renderer->render($templ_name, $vars);
191 $app->static_content->add_content("js/siteconfig.js", $siteconfig, "text/javascript; charset=utf-8");
193 ############################################################
197 ############################################################
199 my $r = $app->routes;
202 $r->get('/')->to('timeline#stories');
203 $r->get('/recent')->to('timeline#recent');
204 $r->get('/popular')->to('timeline#popular');
205 $r->get('/comments')->to('timeline#comments');
206 $r->get('/journals')->to('timeline#journals');
207 $r->get('/submissions')->to('timeline#submissions');
208 $r->get('/polls')->to('timeline#polls');
210 # siteconfig.js for global settings
211 $r->get('/siteconfig/:epoch/siteconfig.js')->to('site_config#site_config');
214 $r->get('/banned')->to('index#banned', noindex => 1);
217 $r->get('/login')->to('login#login');
218 $r->post('/login')->to('login#login');
219 $r->get('/logout')->to('login#logout');
222 $r->get('/my/newuser')->to('login#newuser');
223 $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
226 $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
227 ->to('story#single');
230 $r->get('/comment/:cid/')->to('comment#single');
233 $r->get('/journal/new')->to('journal#create', seclev => 1);
234 $r->get('/journal/:id/')->to('journal#single');
237 $r->get('/submission/new')->to('submission#create');
238 $r->get('/submission/:id/')->to('submission#single');
239 #$r->post('/submission')->to('submission#create');
242 $r->get('/poll/:qid')->to('poll#single');
243 $r->get('/vote/:qid')->to('poll#vote');
244 $r->post('/vote/:qid')->to('poll#vote_post', csrf_check_id => 'vote');
247 $r->get('/story/:year/:month/:day/')->to('archive#story');
248 $r->get('/story/:year/:month/')->to('archive#story');
249 $r->get('/story/')->to('archive#story');
251 $r->get('/journal/:year/:month/:day/')->to('archive#journal');
252 $r->get('/journal/:year/:month/')->to('archive#journal');
253 $r->get('/journal/')->to('archive#journal');
255 $r->get('/submission/:year/:month/:day/')->to('archive#submission');
256 $r->get('/submission/:year/:month/')->to('archive#submission');
257 $r->get('/submission/')->to('archive#submission');
259 $r->get('/poll/:year/:month/')->to('archive#poll');
260 $r->get('/poll/')->to('archive#poll');
263 $r->get('/tag/:tagname/:type/')->to('tag#list_tagged_items');
264 $r->get('/tag/:tagname/')->to('tag#list_tagged_items');
265 $r->get('/tag/')->to('tag#list_tags');
268 $r->get('/my/settings')->to('user#settings', seclev => 1);
269 $r->get('/my/sidebar')->to('user#sidebar', seclev => 1);
270 $r->get('/my/messages')->to('my#messages', seclev => 1);
271 $r->get('/my/')->to('user#home', seclev => 1);
274 $r->get('/search')->to('search#search');
277 # pages under /admin needs seclev equal or greater than 10000;
278 my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
280 $admin->get('/firehose/:id/')->to('admin-firehose#single');
281 $admin->get('/submissions')->to('admin-submissions#index');
283 $admin->get('/css')->to('admin-css#edit');
284 $admin->get('/story/edit')->to('admin-story#edit');
286 $admin->get('/users')->to('admin-users#index');
288 $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
290 $admin->get('/sidebar')->to('admin-sidebar#index');
291 $admin->get('/feed')->to('admin-feed#index');
292 $admin->get('/blocking')->to('admin-blocking#index');
293 $admin->get('/ad')->to('admin-ads#index');
295 $admin->get('/repository')->to('admin-repository#index');
298 # pages under /api/v1/admin needs seclev equal or greater than 10000;
299 my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
300 $admin_api->get('/feed')->to('API::Admin::Feed#get');
301 $admin_api->post('/feed')->to('API::Admin::Feed#post');
302 $admin_api->get('/blocking')->to('API::Admin::Blocking#get');
303 $admin_api->post('/blocking')->to('API::Admin::Blocking#post');
305 $admin_api->get('/repository/export')->to('API::Admin::Repository#export');
306 $admin_api->get('/repository/import')->to('API::Admin::Repository#import');
308 $admin_api->post('/sidebar')->to('API::Admin::Sidebar#post');
309 $admin_api->get('/sidebar')->to('API::Admin::Sidebar#get');
312 my $api = $r->under('/api/v1');
313 $api->post('/login')->to('API::Login#login');
315 $api->get('/sidebar/item')->to('API::SidebarItem#get', seclev => 1);
317 $api->get('/comment')->to('API::Comment#get');
318 $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
320 $api->get('/user')->to('API::User#get');
321 $api->post('/user')->to('API::User#post', seclev => 1);
323 $api->get('/journal')->to('API::Journal#get');
324 $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
326 $api->get('/submission')->to('API::Submission#get');
327 $api->get('/submissions')->to('API::Submission#list');
328 $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
330 $api->get('/story')->to('API::Story#get');
331 $api->post('/story')->to('API::Story#post');
333 $api->get('/poll')->to('API::Poll#get');
334 $api->post('/poll')->to('API::Poll#post');
335 $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
337 $api->get('/moderation')->to('API::Moderation#get');
338 $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
340 $api->get('/metamoderation')->to('API::Metamoderation#get');
341 $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
343 $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
345 $api->get('/token')->to('API::Token#get');
348 # warning: these pathes uses regexp matching, so must write in tail of route definitions.
349 my $user = $r->under('/:nickname');
350 $user->get('/' => [nickname => qr/~.*/])->to('user#home');
351 $user->get('/journals' => [nickname => qr/~.*/])->to('user#journals');
352 $user->get('/journal' => [nickname => qr/~.*/])->to('user#journals'); # for compatibility
353 $user->get('/comments' => [nickname => qr/~.*/])->to('user#comments');
354 $user->get('/submissions' => [nickname => qr/~.*/])->to('user#submissions');
355 $user->get('/friends' => [nickname => qr/~.*/])->to('user#friends');
356 $user->get('/foes' => [nickname => qr/~.*/])->to('user#foes');
357 $user->get('/fans' => [nickname => qr/~.*/])->to('user#fans');
358 $user->get('/freaks' => [nickname => qr/~.*/])->to('user#freaks');
359 #$user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
360 #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');