OSDN Git Service

implement /admin/firehose/<id> for test
[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     # set log level
25     if ($app->config->{Log} && $app->config->{Log}->{level}) {
26         my $loglv = $app->config->{Log}->{level};
27         if (grep { $loglv eq $_ } qw(debug info warn error fatal)) {
28             $app->log->level($loglv);
29         }
30         else {
31             $app->log->warn('invalid log level given in config file');
32         }
33     }
34
35     ############################################################
36     #
37     # Plugin Settings
38     #
39     ############################################################
40
41     # when "test" mode, output debug logs.
42     $app->log->level('debug') if $app->mode eq 'test';
43
44     # enable logging
45     $app->plugin('Newslash::Plugin::AccessLog::Debug', $app->config->{Log} || {});
46     $app->plugin('Newslash::Plugin::AccessLog::LocalFile', $app->config->{Log} || {});
47
48     # secret key for hasing
49     $app->secrets([$app->config->{System}->{secret_key},]);
50
51     # stash for plugins
52     #$app->config->{_Plugins} = {};
53
54     # use BasicAuth?
55     if ($app->config->{BasicAuth} && $app->config->{BasicAuth}->{enable}) {
56         $app->plugin('Newslash::Plugin::BasicAuth');
57     }
58
59     # use TimeLimitedCache ($app->cache)
60     $app->plugin('Newslash::Plugin::TimeLimitedCache');
61
62     # use KeyValue Store ($app->kvs)
63     $app->plugin('Newslash::Plugin::KeyValueStore');
64
65     # add Model Loader
66     my $model_opts = $app->config;
67     $model_opts->{Logger} = $app->log;
68     $app->helper(model => Newslash::Model::loader($model_opts));
69     Newslash::Model::startup($model_opts, $app);
70
71     # use Model Cache ($app->model_cache)
72     $app->plugin('Newslash::Plugin::ModelCache');
73
74     # use Template::Toolkit 2 render
75     $app->plugin('Newslash::Plugin::TT2Renderer');
76
77     # user AntiCsrf ($app->anti_csrf)
78     if ($app->mode ne 'test') {
79         # when test mode, disable AntiCsrf.
80         $app->plugin('Newslash::Plugin::AntiCsrf');
81     }
82
83     # compile CSS
84     #$app->plugin('Newslash::Plugin::CSSCompile');
85
86     # quasi-static content
87     $app->plugin('Newslash::Plugin::QuasiStaticContent');
88
89     # user authorization
90     $app->plugin('Newslash::Plugin::UserAuth');
91
92     # access control
93     $app->plugin('Newslash::Plugin::AccessControl');
94
95     # ReCaptcha control
96     if ($app->mode ne 'test') {
97         $app->plugin('Newslash::Plugin::ReCaptcha');
98     }
99
100     # set canocal (for test.srad.jp)
101     $app->plugin('Newslash::Plugin::Canonical');
102
103     # Event Que
104     $app->plugin('Newslash::Plugin::EventQue');
105
106
107     ############################################################
108     #
109     # Generate site-global used javascript file
110     #
111     ############################################################
112     my $templ_name = "common/siteconfig.js";
113     my $mod_reasons = $app->model('moderations')->reasons();
114     my $topics = $app->model('tags')->get_topics;
115     my @acl2_types = $app->model('users')->acl2_types;
116     my $keywords = {};
117     for my $topic (@$topics) {
118         my $lc_keyword = lc($topic->{keyword});
119         my $lc_textname = lc($topic->{textname});
120         $keywords->{$lc_keyword} = {keyword => $topic->{keyword},
121                                     textname => $topic->{textname},
122                                     image => $topic->{image}};
123         if ($lc_keyword ne $lc_textname) {
124             $keywords->{$lc_textname} = $keywords->{$lc_keyword};
125         }
126     }
127     my $vars = {
128                 moderate_reasons => $mod_reasons,
129                 topics => $keywords,
130                 acl2_types => \@acl2_types,
131                };
132     my $siteconfig = $app->tt2renderer->render($templ_name, $vars);
133     $app->static_content->add_content("js/siteconfig.js", $siteconfig, "text/javascript; charset=utf-8");
134
135     ############################################################
136     #
137     # Routing Settings
138     #
139     ############################################################
140
141     my $r = $app->routes;
142
143     # index page
144     $r->get('/')->to('timeline#stories');
145     $r->get('/recent')->to('timeline#recent');
146     $r->get('/popular')->to('timeline#popular');
147     $r->get('/comments')->to('timeline#comments');
148     $r->get('/journals')->to('timeline#journals');
149     $r->get('/submissions')->to('timeline#submissions');
150     $r->get('/polls')->to('timeline#polls');
151
152     # Banned page
153     $r->get('/banned')->to('index#banned', noindex => 1);
154
155     # Login / Logout
156     $r->get('/login')->to('login#login');
157     $r->post('/login')->to('login#login');
158     $r->get('/logout')->to('login#logout');
159
160     # User Register
161     $r->get('/my/newuser')->to('login#newuser');
162     $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
163
164     # story page
165     $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
166       ->to('story#story');
167
168     # comment page
169     $r->get('/comment/:cid/')->to('comment#single');
170
171     # journal page
172     $r->get('/journal/new')->to('journal#create', seclev => 1);
173     $r->get('/journal/:id/')->to('journal#journal');
174
175     # submission page
176     $r->get('/submission/new')->to('submission#create');
177     $r->get('/submission/:id/')->to('submission#submission');
178     #$r->post('/submission')->to('submission#create');
179
180     # polls page
181     $r->get('/poll/:qid')->to('poll#single');
182     $r->get('/vote/:qid')->to('poll#vote');
183     $r->get('/polls')->to('poll#index');
184     $r->post('/vote/:qid')->to('poll#post', csrf_check_id => 'vote');
185
186     # archive page
187     $r->get('/story/:year/:month/:day/')->to('archive#story');
188     $r->get('/story/:year/:month/')->to('archive#story');
189     $r->get('/story/')->to('archive#story');
190
191     $r->get('/journal/:year/:month/:day/')->to('archive#journal');
192     $r->get('/journal/:year/:month/')->to('archive#journal');
193     $r->get('/journal/')->to('archive#journal');
194
195     $r->get('/submission/:year/:month/:day/')->to('archive#submission');
196     $r->get('/submission/:year/:month/')->to('archive#submission');
197     $r->get('/submission/')->to('archive#submission');
198
199     $r->get('/poll/:year/:month/')->to('archive#poll');
200     $r->get('/poll/')->to('archive#poll');
201
202     # my page
203     $r->get('/my/settings')->to('user#settings', seclev => 1);
204     $r->get('/my/sidebar')->to('user#sidebar', seclev => 1);
205     $r->get('/my/messages')->to('my#messages', seclev => 1);
206     $r->get('/my/')->to('user#home', seclev => 1);
207
208     # Admin
209     # pages under /admin needs seclev equal or greater than 10000;
210     my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
211
212     $admin->get('/firehose/:id/')->to('admin-firehose#single');
213     $admin->get('/submissions')->to('admin-submissions#index');
214
215     $admin->get('/css')->to('admin-css#edit');
216     $admin->get('/story/edit')->to('admin-story#edit');
217
218     $admin->get('/users')->to('admin-users#index');
219
220     $admin->get('/default-sidebar')->to('admin-sidebar#defaults');
221
222     $admin->get('/sidebar')->to('admin-sidebar#index');
223     $admin->get('/feed')->to('admin-feed#index');
224     $admin->get('/blocking')->to('admin-blocking#index');
225
226     $admin->get('/repository')->to('admin-repository#index');
227
228     # Admin API
229     # pages under /api/v1/admin needs seclev equal or greater than 10000;
230     my $admin_api = $r->under('/api/v1/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
231     $admin_api->get('/feed')->to('API::Admin::Feed#get');
232     $admin_api->post('/feed')->to('API::Admin::Feed#post');
233     $admin_api->get('/blocking')->to('API::Admin::Blocking#get');
234     $admin_api->post('/blocking')->to('API::Admin::Blocking#post');
235
236     $admin_api->get('/repository/export')->to('API::Admin::Repository#export');
237     $admin_api->get('/repository/import')->to('API::Admin::Repository#import');
238
239     $admin_api->post('/sidebar')->to('API::Admin::Sidebar#post');
240     $admin_api->get('/sidebar')->to('API::Admin::Sidebar#get');
241
242     # API
243     my $api = $r->under('/api/v1');
244     $api->post('/login')->to('API::Login#login');
245
246     $api->get('/comment')->to('API::Comment#get');
247     $api->post('/comment')->to('API::Comment#post', captcha_check => 1, csrf_check_id => 'comment');
248
249     $api->get('/user')->to('API::User#get');
250     $api->post('/user')->to('API::User#post', seclev => 1);
251
252     $api->get('/journal')->to('API::Journal#get');
253     $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
254
255     $api->get('/submission')->to('API::Submission#get');
256     $api->get('/submissions')->to('API::Submission#list');
257     $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
258
259     $api->get('/story')->to('API::Story#get');
260     $api->post('/story')->to('API::Story#post');
261
262     $api->get('/poll')->to('API::Poll#get');
263     $api->post('/poll')->to('API::Poll#post');
264     $api->post('/vote')->to('API::Poll#vote', csrf_check_id => 'vote');
265
266     $api->get('/moderation')->to('API::Moderation#get');
267     $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
268
269     $api->get('/metamoderation')->to('API::Metamoderation#get');
270     $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
271
272     $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
273
274     $api->get('/token')->to('API::Token#get');
275
276     # user page
277     # warning: these pathes uses regexp matching, so must write in tail of route definitions.
278     my $user = $r->under('/:nickname');
279     $user->get('/'             => [nickname => qr/~.*/])->to('user#home');
280     $user->get('/journals'     => [nickname => qr/~.*/])->to('user#journals');
281     $user->get('/comments'     => [nickname => qr/~.*/])->to('user#comments');
282     $user->get('/submissions'  => [nickname => qr/~.*/])->to('user#submissions');
283     $user->get('/friends'      => [nickname => qr/~.*/])->to('user#friends');
284     $user->get('/foes'         => [nickname => qr/~.*/])->to('user#foes');
285     $user->get('/fans'         => [nickname => qr/~.*/])->to('user#fans');
286     $user->get('/freaks'       => [nickname => qr/~.*/])->to('user#freaks');
287     $user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
288     #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');
289
290 }
291
292 1;