OSDN Git Service

js: implement AD code related methods
[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     if ($app->config->{Log} && $app->config->{Log}->{backtrace}) {
25         require Carp::Always;
26         Carp::Always->import;
27     }
28
29     # set log file
30     if ($app->config->{Log} && $app->config->{Log}->{system_log}) {
31         # check log is writable
32         my $pathname = $app->config->{Log}->{system_log};
33         if (-e $pathname) {
34             if (-w $pathname) {
35                 $app->log->debug("logs will be outputed to $pathname ...");
36                 $app->log(Mojo::Log->new);
37                 $app->log->path($pathname);
38             }
39             else {
40                 $app->log->error("cannot write system log to file: $pathname");
41             }
42         }
43         else {
44             if (open(my $fh, ">", $pathname)) {
45                 close($fh);
46                 $app->log->debug("logs will be outputed to $pathname ...");
47                 $app->log(Mojo::Log->new);
48                 $app->log->path($pathname);
49             }
50             else {
51                 $app->log->error("cannot create system log file: $pathname");
52             }
53         }
54     }
55
56     # set log level
57     if ($app->config->{Log} && $app->config->{Log}->{level}) {
58         my $loglv = $app->config->{Log}->{level};
59         if (grep { $loglv eq $_ } qw(debug info warn error fatal)) {
60             $app->log->level($loglv);
61         }
62         else {
63             $app->log->warn('invalid log level given in config file');
64         }
65     }
66
67     ############################################################
68     #
69     # Plugin Settings
70     #
71     ############################################################
72
73     # when "test" mode, output debug logs.
74     $app->log->level('debug') if $app->mode eq 'test';
75
76     # renderer helper
77     $app->plugin('Newslash::Plugin::RendererHelper');
78
79     # enable logging
80     $app->plugin('Newslash::Plugin::AccessLog::Debug', $app->config->{Log} || {});
81     $app->plugin('Newslash::Plugin::AccessLog::LocalFile', $app->config->{Log} || {});
82
83     # secret key for hasing
84     $app->secrets([$app->config->{System}->{secret_key},]);
85
86     # stash for plugins
87     #$app->config->{_Plugins} = {};
88
89     # use Epoch
90     $app->plugin('Newslash::Plugin::Epoch');
91
92     # use BasicAuth?
93     if ($app->config->{BasicAuth} && $app->config->{BasicAuth}->{enable}) {
94         $app->plugin('Newslash::Plugin::BasicAuth');
95     }
96
97     # use TimeLimitedCache ($app->cache)
98     $app->plugin('Newslash::Plugin::TimeLimitedCache');
99
100     # use KeyValue Store ($app->kvs)
101     $app->plugin('Newslash::Plugin::KeyValueStore');
102
103     # add Model Loader
104     my $model_opts = $app->config;
105     $model_opts->{Logger} = $app->log;
106     $app->helper(model => Newslash::Model::loader($model_opts));
107     Newslash::Model::startup($model_opts, $app);
108
109     # use Model Cache ($app->model_cache)
110     $app->plugin('Newslash::Plugin::ModelCache');
111
112     # use Template::Toolkit 2 render
113     $app->plugin('Newslash::Plugin::TT2Renderer');
114
115     # use ViewFunctions
116     $app->plugin('Newslash::Plugin::ViewFunctions');
117
118     # use CustomBoxes
119     $app->plugin('Newslash::Plugin::CustomBoxes');
120
121     # use Analytics helper
122     $app->plugin('Newslash::Plugin::GoogleAnalytics');
123
124     # user AntiCsrf ($app->anti_csrf)
125     if ($app->mode ne 'test') {
126         # when test mode, disable AntiCsrf.
127         $app->plugin('Newslash::Plugin::AntiCsrf');
128     }
129
130     # compile CSS
131     #$app->plugin('Newslash::Plugin::CSSCompile');
132
133     # quasi-static content
134     $app->plugin('Newslash::Plugin::QuasiStaticContent');
135
136     # javascript loader
137     $app->plugin('Newslash::Plugin::JavaScriptLoader');
138
139     # user authorization
140     $app->plugin('Newslash::Plugin::UserAuth');
141
142     # access control
143     $app->plugin('Newslash::Plugin::AccessControl');
144
145     # ReCaptcha control
146     if ($app->mode ne 'test') {
147         $app->plugin('Newslash::Plugin::ReCaptcha');
148     }
149
150     # set canocal (for test.srad.jp)
151     $app->plugin('Newslash::Plugin::Canonical');
152
153     # DiscussionHelper
154     $app->plugin('Newslash::Plugin::DiscussionHelper');
155
156     # use HSTS
157     $app->plugin('Newslash::Plugin::Hsts');
158
159     # Event Que
160     $app->plugin('Newslash::Plugin::EventQue');
161
162     # Statics Logger
163     $app->plugin('Newslash::Plugin::Statics');
164
165     # Request Body based routing condition
166     $app->plugin('Newslash::Plugin::RequestBodyCondition');
167
168     # NS-RPC
169     $app->plugin('Newslash::Plugin::NSRPC');
170
171     ############################################################
172     #
173     # Generate site-global used javascript file
174     #
175     ############################################################
176     my $templ_name = "common/siteconfig.js";
177     my $mod_reasons = $app->model('moderations')->reasons();
178     my $topics = $app->model('tags')->get_topics;
179     my @acl2_types = $app->model('users')->acl2_types;
180     my $keywords = {};
181     for my $topic (@$topics) {
182         my $lc_keyword = lc($topic->{keyword});
183         my $lc_textname = lc($topic->{textname});
184         $keywords->{$lc_keyword} = {keyword => $topic->{keyword},
185                                     textname => $topic->{textname},
186                                     image => $topic->{image}};
187         if ($lc_keyword ne $lc_textname) {
188             $keywords->{$lc_textname} = $keywords->{$lc_keyword};
189         }
190     }
191     my $vars = {
192                 moderate_reasons => $mod_reasons,
193                 topics => $keywords,
194                 acl2_types => \@acl2_types,
195                };
196     my $siteconfig = $app->tt2renderer->render($templ_name, $vars);
197     $app->static_content->add_content("js/siteconfig.js", $siteconfig, "text/javascript; charset=utf-8");
198
199     ############################################################
200     #
201     # Routing Settings
202     #
203     ############################################################
204
205     my $r = $app->routes;
206
207     # index page
208     $r->get('/')->to('timeline#stories');
209     $r->get('/recent')->to('timeline#recent');
210     $r->get('/popular')->to('timeline#popular');
211     $r->get('/comments')->to('timeline#comments');
212     $r->get('/journals')->to('timeline#journals');
213     $r->get('/submissions')->to('timeline#submissions');
214     $r->get('/polls')->to('timeline#polls');
215
216     # siteconfig.js for global settings
217     $r->get('/siteconfig/:epoch/siteconfig.js')->to('site_config#site_config');
218
219     # Banned page
220     $r->get('/banned')->to('index#banned', noindex => 1);
221
222     # Login / Logout
223     $r->get('/login')->to('login#login');
224     $r->post('/login')->to('login#login');
225     $r->get('/logout')->to('login#logout');
226
227     # User Register
228     $r->get('/my/newuser')->to('login#newuser');
229     $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
230
231     # story page
232     $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
233       ->to('story#single');
234
235     # comment page
236     $r->get('/comment/:cid/')->to('comment#single');
237
238     # journal page
239     $r->get('/journal/new')->to('journal#create', seclev => 1);
240     $r->get('/journal/:id/')->to('journal#single');
241
242     # submission page
243     $r->get('/submission/new')->to('submission#create');
244     $r->get('/submission/:id/')->to('submission#single');
245     #$r->post('/submission')->to('submission#create');
246
247     # polls page
248     $r->get('/poll/:qid')->to('poll#single');
249     $r->get('/vote/:qid')->to('poll#vote');
250     $r->post('/vote/:qid')->to('poll#vote_post', csrf_check_id => 'vote');
251
252     # archive page
253     $r->get('/story/:year/:month/:day/')->to('archive#story');
254     $r->get('/story/:year/:month/')->to('archive#story');
255     $r->get('/story/')->to('archive#story');
256
257     $r->get('/journal/:year/:month/:day/')->to('archive#journal');
258     $r->get('/journal/:year/:month/')->to('archive#journal');
259     $r->get('/journal/')->to('archive#journal');
260
261     $r->get('/submission/:year/:month/:day/')->to('archive#submission');
262     $r->get('/submission/:year/:month/')->to('archive#submission');
263     $r->get('/submission/')->to('archive#submission');
264
265     $r->get('/poll/:year/:month/')->to('archive#poll');
266     $r->get('/poll/')->to('archive#poll');
267
268     # tag page
269     $r->get('/tag/:tagname/:type/')->to('tag#list_tagged_items');
270     $r->get('/tag/:tagname/')->to('tag#list_tagged_items');
271     $r->get('/tag/')->to('tag#list_tags');
272
273     # my page
274     $r->get('/my/settings')->to('user#settings', seclev => 1);
275     $r->get('/my/sidebar')->to('user#sidebar', seclev => 1);
276     $r->get('/my/messages')->to('my#messages', seclev => 1);
277     $r->get('/my/')->to('user#home', seclev => 1);
278
279     # search page
280     $r->get('/search')->to('search#search');
281
282     # Admin
283     # pages under /admin needs seclev equal or greater than 10000;
284     my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
285
286     $admin->get('/firehose/:id/')->to('admin-firehose#single');
287     $admin->get('/submissions')->to('admin-submissions#index');
288
289     $admin->get('/css')->to('admin-css#edit');
290     $admin->get('/story/edit')->to('admin-story#edit');
291
292     $admin->get('/users')->to('admin-users#index');
293
294     $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
295
296     $admin->get('/sidebar')->to('admin-sidebar#index');
297     $admin->get('/feed')->to('admin-feed#index');
298     $admin->get('/blocking')->to('admin-blocking#index');
299     $admin->get('/ad')->to('admin-ads#index');
300
301     $admin->get('/repository')->to('admin-repository#index');
302
303     # Admin API
304     # pages under /api/v1/admin needs seclev equal or greater than 10000;
305     my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
306     $admin_api->get('/feed')->to('API::Admin::Feed#get');
307     $admin_api->post('/feed')->to('API::Admin::Feed#post');
308     $admin_api->get('/blocking')->to('API::Admin::Blocking#get');
309     $admin_api->post('/blocking')->to('API::Admin::Blocking#post');
310
311     $admin_api->get('/repository/export')->to('API::Admin::Repository#export');
312     $admin_api->get('/repository/import')->to('API::Admin::Repository#import');
313
314     $admin_api->post('/sidebar')->to('API::Admin::Sidebar#post');
315     $admin_api->get('/sidebar')->to('API::Admin::Sidebar#get');
316
317     # ad codes management
318     $app->rpc->route_to_model($admin_api->get('/ad/code/'), 'ad_codes', 'select');
319     $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "create"}), 'ad_codes', 'create');
320     $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "update"}), 'ad_codes', 'update');
321     $app->rpc->route_to_model($admin_api->post('/ad/code/')->over(request_body => {action => "delete"}), 'ad_codes', 'delete');
322
323     # API
324     my $api = $r->under('/api/v1');
325     $api->post('/login')->to('API::Login#login');
326
327     $api->get('/sidebar/item')->to('API::SidebarItem#get', seclev => 1);
328
329     $api->get('/comment')->to('API::Comment#get');
330     $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
331
332     $api->get('/user')->to('API::User#get');
333     $api->post('/user')->to('API::User#post', seclev => 1);
334
335     $api->get('/journal')->to('API::Journal#get');
336     $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
337
338     $api->get('/submission')->to('API::Submission#get');
339     $api->get('/submissions')->to('API::Submission#list');
340     $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
341
342     $api->get('/story')->to('API::Story#get');
343     $api->post('/story')->to('API::Story#post');
344
345     $api->get('/poll')->to('API::Poll#get');
346     $api->post('/poll')->to('API::Poll#post');
347     $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
348
349     $api->get('/moderation')->to('API::Moderation#get');
350     $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
351
352     $api->get('/metamoderation')->to('API::Metamoderation#get');
353     $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
354
355     $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
356
357     $api->get('/token')->to('API::Token#get');
358
359     # user page
360     # warning: these pathes uses regexp matching, so must write in tail of route definitions.
361     my $user = $r->under('/:nickname');
362     $user->get('/'             => [nickname => qr/~.*/])->to('user#home');
363     $user->get('/journals'     => [nickname => qr/~.*/])->to('user#journals');
364     $user->get('/journal'      => [nickname => qr/~.*/])->to('user#journals'); # for compatibility
365     $user->get('/comments'     => [nickname => qr/~.*/])->to('user#comments');
366     $user->get('/submissions'  => [nickname => qr/~.*/])->to('user#submissions');
367     $user->get('/friends'      => [nickname => qr/~.*/])->to('user#friends');
368     $user->get('/foes'         => [nickname => qr/~.*/])->to('user#foes');
369     $user->get('/fans'         => [nickname => qr/~.*/])->to('user#fans');
370     $user->get('/freaks'       => [nickname => qr/~.*/])->to('user#freaks');
371     #$user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
372     #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');
373
374 }
375
376 1;