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
25 if ($app->config->{Log} && $app->config->{Log}->{system_log}) {
26 # check log is writable
27 my $pathname = $app->config->{Log}->{system_log};
30 $app->log->debug("logs will be outputed to $pathname ...");
31 $app->log(Mojo::Log->new);
32 $app->log->path($pathname);
35 $app->log->error("cannot write system log to file: $pathname");
39 if (open(my $fh, ">", $pathname)) {
41 $app->log->debug("logs will be outputed to $pathname ...");
42 $app->log(Mojo::Log->new);
43 $app->log->path($pathname);
46 $app->log->error("cannot create system log file: $pathname");
52 if ($app->config->{Log} && $app->config->{Log}->{level}) {
53 my $loglv = $app->config->{Log}->{level};
54 if (grep { $loglv eq $_ } qw(debug info warn error fatal)) {
55 $app->log->level($loglv);
58 $app->log->warn('invalid log level given in config file');
62 ############################################################
66 ############################################################
68 # when "test" mode, output debug logs.
69 $app->log->level('debug') if $app->mode eq 'test';
72 $app->plugin('Newslash::Plugin::AccessLog::Debug', $app->config->{Log} || {});
73 $app->plugin('Newslash::Plugin::AccessLog::LocalFile', $app->config->{Log} || {});
75 # secret key for hasing
76 $app->secrets([$app->config->{System}->{secret_key},]);
79 #$app->config->{_Plugins} = {};
82 if ($app->config->{BasicAuth} && $app->config->{BasicAuth}->{enable}) {
83 $app->plugin('Newslash::Plugin::BasicAuth');
86 # use TimeLimitedCache ($app->cache)
87 $app->plugin('Newslash::Plugin::TimeLimitedCache');
89 # use KeyValue Store ($app->kvs)
90 $app->plugin('Newslash::Plugin::KeyValueStore');
93 my $model_opts = $app->config;
94 $model_opts->{Logger} = $app->log;
95 $app->helper(model => Newslash::Model::loader($model_opts));
96 Newslash::Model::startup($model_opts, $app);
98 # use Model Cache ($app->model_cache)
99 $app->plugin('Newslash::Plugin::ModelCache');
101 # use Template::Toolkit 2 render
102 $app->plugin('Newslash::Plugin::TT2Renderer');
104 # use Analytics helper
105 $app->plugin('Newslash::Plugin::GoogleAnalytics');
107 # user AntiCsrf ($app->anti_csrf)
108 if ($app->mode ne 'test') {
109 # when test mode, disable AntiCsrf.
110 $app->plugin('Newslash::Plugin::AntiCsrf');
114 #$app->plugin('Newslash::Plugin::CSSCompile');
116 # quasi-static content
117 $app->plugin('Newslash::Plugin::QuasiStaticContent');
120 $app->plugin('Newslash::Plugin::UserAuth');
123 $app->plugin('Newslash::Plugin::AccessControl');
126 if ($app->mode ne 'test') {
127 $app->plugin('Newslash::Plugin::ReCaptcha');
130 # set canocal (for test.srad.jp)
131 $app->plugin('Newslash::Plugin::Canonical');
134 $app->plugin('Newslash::Plugin::Hsts');
137 $app->plugin('Newslash::Plugin::EventQue');
140 $app->plugin('Newslash::Plugin::Statics');
142 ############################################################
144 # Generate site-global used javascript file
146 ############################################################
147 my $templ_name = "common/siteconfig.js";
148 my $mod_reasons = $app->model('moderations')->reasons();
149 my $topics = $app->model('tags')->get_topics;
150 my @acl2_types = $app->model('users')->acl2_types;
152 for my $topic (@$topics) {
153 my $lc_keyword = lc($topic->{keyword});
154 my $lc_textname = lc($topic->{textname});
155 $keywords->{$lc_keyword} = {keyword => $topic->{keyword},
156 textname => $topic->{textname},
157 image => $topic->{image}};
158 if ($lc_keyword ne $lc_textname) {
159 $keywords->{$lc_textname} = $keywords->{$lc_keyword};
163 moderate_reasons => $mod_reasons,
165 acl2_types => \@acl2_types,
167 my $siteconfig = $app->tt2renderer->render($templ_name, $vars);
168 $app->static_content->add_content("js/siteconfig.js", $siteconfig, "text/javascript; charset=utf-8");
170 ############################################################
174 ############################################################
176 my $r = $app->routes;
179 $r->get('/')->to('timeline#stories');
180 $r->get('/recent')->to('timeline#recent');
181 $r->get('/popular')->to('timeline#popular');
182 $r->get('/comments')->to('timeline#comments');
183 $r->get('/journals')->to('timeline#journals');
184 $r->get('/submissions')->to('timeline#submissions');
185 $r->get('/polls')->to('timeline#polls');
188 $r->get('/banned')->to('index#banned', noindex => 1);
191 $r->get('/login')->to('login#login');
192 $r->post('/login')->to('login#login');
193 $r->get('/logout')->to('login#logout');
196 $r->get('/my/newuser')->to('login#newuser');
197 $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
200 $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
204 $r->get('/comment/:cid/')->to('comment#single');
207 $r->get('/journal/new')->to('journal#create', seclev => 1);
208 $r->get('/journal/:id/')->to('journal#journal');
211 $r->get('/submission/new')->to('submission#create');
212 $r->get('/submission/:id/')->to('submission#submission');
213 #$r->post('/submission')->to('submission#create');
216 $r->get('/poll/:qid')->to('poll#single');
217 $r->get('/vote/:qid')->to('poll#vote');
218 $r->get('/polls')->to('poll#index');
219 $r->post('/vote/:qid')->to('poll#post', csrf_check_id => 'vote');
222 $r->get('/story/:year/:month/:day/')->to('archive#story');
223 $r->get('/story/:year/:month/')->to('archive#story');
224 $r->get('/story/')->to('archive#story');
226 $r->get('/journal/:year/:month/:day/')->to('archive#journal');
227 $r->get('/journal/:year/:month/')->to('archive#journal');
228 $r->get('/journal/')->to('archive#journal');
230 $r->get('/submission/:year/:month/:day/')->to('archive#submission');
231 $r->get('/submission/:year/:month/')->to('archive#submission');
232 $r->get('/submission/')->to('archive#submission');
234 $r->get('/poll/:year/:month/')->to('archive#poll');
235 $r->get('/poll/')->to('archive#poll');
238 $r->get('/my/settings')->to('user#settings', seclev => 1);
239 $r->get('/my/sidebar')->to('user#sidebar', seclev => 1);
240 $r->get('/my/messages')->to('my#messages', seclev => 1);
241 $r->get('/my/')->to('user#home', seclev => 1);
244 # pages under /admin needs seclev equal or greater than 10000;
245 my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
247 $admin->get('/firehose/:id/')->to('admin-firehose#single');
248 $admin->get('/submissions')->to('admin-submissions#index');
250 $admin->get('/css')->to('admin-css#edit');
251 $admin->get('/story/edit')->to('admin-story#edit');
253 $admin->get('/users')->to('admin-users#index');
255 $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
257 $admin->get('/sidebar')->to('admin-sidebar#index');
258 $admin->get('/feed')->to('admin-feed#index');
259 $admin->get('/blocking')->to('admin-blocking#index');
261 $admin->get('/repository')->to('admin-repository#index');
264 # pages under /api/v1/admin needs seclev equal or greater than 10000;
265 my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
266 $admin_api->get('/feed')->to('API::Admin::Feed#get');
267 $admin_api->post('/feed')->to('API::Admin::Feed#post');
268 $admin_api->get('/blocking')->to('API::Admin::Blocking#get');
269 $admin_api->post('/blocking')->to('API::Admin::Blocking#post');
271 $admin_api->get('/repository/export')->to('API::Admin::Repository#export');
272 $admin_api->get('/repository/import')->to('API::Admin::Repository#import');
274 $admin_api->post('/sidebar')->to('API::Admin::Sidebar#post');
275 $admin_api->get('/sidebar')->to('API::Admin::Sidebar#get');
278 my $api = $r->under('/api/v1');
279 $api->post('/login')->to('API::Login#login');
281 $api->get('/comment')->to('API::Comment#get');
282 $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
284 $api->get('/user')->to('API::User#get');
285 $api->post('/user')->to('API::User#post', seclev => 1);
287 $api->get('/journal')->to('API::Journal#get');
288 $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
290 $api->get('/submission')->to('API::Submission#get');
291 $api->get('/submissions')->to('API::Submission#list');
292 $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
294 $api->get('/story')->to('API::Story#get');
295 $api->post('/story')->to('API::Story#post');
297 $api->get('/poll')->to('API::Poll#get');
298 $api->post('/poll')->to('API::Poll#post');
299 $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
301 $api->get('/moderation')->to('API::Moderation#get');
302 $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
304 $api->get('/metamoderation')->to('API::Metamoderation#get');
305 $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
307 $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
309 $api->get('/token')->to('API::Token#get');
312 # warning: these pathes uses regexp matching, so must write in tail of route definitions.
313 my $user = $r->under('/:nickname');
314 $user->get('/' => [nickname => qr/~.*/])->to('user#home');
315 $user->get('/journals' => [nickname => qr/~.*/])->to('user#journals');
316 $user->get('/journal' => [nickname => qr/~.*/])->to('user#journals'); # for compatibility
317 $user->get('/comments' => [nickname => qr/~.*/])->to('user#comments');
318 $user->get('/submissions' => [nickname => qr/~.*/])->to('user#submissions');
319 $user->get('/friends' => [nickname => qr/~.*/])->to('user#friends');
320 $user->get('/foes' => [nickname => qr/~.*/])->to('user#foes');
321 $user->get('/fans' => [nickname => qr/~.*/])->to('user#fans');
322 $user->get('/freaks' => [nickname => qr/~.*/])->to('user#freaks');
323 $user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
324 #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');