OSDN Git Service

move /siteconfig.js to /siteconfig/:epoch/siteconfig.js
[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 CustomBoxes
116     $app->plugin('Newslash::Plugin::CustomBoxes');
117
118     # use Analytics helper
119     $app->plugin('Newslash::Plugin::GoogleAnalytics');
120
121     # user AntiCsrf ($app->anti_csrf)
122     if ($app->mode ne 'test') {
123         # when test mode, disable AntiCsrf.
124         $app->plugin('Newslash::Plugin::AntiCsrf');
125     }
126
127     # compile CSS
128     #$app->plugin('Newslash::Plugin::CSSCompile');
129
130     # quasi-static content
131     $app->plugin('Newslash::Plugin::QuasiStaticContent');
132
133     # javascript loader
134     $app->plugin('Newslash::Plugin::JavaScriptLoader');
135
136     # user authorization
137     $app->plugin('Newslash::Plugin::UserAuth');
138
139     # access control
140     $app->plugin('Newslash::Plugin::AccessControl');
141
142     # ReCaptcha control
143     if ($app->mode ne 'test') {
144         $app->plugin('Newslash::Plugin::ReCaptcha');
145     }
146
147     # set canocal (for test.srad.jp)
148     $app->plugin('Newslash::Plugin::Canonical');
149
150     # DiscussionHelper
151     $app->plugin('Newslash::Plugin::DiscussionHelper');
152
153     # use HSTS
154     $app->plugin('Newslash::Plugin::Hsts');
155
156     # Event Que
157     $app->plugin('Newslash::Plugin::EventQue');
158
159     # Statics Logger
160     $app->plugin('Newslash::Plugin::Statics');
161
162     ############################################################
163     #
164     # Generate site-global used javascript file
165     #
166     ############################################################
167     my $templ_name = "common/siteconfig.js";
168     my $mod_reasons = $app->model('moderations')->reasons();
169     my $topics = $app->model('tags')->get_topics;
170     my @acl2_types = $app->model('users')->acl2_types;
171     my $keywords = {};
172     for my $topic (@$topics) {
173         my $lc_keyword = lc($topic->{keyword});
174         my $lc_textname = lc($topic->{textname});
175         $keywords->{$lc_keyword} = {keyword => $topic->{keyword},
176                                     textname => $topic->{textname},
177                                     image => $topic->{image}};
178         if ($lc_keyword ne $lc_textname) {
179             $keywords->{$lc_textname} = $keywords->{$lc_keyword};
180         }
181     }
182     my $vars = {
183                 moderate_reasons => $mod_reasons,
184                 topics => $keywords,
185                 acl2_types => \@acl2_types,
186                };
187     my $siteconfig = $app->tt2renderer->render($templ_name, $vars);
188     $app->static_content->add_content("js/siteconfig.js", $siteconfig, "text/javascript; charset=utf-8");
189
190     ############################################################
191     #
192     # Routing Settings
193     #
194     ############################################################
195
196     my $r = $app->routes;
197
198     # index page
199     $r->get('/')->to('timeline#stories');
200     $r->get('/recent')->to('timeline#recent');
201     $r->get('/popular')->to('timeline#popular');
202     $r->get('/comments')->to('timeline#comments');
203     $r->get('/journals')->to('timeline#journals');
204     $r->get('/submissions')->to('timeline#submissions');
205     $r->get('/polls')->to('timeline#polls');
206
207     # siteconfig.js for global settings
208     $r->get('/siteconfig/:epoch/siteconfig.js')->to('site_config#site_config');
209
210     # Banned page
211     $r->get('/banned')->to('index#banned', noindex => 1);
212
213     # Login / Logout
214     $r->get('/login')->to('login#login');
215     $r->post('/login')->to('login#login');
216     $r->get('/logout')->to('login#logout');
217
218     # User Register
219     $r->get('/my/newuser')->to('login#newuser');
220     $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
221
222     # story page
223     $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
224       ->to('story#single');
225
226     # comment page
227     $r->get('/comment/:cid/')->to('comment#single');
228
229     # journal page
230     $r->get('/journal/new')->to('journal#create', seclev => 1);
231     $r->get('/journal/:id/')->to('journal#single');
232
233     # submission page
234     $r->get('/submission/new')->to('submission#create');
235     $r->get('/submission/:id/')->to('submission#single');
236     #$r->post('/submission')->to('submission#create');
237
238     # polls page
239     $r->get('/poll/:qid')->to('poll#single');
240     $r->get('/vote/:qid')->to('poll#vote');
241     $r->post('/vote/:qid')->to('poll#vote_post', csrf_check_id => 'vote');
242
243     # archive page
244     $r->get('/story/:year/:month/:day/')->to('archive#story');
245     $r->get('/story/:year/:month/')->to('archive#story');
246     $r->get('/story/')->to('archive#story');
247
248     $r->get('/journal/:year/:month/:day/')->to('archive#journal');
249     $r->get('/journal/:year/:month/')->to('archive#journal');
250     $r->get('/journal/')->to('archive#journal');
251
252     $r->get('/submission/:year/:month/:day/')->to('archive#submission');
253     $r->get('/submission/:year/:month/')->to('archive#submission');
254     $r->get('/submission/')->to('archive#submission');
255
256     $r->get('/poll/:year/:month/')->to('archive#poll');
257     $r->get('/poll/')->to('archive#poll');
258
259     # tag page
260     $r->get('/tag/:tagname/')->to('tag#list');
261
262     # my page
263     $r->get('/my/settings')->to('user#settings', seclev => 1);
264     $r->get('/my/sidebar')->to('user#sidebar', seclev => 1);
265     $r->get('/my/messages')->to('my#messages', seclev => 1);
266     $r->get('/my/')->to('user#home', seclev => 1);
267
268     # Admin
269     # pages under /admin needs seclev equal or greater than 10000;
270     my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
271
272     $admin->get('/firehose/:id/')->to('admin-firehose#single');
273     $admin->get('/submissions')->to('admin-submissions#index');
274
275     $admin->get('/css')->to('admin-css#edit');
276     $admin->get('/story/edit')->to('admin-story#edit');
277
278     $admin->get('/users')->to('admin-users#index');
279
280     $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
281
282     $admin->get('/sidebar')->to('admin-sidebar#index');
283     $admin->get('/feed')->to('admin-feed#index');
284     $admin->get('/blocking')->to('admin-blocking#index');
285
286     $admin->get('/repository')->to('admin-repository#index');
287
288     # Admin API
289     # pages under /api/v1/admin needs seclev equal or greater than 10000;
290     my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
291     $admin_api->get('/feed')->to('API::Admin::Feed#get');
292     $admin_api->post('/feed')->to('API::Admin::Feed#post');
293     $admin_api->get('/blocking')->to('API::Admin::Blocking#get');
294     $admin_api->post('/blocking')->to('API::Admin::Blocking#post');
295
296     $admin_api->get('/repository/export')->to('API::Admin::Repository#export');
297     $admin_api->get('/repository/import')->to('API::Admin::Repository#import');
298
299     $admin_api->post('/sidebar')->to('API::Admin::Sidebar#post');
300     $admin_api->get('/sidebar')->to('API::Admin::Sidebar#get');
301
302     # API
303     my $api = $r->under('/api/v1');
304     $api->post('/login')->to('API::Login#login');
305
306     $api->get('/sidebar/item')->to('API::SidebarItem#get', seclev => 1);
307
308     $api->get('/comment')->to('API::Comment#get');
309     $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
310
311     $api->get('/user')->to('API::User#get');
312     $api->post('/user')->to('API::User#post', seclev => 1);
313
314     $api->get('/journal')->to('API::Journal#get');
315     $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
316
317     $api->get('/submission')->to('API::Submission#get');
318     $api->get('/submissions')->to('API::Submission#list');
319     $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
320
321     $api->get('/story')->to('API::Story#get');
322     $api->post('/story')->to('API::Story#post');
323
324     $api->get('/poll')->to('API::Poll#get');
325     $api->post('/poll')->to('API::Poll#post');
326     $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
327
328     $api->get('/moderation')->to('API::Moderation#get');
329     $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
330
331     $api->get('/metamoderation')->to('API::Metamoderation#get');
332     $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
333
334     $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
335
336     $api->get('/token')->to('API::Token#get');
337
338     # user page
339     # warning: these pathes uses regexp matching, so must write in tail of route definitions.
340     my $user = $r->under('/:nickname');
341     $user->get('/'             => [nickname => qr/~.*/])->to('user#home');
342     $user->get('/journals'     => [nickname => qr/~.*/])->to('user#journals');
343     $user->get('/journal'      => [nickname => qr/~.*/])->to('user#journals'); # for compatibility
344     $user->get('/comments'     => [nickname => qr/~.*/])->to('user#comments');
345     $user->get('/submissions'  => [nickname => qr/~.*/])->to('user#submissions');
346     $user->get('/friends'      => [nickname => qr/~.*/])->to('user#friends');
347     $user->get('/foes'         => [nickname => qr/~.*/])->to('user#foes');
348     $user->get('/fans'         => [nickname => qr/~.*/])->to('user#fans');
349     $user->get('/freaks'       => [nickname => qr/~.*/])->to('user#freaks');
350     #$user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
351     #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');
352
353 }
354
355 1;