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}->{level}) {
26 my $loglv = $app->config->{Log}->{level};
27 if (grep { $loglv eq $_ } qw(debug info warn error fatal)) {
28 $app->log->level($loglv);
31 $app->log->warn('invalid log level given in config file');
35 ############################################################
39 ############################################################
41 # when "test" mode, output debug logs.
42 $app->log->level('debug') if $app->mode eq 'test';
45 $app->plugin('Newslash::Plugin::AccessLog::Debug', $app->config->{Log} || {});
46 $app->plugin('Newslash::Plugin::AccessLog::LocalFile', $app->config->{Log} || {});
48 # secret key for hasing
49 $app->secrets([$app->config->{System}->{secret_key},]);
52 #$app->config->{_Plugins} = {};
55 if ($app->config->{BasicAuth} && $app->config->{BasicAuth}->{enable}) {
56 $app->plugin('Newslash::Plugin::BasicAuth');
59 # use TimeLimitedCache ($app->cache)
60 $app->plugin('Newslash::Plugin::TimeLimitedCache');
62 # use KeyValue Store ($app->kvs)
63 $app->plugin('Newslash::Plugin::KeyValueStore');
66 my $model_opts = $app->config;
67 $model_opts->{Logger} = $app->log;
68 $app->helper(model => Newslash::Model::loader($model_opts));
69 Newslash::Model::startup($model_opts, $app);
71 # use Model Cache ($app->model_cache)
72 $app->plugin('Newslash::Plugin::ModelCache');
74 # use Template::Toolkit 2 render
75 $app->plugin('Newslash::Plugin::TT2Renderer');
77 # user AntiCsrf ($app->anti_csrf)
78 if ($app->mode ne 'test') {
79 # when test mode, disable AntiCsrf.
80 $app->plugin('Newslash::Plugin::AntiCsrf');
84 #$app->plugin('Newslash::Plugin::CSSCompile');
86 # quasi-static content
87 $app->plugin('Newslash::Plugin::QuasiStaticContent');
90 $app->plugin('Newslash::Plugin::UserAuth');
93 $app->plugin('Newslash::Plugin::AccessControl');
96 if ($app->mode ne 'test') {
97 $app->plugin('Newslash::Plugin::ReCaptcha');
101 $app->plugin('Newslash::Plugin::EventQue');
104 ############################################################
106 # Generate site-global used javascript file
108 ############################################################
109 my $templ_name = "common/siteconfig.js";
110 my $mod_reasons = $app->model('moderations')->reasons();
111 my $topics = $app->model('tags')->get_topics;
112 my @acl2_types = $app->model('users')->acl2_types;
114 for my $topic (@$topics) {
115 my $lc_keyword = lc($topic->{keyword});
116 my $lc_textname = lc($topic->{textname});
117 $keywords->{$lc_keyword} = {keyword => $topic->{keyword},
118 textname => $topic->{textname},
119 image => $topic->{image}};
120 if ($lc_keyword ne $lc_textname) {
121 $keywords->{$lc_textname} = $keywords->{$lc_keyword};
125 moderate_reasons => $mod_reasons,
127 acl2_types => \@acl2_types,
129 my $siteconfig = $app->tt2renderer->render($templ_name, $vars);
130 $app->static_content->add_content("js/siteconfig.js", $siteconfig, "text/javascript; charset=utf-8");
132 ############################################################
136 ############################################################
138 my $r = $app->routes;
141 $r->get('/')->to('index#root');
142 $r->get('/recent')->to('timeline#recent');
143 $r->get('/journals')->to('index#journals');
144 $r->get('/submissions')->to('index#submissions');
147 $r->get('/comments')->to('comment#index');
150 $r->get('/banned')->to('index#banned', noindex => 1);
153 $r->get('/login')->to('login#login');
154 $r->post('/login')->to('login#login');
155 $r->get('/logout')->to('login#logout');
158 $r->get('/my/newuser')->to('login#newuser');
159 $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
162 $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
166 $r->get('/journal/new')->to('journal#create', seclev => 1);
167 $r->get('/journal/:id/')->to('journal#journal');
170 $r->get('/submission/new')->to('submission#create');
171 $r->get('/submission/:id/')->to('submission#submission');
172 #$r->post('/submission')->to('submission#create');
175 $r->get('/poll/:qid')->to('poll#single');
176 $r->get('/vote/:qid')->to('poll#vote');
177 $r->get('/polls')->to('poll#index');
178 $r->post('/vote/:qid')->to('poll#post', csrf_check_id => 'vote');
181 $r->get('/story/:year/:month/:day/')->to('archive#story');
182 $r->get('/story/:year/:month/')->to('archive#story');
183 $r->get('/story/:year/')->to('archive#story');
184 $r->get('/story/')->to('archive#story');
185 $r->get('/journal/:year/:month/:day/')->to('archive#journal');
186 $r->get('/journal/:year/:month/')->to('archive#journal');
187 $r->get('/journal/:year/')->to('archive#journal');
188 $r->get('/journal/')->to('archive#journal');
190 $r->get('/journal/:year/:month/:day/' => [year => qr/[0-9]{2}/,
191 month => qr/[0-9]{2}/,
192 day => qr/[0-9]{2}/])->to('archive#journal');
194 $r->get('/comment/:year/:month/:day/' => [year => qr/[0-9]{2}/,
195 month => qr/[0-9]{2}/,
196 day => qr/[0-9]{2}/])->to('archive#comment');
198 $r->get('/submission/:year/:month/:day/' => [year => qr/[0-9]{2}/,
199 month => qr/[0-9]{2}/,
200 day => qr/[0-9]{2}/])->to('archive#submission');
204 $r->get('/my/settings')->to('user#settings', seclev => 1);
205 $r->get('/my/sidebar')->to('user#sidebar', seclev => 1);
206 $r->get('/my/messages')->to('my#messages', seclev => 1);
207 $r->get('/my/')->to('user#home', seclev => 1);
210 # pages under /admin needs seclev equal or greater than 10000;
211 my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
213 $admin->get('/submissions')->to('admin-submissions#index');
214 $admin->get('/submissions/list')->to('admin-submissions#list');
216 $admin->get('/css')->to('admin-css#edit');
217 $admin->get('/story/edit')->to('admin-story#edit');
219 $admin->get('/users')->to('admin-users#index');
221 $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
223 $admin->get('/sidebar')->to('admin-sidebar#index');
224 $admin->post('/sidebar/update')->to('admin-sidebar#update');
225 $admin->post('/sidebar/delete')->to('admin-sidebar#delete');
226 $admin->any(['GET', 'POST'] => '/sidebar/list')->to('admin-sidebar#list');
227 $admin->post('/sidebar/get')->to('admin-sidebar#get');
229 $admin->get('/feed')->to('admin-feed#index');
230 $admin->get('/blocking')->to('admin-blocking#index');
233 # pages under /api/v1/admin needs seclev equal or greater than 10000;
234 my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
235 $admin_api->get('/feed')->to('API::Admin::Feed#get');
236 $admin_api->post('/feed')->to('API::Admin::Feed#post');
237 $admin_api->get('/contents/blocked')->to('API::Admin::Contents::Blocked#get');
238 $admin_api->post('/contents/blocked')->to('API::Admin::Contents::Blocked#post');
241 my $api = $r->under('/api/v1');
242 $api->get('/sidebars')->to('admin-sidebar#list');
244 $api->post('/login')->to('API::Login#login');
246 $api->get('/comment')->to('API::Comment#get');
247 $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
249 $api->get('/user')->to('API::User#get');
250 $api->post('/user')->to('API::User#post', seclev => 1);
252 $api->get('/journal')->to('API::Journal#get');
253 $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
255 $api->get('/submission')->to('API::Submission#get');
256 $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
258 $api->get('/story')->to('API::Story#get');
259 $api->post('/story')->to('API::Story#post');
261 $api->get('/poll')->to('API::Poll#get');
262 $api->post('/poll')->to('API::Poll#post');
263 $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
265 $api->get('/moderation')->to('API::Moderation#get');
266 $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
268 $api->get('/metamoderation')->to('API::Metamoderation#get');
269 $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
271 $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
273 $api->get('/token')->to('API::Token#get');
276 # warning: these pathes uses regexp matching, so must write in tail of route definitions.
277 my $user = $r->under('/:nickname');
278 $user->get('/' => [nickname => qr/~.*/])->to('user#home');
279 $user->get('/journals' => [nickname => qr/~.*/])->to('user#journals');
280 $user->get('/comments' => [nickname => qr/~.*/])->to('user#comments');
281 $user->get('/submissions' => [nickname => qr/~.*/])->to('user#submissions');
282 $user->get('/friends' => [nickname => qr/~.*/])->to('user#friends');
283 $user->get('/foes' => [nickname => qr/~.*/])->to('user#foes');
284 $user->get('/fans' => [nickname => qr/~.*/])->to('user#fans');
285 $user->get('/freaks' => [nickname => qr/~.*/])->to('user#freaks');
286 $user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
287 #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');