OSDN Git Service

timeline: add up/down vote feature for journal timeline
[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 use List::Util qw(any);
5
6 use constant CONFIG_FILE => '/etc/newslash/newslash.conf';
7 has subcommand => "";
8
9 # This method will run once at server start
10 sub startup {
11     my $app = shift;
12
13     if ($ARGV[0]) {
14         $app->subcommand($ARGV[0]);
15     }
16
17     # add commands in Newslash::Command
18     push @{$app->commands->namespaces}, 'Newslash::Command';
19
20     # load config file
21     # first, check existence of /etc/newslash.conf
22     if ($app->mode eq 'production' && -e CONFIG_FILE) {
23         $app->plugin('Newslash::Plugin::YAMLConfig', file => CONFIG_FILE);
24     }
25     else {
26         if ($app->subcommand eq "configdump") {
27             $app->plugin('Newslash::Plugin::YAMLConfig', default => {});
28         }
29         else {
30             $app->plugin('Newslash::Plugin::YAMLConfig');
31         }
32     }
33
34     # load default configuration values
35     $app->plugin('Newslash::Plugin::DefaultConfig');
36
37     # TODO: load/save configs with database
38
39     if ($app->config->{Log} && $app->config->{Log}->{backtrace}) {
40         require Carp::Always;
41         Carp::Always->import;
42     }
43
44     # system log config
45     if ($app->config->{Log} && $app->config->{Log}->{system_log}) {
46         my $cnf = $app->config->{Log}->{system_log};
47
48         # set log level
49         if ($cnf->{level}) {
50             my $loglv = $cnf->{level};
51             if (grep { $loglv eq $_ } qw(debug info warn error fatal)) {
52                 $app->log->level($loglv);
53                 $app->log->info("set log level to $loglv");
54             }
55             else {
56                 $app->log->warn('invalid log level given in config file');
57             }
58         }
59
60         # file output settings
61         if ($cnf->{mode} eq "local_file") {
62             my $pathname = $cnf->{local_file};
63             # check log is writable
64             if (!$pathname) {
65                 $app->log->error("cannot write system log to file: filename not given");
66             }
67             elsif (-e $pathname) {
68                 if (-w $pathname) {
69                     $app->log->debug("logs will be outputed to $pathname ...");
70                     $app->log(Mojo::Log->new);
71                     $app->log->path($pathname);
72                 }
73                 else {
74                     $app->log->error("cannot write system log to file: $pathname");
75                 }
76             }
77             else {
78                 if (open(my $fh, ">", $pathname)) {
79                     close($fh);
80                     $app->log->debug("logs will be outputed to $pathname ...");
81                     $app->log(Mojo::Log->new);
82                     $app->log->path($pathname);
83                 }
84                 else {
85                     $app->log->error("cannot create system log file: $pathname");
86                 }
87             }
88         }
89     }
90
91     # add static contents directories
92     if ($app->config->{System} && $app->config->{System}->{static_dir}) {
93         for my $dir (@{$app->config->{System}->{static_dir}}) {
94             push @{$app->static->paths}, $dir;
95         }
96     }
97
98     # secret key for hasing
99     $app->secrets([$app->config->{System}->{secret_key},]);
100
101     # when "test" mode, output debug logs.
102     $app->log->level('debug') if $app->mode eq 'test';
103
104     # check if 'maintenance' mode
105     my $maintenance_mode = 0;
106     if ($app->subcommand
107         && any { $app->subcommand eq $_ } qw[configdump configimport databaseinit
108                                              testdatainsert useradd usermod
109                                            ]) {
110         $maintenance_mode = 1;
111     }
112
113     ############################################################
114     #
115     # Plugin Settings
116     #
117     ############################################################
118
119     # profiler settings
120     if ($app->mode eq 'development'
121         && $app->config->{Profiler}
122         && $app->config->{Profiler}->{enable}) {
123         if ($app->config->{Profiler}->{profiler} eq 'nytprof') {
124             $app->plugin('Mojolicious::Plugin::NYTProf', { nytprof => {} });
125         }
126     }
127
128     # enable access logging
129     $app->plugin('Newslash::Plugin::AccessLog');
130
131     # Helpers for Newslash
132     $app->plugin('Newslash::Plugin::NewslashHelpers');
133
134     # use Epoch
135     $app->plugin('Newslash::Plugin::Epoch');
136
137     # support BasicAuth
138     $app->plugin('Newslash::Plugin::BasicAuth');
139
140     # use TimeLimitedCache ($app->cache)
141     $app->plugin('Newslash::Plugin::TimeLimitedCache');
142
143     # use KeyValue Store ($app->kvs)
144     $app->plugin('Newslash::Plugin::KeyValueStore');
145
146     # add Model Loader
147     $app->plugin('Newslash::Plugin::Model', { bypass_startup => $maintenance_mode });
148
149     # use Easy Cache ($app->ezcache)
150     #$app->plugin('Newslash::Plugin::EasyCache');
151
152     # use CompositeCache Cache ($app->ccache)
153     $app->plugin('Newslash::Plugin::CompositeCache');
154
155     # use Template::Toolkit 2 render
156     $app->plugin('Newslash::Plugin::TT2Renderer');
157
158     # use ResponseFilter (Faculities)
159     $app->plugin('Newslash::Plugin::ResponseFilter');
160
161     # use CustomBoxes
162     $app->plugin('Newslash::Plugin::CustomBoxes');
163
164     # use AntiCsrf
165     $app->plugin('Newslash::Plugin::AntiCsrf');
166
167     # contents preprocessor
168     $app->plugin('Newslash::Plugin::Preprocessor');
169     if ($maintenance_mode) {
170         $app->log->info("bypassing preprocessor...");
171     }
172     else {
173         $app->preprocessor->generate_all;
174     }
175
176     # javascript loader
177     $app->plugin('Newslash::Plugin::JavaScriptLoader');
178     if ($maintenance_mode) {
179         $app->log->info("bypassing javascriptloader...");
180     }
181     else {
182         $app->javascript_loader->load_all_bundles;
183     }
184
185     # user authorization
186     $app->plugin('Newslash::Plugin::UserAuth');
187
188     # access control
189     $app->plugin('Newslash::Plugin::AccessControl');
190
191     # ReCaptcha control
192     $app->plugin('Newslash::Plugin::ReCaptcha');
193
194     # Post Filter
195     $app->plugin('Newslash::Plugin::PostFilter');
196
197     # set canocal (for test.srad.jp)
198     $app->plugin('Newslash::Plugin::Canonical');
199
200     # DiscussionHelper
201     $app->plugin('Newslash::Plugin::DiscussionHelper');
202
203     # use HSTS
204     $app->plugin('Newslash::Plugin::Hsts');
205
206     # Event Que
207     $app->plugin('Newslash::Plugin::EventQue');
208
209     # Stats Logger
210     $app->plugin('Newslash::Plugin::Stats');
211
212     # Request Body based routing condition
213     $app->plugin('Newslash::Plugin::RequestBodyCondition');
214
215     # NS-RPC
216     $app->plugin('Newslash::Plugin::NSRPC');
217
218     # AD renderer
219     $app->plugin('Newslash::Plugin::ADRenderer');
220
221     # HTTP Compression
222     $app->plugin('Newslash::Plugin::HttpCompression');
223
224     # Users helper
225     $app->plugin('Newslash::Plugin::Users');
226
227     # Stories helper
228     $app->plugin('Newslash::Plugin::Stories');
229
230     # Sendmail helper
231     $app->plugin('Newslash::Plugin::Sendmail');
232
233     # Wiki contents reader helper
234     $app->plugin('Newslash::Plugin::WikiContentsReader');
235
236     ############################################################
237     #
238     # Routing Settings
239     #
240     ############################################################
241
242     my $r = $app->routes;
243
244     # index page
245     $r->get('/')->to('timeline#stories');
246     $r->get('/recent')->to('timeline#recent');
247     $r->get('/popular')->to('timeline#popular');
248     $r->get('/comments')->to('timeline#comments');
249     $r->get('/journals')->to('timeline#journals');
250     $r->get('/submissions')->to('timeline#submissions');
251     $r->get('/polls')->to('timeline#polls');
252
253     # RSS
254     $r->get('/:rss_type' => [format => ['rss', 'xml']])->to('rss#stories');
255     $r->get('/journals/rss')->to('rss#journals');
256
257     # Banned page
258     $r->get('/banned')->to('index#banned', noindex => 1);
259
260     # Login / Logout
261     $r->get('/login')->to('login#login');
262     $r->post('/login')->to('login#login');
263     $r->get('/logout')->to('login#logout');
264
265     # story page
266     $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
267       ->to('story#single');
268
269     # comment page
270     $r->get('/comment/:cid/')->to('comment#single');
271
272     # journal page
273     # also see user page settings for /~username/journal/
274     $r->get('/journal/new')->to('journal#create', seclev => 1);
275     $r->get('/journal/:id/')->to('journal#single');
276
277     # submission page
278     $r->get('/submission/new')->to('submission#create');
279     $r->get('/submission/:id/')->to('submission#single');
280     #$r->post('/submission')->to('submission#create');
281
282     # polls page
283     $r->get('/poll/:qid')->to('poll#single');
284     $r->get('/vote/:qid')->to('poll#vote');
285     $r->post('/vote/:qid')->to('poll#vote_post', csrf_check_id => 'vote');
286
287     # archive page
288     $r->get('/story/:year/:month/:day/')->to('archive#story');
289     $r->get('/story/:year/:month/')->to('archive#story');
290     $r->get('/story/')->to('archive#story');
291
292     $r->get('/journal/:year/:month/:day/')->to('archive#journal');
293     $r->get('/journal/:year/:month/')->to('archive#journal');
294     $r->get('/journal/')->to('archive#journal');
295
296     $r->get('/submission/:year/:month/:day/')->to('archive#submission');
297     $r->get('/submission/:year/:month/')->to('archive#submission');
298     $r->get('/submission/')->to('archive#submission');
299
300     $r->get('/comment/:year/:month/:day/')->to('archive#comment');
301     $r->get('/comment/:year/:month/')->to('archive#comment');
302     $r->get('/comment/')->to('archive#comment');
303
304     $r->get('/poll/:year/:month/')->to('archive#poll');
305     $r->get('/poll/')->to('archive#poll');
306
307     # tag page
308     $r->get('/tag/:tagname/:type/')->to('tag#list_tagged_items');
309     $r->get('/tag/:tagname/')->to('tag#list_tagged_items');
310     $r->get('/tag/')->to('tag#list_tags');
311
312     # my page
313     $r->get('/my/settings')->to('my#settings', seclev => 1);
314     $r->get('/my/sidebar')->to('my#sidebar', seclev => 1);
315     $r->get('/my/messages')->to('my#messages', seclev => 1);
316     $r->get('/my/')->to('user#home', seclev => 1);
317
318     # User Register
319     $r->get('/my/newuser')->to('login#newuser');
320     $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
321     $r->get('/my/activation')->to('login#activation');
322     $r->get('/my/resetpassword')->to('login#reset_password');
323     $r->post('/my/resetpassword')->to('login#reset_password', captcha_check => 1);
324
325     # Change Email
326     $r->get('/my/change_email')->to('my#change_email', seclev => 1);
327
328     # search page
329     $r->get('/search')->to('search#search');
330
331     # about
332     $r->get('/about')->to('about#about');
333
334     #faq
335     $r->get('/faq/editorial')->to('faq#editorial');
336     $r->get('/faq/UI')->to('faq#UI');
337     $r->get('/faq/firehose')->to('faq#firehose');
338     $r->get('/faq/com-mod')->to('faq#com_mod');
339     $r->get('/faq/metamod')->to('faq#metamod');
340     $r->get('/faq/accounts')->to('faq#accounts');
341     $r->get('/faq/friends')->to('faq#friends');
342     $r->get('/faq/tech')->to('faq#tech');
343     $r->get('/faq/')->to('faq#faq');
344
345     # Admin
346     # pages under /admin needs seclev equal or greater than 10000;
347     my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
348
349     $admin->get('/firehose/:id/')->to('admin-firehose#single');
350     $admin->get('/submissions')->to('admin-submissions#index');
351
352     $admin->get('/css')->to('admin-css#edit');
353     $admin->get('/story/edit')->to('admin-story#edit');
354
355     $admin->get('/users')->to('admin-users#index');
356
357     $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
358
359     $admin->get('/sidebar')->to('admin-sidebar#index');
360     $admin->get('/feed')->to('admin-feed#index');
361     $admin->get('/filter')->to('admin-filter#index');
362     $admin->get('/blocking')->to('admin-blocking#index');
363     $admin->get('/ad')->to('admin-ads#index');
364
365     $admin->get('/repository')->to('admin-repository#index');
366     $admin->get('/cache')->to('admin-config#cache');
367
368     # Admin API
369     # pages under /api/v1/admin needs seclev equal or greater than 10000;
370     my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
371
372     $admin_api->get('/feed')->to('API::Admin::Feed#get');
373     $admin_api->post('/feed')->to('API::Admin::Feed#post');
374     $admin_api->get('/filter')->to('API::Admin::Filter#get');
375     $admin_api->post('/filter')->to('API::Admin::Filter#post');
376     $admin_api->get('/blocking')->to('API::Admin::Blocking#get');
377     $admin_api->post('/blocking')->to('API::Admin::Blocking#post');
378
379     $admin_api->get('/repository/export')->to('API::Admin::Repository#export');
380     $admin_api->get('/repository/import')->to('API::Admin::Repository#import');
381
382     $admin_api->post('/sidebar')->to('API::Admin::Sidebar#post');
383     $admin_api->get('/sidebar')->to('API::Admin::Sidebar#get');
384
385     # ad codes management
386     $app->rpc->route_to_model($admin_api->get('/ad/code/'), 'ad_codes', 'select');
387     $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "create"}), 'ad_codes', 'create');
388     $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "update"}), 'ad_codes', 'update');
389     $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "delete"}), 'ad_codes', 'delete');
390
391     # API
392     my $api = $r->under('/api/v1');
393     $api->post('/login')->to('API::Login#login');
394
395     $api->post('/newuser/validate')->to('API::User#validate_new_user');
396     $api->post('/newuser/create')->to('API::User#create_new_user');
397     $api->post('/newuser/password')->to('API::User#newuser_password');
398
399     $api->get('/sidebar/item')->to('API::SidebarItem#get', seclev => 1);
400
401     $api->get('/comment')->to('API::Comment#get');
402     $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
403
404     $api->get('/user')->to('API::User#get');
405     $api->post('/user')->to('API::User#post', seclev => 1);
406
407     $api->get('/journal')->to('API::Journal#get');
408     $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
409
410     $api->get('/submission')->to('API::Submission#get');
411     $api->get('/submissions')->to('API::Submission#list');
412     $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
413
414     $api->get('/story')->to('API::Story#get');
415     $api->post('/story')->to('API::Story#post');
416
417     $api->get('/timeline')->to('API::Timeline#get');
418
419     $api->get('/poll')->to('API::Poll#get');
420     $api->post('/poll')->to('API::Poll#post');
421     $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
422
423     $api->get('/moderation')->to('API::Moderation#get');
424     $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
425
426     $api->get('/metamoderation')->to('API::Metamoderation#get');
427     $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
428
429     $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
430
431     $api->get('/token')->to('API::Token#get');
432
433     $api->post('/tag')->to('API::Tag#post');
434
435     # user page
436     # warning: these pathes uses regexp matching, so must write in tail of route definitions.
437     my $user = $r->under('/~:nickname');
438     $user->get('/'             => [nickname => qr/.*/])->to('user#home');
439     $user->get('/journal/:id'  => [nickname => qr/.*/])->to('journal#single');
440     $user->get('/journals'     => [nickname => qr/.*/])->to('user#journals');
441     $user->get('/journal'      => [nickname => qr/.*/])->to('user#journals'); # for compatibility
442     $user->get('/comments'     => [nickname => qr/.*/])->to('user#comments');
443     $user->get('/submissions'  => [nickname => qr/.*/])->to('user#submissions');
444     $user->get('/friends'      => [nickname => qr/.*/])->to('user#friends');
445     $user->get('/foes'         => [nickname => qr/.*/])->to('user#foes');
446     $user->get('/fans'         => [nickname => qr/.*/])->to('user#fans');
447     $user->get('/freaks'       => [nickname => qr/.*/])->to('user#freaks');
448     $user->get('/achievements' => [nickname => qr/.*/])->to('user#achievements');
449
450 }
451
452 1;