OSDN Git Service

Plugin: add Users helper
[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             }
54             else {
55                 $app->log->warn('invalid log level given in config file');
56             }
57         }
58
59         # file output settings
60         if ($cnf->{mode} eq "local_file") {
61             my $pathname = $cnf->{local_file};
62             # check log is writable
63             if (!$pathname) {
64                 $app->log->error("cannot write system log to file: filename not given");
65             }
66             elsif (-e $pathname) {
67                 if (-w $pathname) {
68                     $app->log->debug("logs will be outputed to $pathname ...");
69                     $app->log(Mojo::Log->new);
70                     $app->log->path($pathname);
71                 }
72                 else {
73                     $app->log->error("cannot write system log to file: $pathname");
74                 }
75             }
76             else {
77                 if (open(my $fh, ">", $pathname)) {
78                     close($fh);
79                     $app->log->debug("logs will be outputed to $pathname ...");
80                     $app->log(Mojo::Log->new);
81                     $app->log->path($pathname);
82                 }
83                 else {
84                     $app->log->error("cannot create system log file: $pathname");
85                 }
86             }
87         }
88     }
89
90     # add static contents directories
91     if ($app->config->{System} && $app->config->{System}->{static_dir}) {
92         for my $dir (@{$app->config->{System}->{static_dir}}) {
93             push @{$app->static->paths}, $dir;
94         }
95     }
96
97     # secret key for hasing
98     $app->secrets([$app->config->{System}->{secret_key},]);
99
100     # when "test" mode, output debug logs.
101     $app->log->level('debug') if $app->mode eq 'test';
102
103     # check if 'maintenance' mode
104     my $maintenance_mode = 0;
105     if ($app->subcommand
106         && any { $app->subcommand eq $_ } qw[configdump configimport databaseinit
107                                              testdatainsert useradd usermod
108                                            ]) {
109         $maintenance_mode = 1;
110     }
111
112     ############################################################
113     #
114     # Plugin Settings
115     #
116     ############################################################
117
118     # profiler settings
119     if ($app->mode eq 'development'
120         && $app->config->{Profiler}
121         && $app->config->{Profiler}->{enable}) {
122         if ($app->config->{Profiler}->{profiler} eq 'nytprof') {
123             $app->plugin('Mojolicious::Plugin::NYTProf', { nytprof => {} });
124         }
125     }
126
127     # enable access logging
128     $app->plugin('Newslash::Plugin::AccessLog');
129
130     # Helpers for Newslash
131     $app->plugin('Newslash::Plugin::NewslashHelpers');
132
133     # use Epoch
134     $app->plugin('Newslash::Plugin::Epoch');
135
136     # support BasicAuth
137     $app->plugin('Newslash::Plugin::BasicAuth');
138
139     # use TimeLimitedCache ($app->cache)
140     $app->plugin('Newslash::Plugin::TimeLimitedCache');
141
142     # use KeyValue Store ($app->kvs)
143     $app->plugin('Newslash::Plugin::KeyValueStore');
144
145     # add Model Loader
146     $app->plugin('Newslash::Plugin::Model', { bypass_startup => $maintenance_mode });
147
148     # use Easy Cache ($app->ezcache)
149     $app->plugin('Newslash::Plugin::EasyCache');
150
151     # use Template::Toolkit 2 render
152     $app->plugin('Newslash::Plugin::TT2Renderer');
153
154     # use ResponseFilter (Faculities)
155     $app->plugin('Newslash::Plugin::ResponseFilter');
156
157     # use CustomBoxes
158     $app->plugin('Newslash::Plugin::CustomBoxes');
159
160     # use AntiCsrf
161     $app->plugin('Newslash::Plugin::AntiCsrf');
162
163     # contents preprocessor
164     $app->plugin('Newslash::Plugin::Preprocessor');
165     if ($maintenance_mode) {
166         $app->log->info("bypassing preprocessor...");
167     }
168     else {
169         $app->preprocessor->generate_all;
170     }
171
172     # javascript loader
173     $app->plugin('Newslash::Plugin::JavaScriptLoader');
174     if ($maintenance_mode) {
175         $app->log->info("bypassing javascriptloader...");
176     }
177     else {
178         $app->javascript_loader->load_all_bundles;
179     }
180
181     # user authorization
182     $app->plugin('Newslash::Plugin::UserAuth');
183
184     # access control
185     $app->plugin('Newslash::Plugin::AccessControl');
186
187     # ReCaptcha control
188     $app->plugin('Newslash::Plugin::ReCaptcha');
189
190     # set canocal (for test.srad.jp)
191     $app->plugin('Newslash::Plugin::Canonical');
192
193     # DiscussionHelper
194     $app->plugin('Newslash::Plugin::DiscussionHelper');
195
196     # use HSTS
197     $app->plugin('Newslash::Plugin::Hsts');
198
199     # Event Que
200     $app->plugin('Newslash::Plugin::EventQue');
201
202     # Statics Logger
203     $app->plugin('Newslash::Plugin::Statics');
204
205     # Request Body based routing condition
206     $app->plugin('Newslash::Plugin::RequestBodyCondition');
207
208     # NS-RPC
209     $app->plugin('Newslash::Plugin::NSRPC');
210
211     # AD renderer
212     $app->plugin('Newslash::Plugin::ADRenderer');
213
214     # HTTP Compression
215     $app->plugin('Newslash::Plugin::HttpCompression');
216
217     # Users helper
218     $app->plugin('Newslash::Plugin::Users');
219
220     ############################################################
221     #
222     # Routing Settings
223     #
224     ############################################################
225
226     my $r = $app->routes;
227
228     # index page
229     $r->get('/')->to('timeline#stories');
230     $r->get('/recent')->to('timeline#recent');
231     $r->get('/popular')->to('timeline#popular');
232     $r->get('/comments')->to('timeline#comments');
233     $r->get('/journals')->to('timeline#journals');
234     $r->get('/submissions')->to('timeline#submissions');
235     $r->get('/polls')->to('timeline#polls');
236
237     # Banned page
238     $r->get('/banned')->to('index#banned', noindex => 1);
239
240     # Login / Logout
241     $r->get('/login')->to('login#login');
242     $r->post('/login')->to('login#login');
243     $r->get('/logout')->to('login#logout');
244
245     # story page
246     $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
247       ->to('story#single');
248
249     # comment page
250     $r->get('/comment/:cid/')->to('comment#single');
251
252     # journal page
253     $r->get('/journal/new')->to('journal#create', seclev => 1);
254     $r->get('/journal/:id/')->to('journal#single');
255
256     # submission page
257     $r->get('/submission/new')->to('submission#create');
258     $r->get('/submission/:id/')->to('submission#single');
259     #$r->post('/submission')->to('submission#create');
260
261     # polls page
262     $r->get('/poll/:qid')->to('poll#single');
263     $r->get('/vote/:qid')->to('poll#vote');
264     $r->post('/vote/:qid')->to('poll#vote_post', csrf_check_id => 'vote');
265
266     # archive page
267     $r->get('/story/:year/:month/:day/')->to('archive#story');
268     $r->get('/story/:year/:month/')->to('archive#story');
269     $r->get('/story/')->to('archive#story');
270
271     $r->get('/journal/:year/:month/:day/')->to('archive#journal');
272     $r->get('/journal/:year/:month/')->to('archive#journal');
273     $r->get('/journal/')->to('archive#journal');
274
275     $r->get('/submission/:year/:month/:day/')->to('archive#submission');
276     $r->get('/submission/:year/:month/')->to('archive#submission');
277     $r->get('/submission/')->to('archive#submission');
278
279     $r->get('/poll/:year/:month/')->to('archive#poll');
280     $r->get('/poll/')->to('archive#poll');
281
282     # tag page
283     $r->get('/tag/:tagname/:type/')->to('tag#list_tagged_items');
284     $r->get('/tag/:tagname/')->to('tag#list_tagged_items');
285     $r->get('/tag/')->to('tag#list_tags');
286
287     # my page
288     $r->get('/my/settings')->to('my#settings', seclev => 1);
289     $r->get('/my/sidebar')->to('my#sidebar', seclev => 1);
290     $r->get('/my/messages')->to('my#messages', seclev => 1);
291     $r->get('/my/')->to('user#home', seclev => 1);
292
293     # User Register
294     $r->get('/my/newuser')->to('login#newuser');
295     $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
296
297     # search page
298     $r->get('/search')->to('search#search');
299
300     # Admin
301     # pages under /admin needs seclev equal or greater than 10000;
302     my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
303
304     $admin->get('/firehose/:id/')->to('admin-firehose#single');
305     $admin->get('/submissions')->to('admin-submissions#index');
306
307     $admin->get('/css')->to('admin-css#edit');
308     $admin->get('/story/edit')->to('admin-story#edit');
309
310     $admin->get('/users')->to('admin-users#index');
311
312     $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
313
314     $admin->get('/sidebar')->to('admin-sidebar#index');
315     $admin->get('/feed')->to('admin-feed#index');
316     $admin->get('/blocking')->to('admin-blocking#index');
317     $admin->get('/ad')->to('admin-ads#index');
318
319     $admin->get('/repository')->to('admin-repository#index');
320     $admin->get('/cache')->to('admin-config#cache');
321
322     # Admin API
323     # pages under /api/v1/admin needs seclev equal or greater than 10000;
324     my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
325     $admin_api->get('/feed')->to('API::Admin::Feed#get');
326     $admin_api->post('/feed')->to('API::Admin::Feed#post');
327     $admin_api->get('/blocking')->to('API::Admin::Blocking#get');
328     $admin_api->post('/blocking')->to('API::Admin::Blocking#post');
329
330     $admin_api->get('/repository/export')->to('API::Admin::Repository#export');
331     $admin_api->get('/repository/import')->to('API::Admin::Repository#import');
332
333     $admin_api->post('/sidebar')->to('API::Admin::Sidebar#post');
334     $admin_api->get('/sidebar')->to('API::Admin::Sidebar#get');
335
336     # ad codes management
337     $app->rpc->route_to_model($admin_api->get('/ad/code/'), 'ad_codes', 'select');
338     $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "create"}), 'ad_codes', 'create');
339     $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "update"}), 'ad_codes', 'update');
340     $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "delete"}), 'ad_codes', 'delete');
341
342     # API
343     my $api = $r->under('/api/v1');
344     $api->post('/login')->to('API::Login#login');
345
346     $api->get('/sidebar/item')->to('API::SidebarItem#get', seclev => 1);
347
348     $api->get('/comment')->to('API::Comment#get');
349     $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
350
351     $api->get('/user')->to('API::User#get');
352     $api->post('/user')->to('API::User#post', seclev => 1);
353
354     $api->get('/journal')->to('API::Journal#get');
355     $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
356
357     $api->get('/submission')->to('API::Submission#get');
358     $api->get('/submissions')->to('API::Submission#list');
359     $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
360
361     $api->get('/story')->to('API::Story#get');
362     $api->post('/story')->to('API::Story#post');
363
364     $api->get('/poll')->to('API::Poll#get');
365     $api->post('/poll')->to('API::Poll#post');
366     $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
367
368     $api->get('/moderation')->to('API::Moderation#get');
369     $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
370
371     $api->get('/metamoderation')->to('API::Metamoderation#get');
372     $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
373
374     $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
375
376     $api->get('/token')->to('API::Token#get');
377
378     # user page
379     # warning: these pathes uses regexp matching, so must write in tail of route definitions.
380     my $user = $r->under('/:nickname');
381     $user->get('/'             => [nickname => qr/~.*/])->to('user#home');
382     $user->get('/journals'     => [nickname => qr/~.*/])->to('user#journals');
383     $user->get('/journal'      => [nickname => qr/~.*/])->to('user#journals'); # for compatibility
384     $user->get('/comments'     => [nickname => qr/~.*/])->to('user#comments');
385     $user->get('/submissions'  => [nickname => qr/~.*/])->to('user#submissions');
386     $user->get('/friends'      => [nickname => qr/~.*/])->to('user#friends');
387     $user->get('/foes'         => [nickname => qr/~.*/])->to('user#foes');
388     $user->get('/fans'         => [nickname => qr/~.*/])->to('user#fans');
389     $user->get('/freaks'       => [nickname => qr/~.*/])->to('user#freaks');
390     $user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
391     #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');
392
393 }
394
395 1;