2 use Mojo::Base 'Mojolicious';
3 use Mojo::Util qw(dumper);
7 use constant CONFIG_FILE => '/etc/newslash/newslash.conf';
9 # This method will run once at server start
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);
19 #$app->plugin('JSONConfig');
20 $app->plugin('Newslash::Plugin::YAMLConfig');
22 # TODO: load/save configs with database
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);
31 $app->log->warn('invalid log level given in config file');
35 ############################################################
39 ############################################################
41 # when "test" mode, output debug logs.
42 $app->log->level('debug') if $app->mode eq 'test';
45 $app->plugin('Newslash::Plugin::AccessLog::Debug', $app->config->{Log} || {});
46 $app->plugin('Newslash::Plugin::AccessLog::LocalFile', $app->config->{Log} || {});
48 # secret key for hasing
49 $app->secrets([$app->config->{System}->{secret_key},]);
52 #$app->config->{_Plugins} = {};
55 if ($app->config->{BasicAuth} && $app->config->{BasicAuth}->{enable}) {
56 $app->plugin('Newslash::Plugin::BasicAuth');
59 # use TimeLimitedCache ($app->cache)
60 $app->plugin('Newslash::Plugin::TimeLimitedCache');
62 # use KeyValue Store ($app->kvs)
63 $app->plugin('Newslash::Plugin::KeyValueStore');
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);
71 # use Model Cache ($app->model_cache)
72 $app->plugin('Newslash::Plugin::ModelCache');
74 # use Template::Toolkit 2 render
75 $app->plugin('Newslash::Plugin::TT2Renderer');
77 # user AntiCsrf ($app->anti_csrf)
78 $app->plugin('Newslash::Plugin::AntiCsrf');
81 #$app->plugin('Newslash::Plugin::CSSCompile');
83 # quasi-static content
84 $app->plugin('Newslash::Plugin::QuasiStaticContent');
87 $app->plugin('Newslash::Plugin::UserAuth');
90 $app->plugin('Newslash::Plugin::AccessControl');
93 $app->plugin('Newslash::Plugin::ReCaptcha');
96 $app->plugin('Newslash::Plugin::EventQue');
99 ############################################################
101 # Generate site-global used javascript file
103 ############################################################
104 my $templ_name = "common/siteconfig.js";
105 my $mod_reasons = $app->model('moderations')->reasons();
106 my $topics = $app->model('tags')->get_topics;
107 my @acl2_types = $app->model('users')->acl2_types;
109 for my $topic (@$topics) {
110 my $lc_keyword = lc($topic->{keyword});
111 my $lc_textname = lc($topic->{textname});
112 $keywords->{$lc_keyword} = {keyword => $topic->{keyword},
113 textname => $topic->{textname},
114 image => $topic->{image}};
115 if ($lc_keyword ne $lc_textname) {
116 $keywords->{$lc_textname} = $keywords->{$lc_keyword};
120 moderate_reasons => $mod_reasons,
122 acl2_types => \@acl2_types,
124 my $siteconfig = $app->tt2renderer->render($templ_name, $vars);
125 $app->static_content->add_content("js/siteconfig.js", $siteconfig, "text/javascript; charset=utf-8");
127 ############################################################
131 ############################################################
133 my $r = $app->routes;
136 $r->get('/')->to('index#root');
137 $r->get('/recent')->to('timeline#recent');
138 $r->get('/journals')->to('index#journals');
139 $r->get('/comments')->to('index#comments');
140 $r->get('/submissions')->to('index#submissions');
143 $r->get('/banned')->to('index#banned', noindex => 1);
146 $r->get('/story/:year/:month/:day/' => [year => qr/[0-9]{2}/,
147 month => qr/[0-9]{2}/,
148 day => qr/[0-9]{2}/])->to('index#story_archive');
152 $r->get('/login')->to('login#login');
153 $r->post('/login')->to('login#login');
154 $r->get('/logout')->to('login#logout');
157 $r->get('/my/newuser')->to('login#newuser');
158 $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
161 $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
165 $r->get('/journal/new')->to('journal#create', seclev => 1);
166 $r->get('/journal/:id/')->to('journal#journal');
169 $r->get('/submission/new')->to('submission#create');
170 $r->get('/submission/:id/')->to('submission#submission');
171 #$r->post('/submission')->to('submission#create');
174 $r->get('/my/settings')->to('user#settings', seclev => 1);
175 $r->get('/my/sidebar')->to('user#sidebar', seclev => 1);
176 $r->get('/my/messages')->to('my#messages', seclev => 1);
177 $r->get('/my/')->to('user#home', seclev => 1);
180 # pages under /admin needs seclev equal or greater than 10000;
181 my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
183 $admin->get('/submissions')->to('admin-submissions#index');
184 $admin->get('/submissions/list')->to('admin-submissions#list');
186 $admin->get('/css')->to('admin-css#edit');
187 $admin->get('/story/edit')->to('admin-story#edit');
189 $admin->get('/users')->to('admin-users#index');
191 $admin->get('/sidebar')->to('admin-sidebar#index');
192 $admin->post('/sidebar/update')->to('admin-sidebar#update');
193 $admin->post('/sidebar/delete')->to('admin-sidebar#delete');
194 $admin->any(['GET', 'POST'] => '/sidebar/list')->to('admin-sidebar#list');
195 $admin->post('/sidebar/get')->to('admin-sidebar#get');
198 my $api = $r->under('/api/v1');
199 $api->get('/sidebars')->to('admin-sidebar#list');
201 $api->post('/login')->to('API::Login#login');
203 $api->get('/comment')->to('API::Comment#get');
204 $api->post('/comment')->to('API::Comment#post', captcha_check => 1);
206 $api->get('/user')->to('API::User#get');
207 $api->post('/user')->to('API::User#post', seclev => 1);
209 $api->get('/journal')->to('API::Journal#get');
210 $api->post('/journal')->to('API::Journal#post', seclev => 1, csrf_check_id => 'journal');
212 $api->get('/submission')->to('API::Submission#get');
213 $api->post('/submission')->to('API::Submission#post', captcha_check => 1, csrf_check_id => 'submission');
215 $api->get('/story')->to('API::Story#get');
216 #$api->post('/story')->to('API::Story#post');
218 $api->get('/moderation')->to('API::Moderation#get');
219 $api->post('/moderation')->to('API::Moderation#post', seclev => 1, csrf_check_id => 'moderation');
221 $api->get('/metamoderation')->to('API::Metamoderation#get');
222 $api->post('/metamoderation')->to('API::Metamoderation#post', seclev => 1, csrf_check_id => 'moderation');
224 $api->post('/relation')->to('API::Relation#post', seclev => 1, csrf_check_id => 'relation');
226 $api->get('/token')->to('API::Token#get');
229 # warning: these pathes uses regexp matching, so must write in tail of route definitions.
230 my $user = $r->under('/:nickname');
231 $user->get('/' => [nickname => qr/~.*/])->to('user#home');
232 $user->get('/journals' => [nickname => qr/~.*/])->to('user#journals');
233 $user->get('/comments' => [nickname => qr/~.*/])->to('user#comments');
234 $user->get('/submissions' => [nickname => qr/~.*/])->to('user#submissions');
235 $user->get('/friends' => [nickname => qr/~.*/])->to('user#friends');
236 $user->get('/foes' => [nickname => qr/~.*/])->to('user#foes');
237 $user->get('/fans' => [nickname => qr/~.*/])->to('user#fans');
238 $user->get('/freaks' => [nickname => qr/~.*/])->to('user#freaks');
239 $user->get('/achievements' => [nickname => qr/~.*/])->to('user#achievements');
240 #$r->get('/:user_name/journal' => [user_name => qr/~.*/])->to('journal#user_journals');