OSDN Git Service

Web: implement GET for /api/v1/story
[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     $app->plugin('Newslash::Plugin::AntiCsrf');
79
80     # compile CSS
81     #$app->plugin('Newslash::Plugin::CSSCompile');
82
83     # quasi-static content
84     $app->plugin('Newslash::Plugin::QuasiStaticContent');
85
86     # user authorization
87     $app->plugin('Newslash::Plugin::UserAuth');
88
89     # access control
90     $app->plugin('Newslash::Plugin::AccessControl');
91
92     # Event Que
93     $app->plugin('Newslash::Plugin::EventQue');
94
95
96     ############################################################
97     #
98     # Generate site-global used javascript file
99     #
100     ############################################################
101     my $templ_name = "common/siteconfig.js";
102     my $mod_reasons = $app->model('moderations')->reasons();
103     my $topics = $app->model('tags')->get_topics;
104     my $keywords = [];
105     for my $topic (@$topics) {
106         my $lc_keyword = lc($topic->{keyword});
107         push @$keywords, $lc_keyword;
108         if ($lc_keyword eq lc($topic->{textname})) {
109             push @$keywords, lc($topic->{textname});
110         }
111     }
112     my $vars = {
113                 moderate_reasons => $mod_reasons,
114                 topics => $keywords,
115                };
116     my $siteconfig = $app->tt2renderer->render($templ_name, $vars);
117     $app->static_content->add_content("js/siteconfig.js", $siteconfig, "text/javascript; charset=utf-8");
118
119     ############################################################
120     #
121     # Routing Settings
122     #
123     ############################################################
124
125     my $r = $app->routes;
126
127     # index page
128     $r->get('/')->to('index#root');
129     $r->get('/recent')->to('timeline#recent');
130     $r->get('/journals')->to('index#journals');
131     $r->get('/comments')->to('index#comments');
132     $r->get('/submissions')->to('index#submissions');
133
134     # archive page
135     $r->get('/story/:year/:month/:day/' => [year => qr/[0-9]{2}/,
136                                             month => qr/[0-9]{2}/,
137                                             day => qr/[0-9]{2}/])->to('index#story_archive');
138
139
140     # Login / Logout
141     $r->get('/login')->to('login#login');
142     $r->post('/login')->to('login#login');
143     $r->get('/logout')->to('login#logout');
144
145     # User Register
146     $r->get('/my/newuser')->to('login#newuser');
147     $r->post('/my/newuser')->to('login#newuser');
148
149     # story page
150     $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
151       ->to('story#story');
152
153     # journal page
154     $r->get('/journal/new')->to('journal#create', seclev => 1);
155     $r->get('/journal/:id/')->to('journal#journal');
156
157     # submission page
158     $r->get('/submission/new')->to('submission#create');
159     $r->get('/submission/:id/')->to('submission#submission');
160     #$r->post('/submission')->to('submission#create');
161
162     # my page
163     $r->get('/my/settings')->to('user#settings', seclev => 1);
164     $r->get('/my/sidebar')->to('user#sidebar', seclev => 1);
165     $r->get('/my/messages')->to('my#messages', seclev => 1);
166     $r->get('/my/')->to('user#home', seclev => 1);
167
168     # Admin
169     # pages under /admin needs seclev equal or greater than 10000;
170     my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
171
172     $admin->get('/submissions')->to('admin-submissions#index');
173     $admin->get('/submissions/list')->to('admin-submissions#list');
174
175     $admin->get('/css')->to('admin-css#edit');
176     $admin->get('/story/edit')->to('admin-story#edit');
177
178     $admin->get('/sidebar')->to('admin-sidebar#index');
179     $admin->post('/sidebar/update')->to('admin-sidebar#update');
180     $admin->post('/sidebar/delete')->to('admin-sidebar#delete');
181     $admin->any(['GET', 'POST'] => '/sidebar/list')->to('admin-sidebar#list');
182     $admin->post('/sidebar/get')->to('admin-sidebar#get');
183
184     # API
185     my $api = $r->under('/api/v1');
186     $api->get('/sidebars')->to('admin-sidebar#list');
187
188     $api->post('/login')->to('API::Login#login');
189
190     $api->get('/comment')->to('API::Comment#get');
191     $api->post('/comment')->to('API::Comment#post');
192
193     $api->get('/user')->to('API::User#get');
194     $api->post('/user')->to('API::User#post', seclev => 1);
195
196     $api->get('/journal')->to('API::Journal#get');
197     $api->post('/journal')->to('API::Journal#post', seclev => 1);
198
199     $api->get('/submission')->to('API::Submission#get');
200     $api->post('/submission')->to('API::Submission#post');
201     $api->get('/story')->to('API::Story#get');
202     $api->post('/story')->to('API::Story#post');
203
204     $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
205     $api->get('/moderation')->to('API::Moderation#get',, csrf_check_id => 'moderation');
206
207     $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
208     $api->get('/metamoderation')->to('API::Metamoderation#get', csrf_check_id => 'moderation');
209
210     $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
211
212     $api->get('/token')->to('API::Token#get');
213
214     # user page
215     # warning: these pathes uses regexp matching, so must write in tail of route definitions.
216     my $user = $r->under('/:nickname');
217     $user->get('/'             => [nickname => qr/~.*/])->to('user#home');
218     $user->get('/journals'     => [nickname => qr/~.*/])->to('user#journals');
219     $user->get('/comments'     => [nickname => qr/~.*/])->to('user#comments');
220     $user->get('/submissions'  => [nickname => qr/~.*/])->to('user#submissions');
221     $user->get('/friends'      => [nickname => qr/~.*/])->to('user#friends');
222     $user->get('/foes'         => [nickname => qr/~.*/])->to('user#foes');
223     $user->get('/fans'         => [nickname => qr/~.*/])->to('user#fans');
224     $user->get('/freaks'       => [nickname => qr/~.*/])->to('user#freaks');
225     $user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
226     #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');
227
228 }
229
230 1;