OSDN Git Service

set cooling time for voting from same ipid
[newslash/newslash.git] / src / newslash_web / lib / Newslash / Web.pm
1 package Newslash::Web;
2 use Mojo::Base 'Mojolicious';
3 use Mojo::Util qw(dumper);
4
5 use Newslash::Model;
6
7 use constant CONFIG_FILE => '/etc/newslash/newslash.conf';
8
9 # This method will run once at server start
10 sub startup {
11     my $app = shift;
12
13     # load config file
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);
17     }
18     else {
19         #$app->plugin('JSONConfig');
20         $app->plugin('Newslash::Plugin::YAMLConfig');
21     }
22     # TODO: load/save configs with database
23
24     # set log file
25     if ($app->config->{Log} && $app->config->{Log}->{system_log}) {
26         # check log is writable
27         my $pathname = $app->config->{Log}->{system_log};
28         if (-e $pathname) {
29             if (-w $pathname) {
30                 $app->log->debug("logs will be outputed to $pathname ...");
31                 $app->log(Mojo::Log->new);
32                 $app->log->path($pathname);
33             }
34             else {
35                 $app->log->error("cannot write system log to file: $pathname");
36             }
37         }
38         else {
39             if (open(my $fh, ">", $pathname)) {
40                 close($fh);
41                 $app->log->debug("logs will be outputed to $pathname ...");
42                 $app->log(Mojo::Log->new);
43                 $app->log->path($pathname);
44             }
45             else {
46                 $app->log->error("cannot create system log file: $pathname");
47             }
48         }
49     }
50
51     # set log level
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);
56         }
57         else {
58             $app->log->warn('invalid log level given in config file');
59         }
60     }
61
62     ############################################################
63     #
64     # Plugin Settings
65     #
66     ############################################################
67
68     # when "test" mode, output debug logs.
69     $app->log->level('debug') if $app->mode eq 'test';
70
71     # enable logging
72     $app->plugin('Newslash::Plugin::AccessLog::Debug', $app->config->{Log} || {});
73     $app->plugin('Newslash::Plugin::AccessLog::LocalFile', $app->config->{Log} || {});
74
75     # secret key for hasing
76     $app->secrets([$app->config->{System}->{secret_key},]);
77
78     # stash for plugins
79     #$app->config->{_Plugins} = {};
80
81     # use BasicAuth?
82     if ($app->config->{BasicAuth} && $app->config->{BasicAuth}->{enable}) {
83         $app->plugin('Newslash::Plugin::BasicAuth');
84     }
85
86     # use TimeLimitedCache ($app->cache)
87     $app->plugin('Newslash::Plugin::TimeLimitedCache');
88
89     # use KeyValue Store ($app->kvs)
90     $app->plugin('Newslash::Plugin::KeyValueStore');
91
92     # add Model Loader
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);
97
98     # use Model Cache ($app->model_cache)
99     $app->plugin('Newslash::Plugin::ModelCache');
100
101     # use Template::Toolkit 2 render
102     $app->plugin('Newslash::Plugin::TT2Renderer');
103
104     # use CustomBoxes
105     $app->plugin('Newslash::Plugin::CustomBoxes');
106
107     # use Analytics helper
108     $app->plugin('Newslash::Plugin::GoogleAnalytics');
109
110     # user AntiCsrf ($app->anti_csrf)
111     if ($app->mode ne 'test') {
112         # when test mode, disable AntiCsrf.
113         $app->plugin('Newslash::Plugin::AntiCsrf');
114     }
115
116     # compile CSS
117     #$app->plugin('Newslash::Plugin::CSSCompile');
118
119     # quasi-static content
120     $app->plugin('Newslash::Plugin::QuasiStaticContent');
121
122     # user authorization
123     $app->plugin('Newslash::Plugin::UserAuth');
124
125     # access control
126     $app->plugin('Newslash::Plugin::AccessControl');
127
128     # ReCaptcha control
129     if ($app->mode ne 'test') {
130         $app->plugin('Newslash::Plugin::ReCaptcha');
131     }
132
133     # set canocal (for test.srad.jp)
134     $app->plugin('Newslash::Plugin::Canonical');
135
136     # use HSTS
137     $app->plugin('Newslash::Plugin::Hsts');
138
139     # Event Que
140     $app->plugin('Newslash::Plugin::EventQue');
141
142     # Statics Logger
143     $app->plugin('Newslash::Plugin::Statics');
144
145     ############################################################
146     #
147     # Generate site-global used javascript file
148     #
149     ############################################################
150     my $templ_name = "common/siteconfig.js";
151     my $mod_reasons = $app->model('moderations')->reasons();
152     my $topics = $app->model('tags')->get_topics;
153     my @acl2_types = $app->model('users')->acl2_types;
154     my $keywords = {};
155     for my $topic (@$topics) {
156         my $lc_keyword = lc($topic->{keyword});
157         my $lc_textname = lc($topic->{textname});
158         $keywords->{$lc_keyword} = {keyword => $topic->{keyword},
159                                     textname => $topic->{textname},
160                                     image => $topic->{image}};
161         if ($lc_keyword ne $lc_textname) {
162             $keywords->{$lc_textname} = $keywords->{$lc_keyword};
163         }
164     }
165     my $vars = {
166                 moderate_reasons => $mod_reasons,
167                 topics => $keywords,
168                 acl2_types => \@acl2_types,
169                };
170     my $siteconfig = $app->tt2renderer->render($templ_name, $vars);
171     $app->static_content->add_content("js/siteconfig.js", $siteconfig, "text/javascript; charset=utf-8");
172
173     ############################################################
174     #
175     # Routing Settings
176     #
177     ############################################################
178
179     my $r = $app->routes;
180
181     # index page
182     $r->get('/')->to('timeline#stories');
183     $r->get('/recent')->to('timeline#recent');
184     $r->get('/popular')->to('timeline#popular');
185     $r->get('/comments')->to('timeline#comments');
186     $r->get('/journals')->to('timeline#journals');
187     $r->get('/submissions')->to('timeline#submissions');
188     $r->get('/polls')->to('timeline#polls');
189
190     # Banned page
191     $r->get('/banned')->to('index#banned', noindex => 1);
192
193     # Login / Logout
194     $r->get('/login')->to('login#login');
195     $r->post('/login')->to('login#login');
196     $r->get('/logout')->to('login#logout');
197
198     # User Register
199     $r->get('/my/newuser')->to('login#newuser');
200     $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
201
202     # story page
203     $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
204       ->to('story#story');
205
206     # comment page
207     $r->get('/comment/:cid/')->to('comment#single');
208
209     # journal page
210     $r->get('/journal/new')->to('journal#create', seclev => 1);
211     $r->get('/journal/:id/')->to('journal#journal');
212
213     # submission page
214     $r->get('/submission/new')->to('submission#create');
215     $r->get('/submission/:id/')->to('submission#submission');
216     #$r->post('/submission')->to('submission#create');
217
218     # polls page
219     $r->get('/poll/:qid')->to('poll#single');
220     $r->get('/vote/:qid')->to('poll#vote');
221     $r->get('/polls')->to('poll#index');
222     $r->post('/vote/:qid')->to('poll#vote_post', csrf_check_id => 'vote');
223
224     # archive page
225     $r->get('/story/:year/:month/:day/')->to('archive#story');
226     $r->get('/story/:year/:month/')->to('archive#story');
227     $r->get('/story/')->to('archive#story');
228
229     $r->get('/journal/:year/:month/:day/')->to('archive#journal');
230     $r->get('/journal/:year/:month/')->to('archive#journal');
231     $r->get('/journal/')->to('archive#journal');
232
233     $r->get('/submission/:year/:month/:day/')->to('archive#submission');
234     $r->get('/submission/:year/:month/')->to('archive#submission');
235     $r->get('/submission/')->to('archive#submission');
236
237     $r->get('/poll/:year/:month/')->to('archive#poll');
238     $r->get('/poll/')->to('archive#poll');
239
240     # my page
241     $r->get('/my/settings')->to('user#settings', seclev => 1);
242     $r->get('/my/sidebar')->to('user#sidebar', seclev => 1);
243     $r->get('/my/messages')->to('my#messages', seclev => 1);
244     $r->get('/my/')->to('user#home', seclev => 1);
245
246     # Admin
247     # pages under /admin needs seclev equal or greater than 10000;
248     my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
249
250     $admin->get('/firehose/:id/')->to('admin-firehose#single');
251     $admin->get('/submissions')->to('admin-submissions#index');
252
253     $admin->get('/css')->to('admin-css#edit');
254     $admin->get('/story/edit')->to('admin-story#edit');
255
256     $admin->get('/users')->to('admin-users#index');
257
258     $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
259
260     $admin->get('/sidebar')->to('admin-sidebar#index');
261     $admin->get('/feed')->to('admin-feed#index');
262     $admin->get('/blocking')->to('admin-blocking#index');
263
264     $admin->get('/repository')->to('admin-repository#index');
265
266     # Admin API
267     # pages under /api/v1/admin needs seclev equal or greater than 10000;
268     my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
269     $admin_api->get('/feed')->to('API::Admin::Feed#get');
270     $admin_api->post('/feed')->to('API::Admin::Feed#post');
271     $admin_api->get('/blocking')->to('API::Admin::Blocking#get');
272     $admin_api->post('/blocking')->to('API::Admin::Blocking#post');
273
274     $admin_api->get('/repository/export')->to('API::Admin::Repository#export');
275     $admin_api->get('/repository/import')->to('API::Admin::Repository#import');
276
277     $admin_api->post('/sidebar')->to('API::Admin::Sidebar#post');
278     $admin_api->get('/sidebar')->to('API::Admin::Sidebar#get');
279
280     # API
281     my $api = $r->under('/api/v1');
282     $api->post('/login')->to('API::Login#login');
283
284     $api->get('/comment')->to('API::Comment#get');
285     $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
286
287     $api->get('/user')->to('API::User#get');
288     $api->post('/user')->to('API::User#post', seclev => 1);
289
290     $api->get('/journal')->to('API::Journal#get');
291     $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
292
293     $api->get('/submission')->to('API::Submission#get');
294     $api->get('/submissions')->to('API::Submission#list');
295     $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
296
297     $api->get('/story')->to('API::Story#get');
298     $api->post('/story')->to('API::Story#post');
299
300     $api->get('/poll')->to('API::Poll#get');
301     $api->post('/poll')->to('API::Poll#post');
302     $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
303
304     $api->get('/moderation')->to('API::Moderation#get');
305     $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
306
307     $api->get('/metamoderation')->to('API::Metamoderation#get');
308     $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
309
310     $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
311
312     $api->get('/token')->to('API::Token#get');
313
314     # user page
315     # warning: these pathes uses regexp matching, so must write in tail of route definitions.
316     my $user = $r->under('/:nickname');
317     $user->get('/'             => [nickname => qr/~.*/])->to('user#home');
318     $user->get('/journals'     => [nickname => qr/~.*/])->to('user#journals');
319     $user->get('/journal'      => [nickname => qr/~.*/])->to('user#journals'); # for compatibility
320     $user->get('/comments'     => [nickname => qr/~.*/])->to('user#comments');
321     $user->get('/submissions'  => [nickname => qr/~.*/])->to('user#submissions');
322     $user->get('/friends'      => [nickname => qr/~.*/])->to('user#friends');
323     $user->get('/foes'         => [nickname => qr/~.*/])->to('user#foes');
324     $user->get('/fans'         => [nickname => qr/~.*/])->to('user#fans');
325     $user->get('/freaks'       => [nickname => qr/~.*/])->to('user#freaks');
326     #$user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
327     #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');
328
329 }
330
331 1;