OSDN Git Service

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