OSDN Git Service

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