OSDN Git Service

Plugin::Stories: add storiees->latest helper
[newslash/newslash.git] / src / newslash_web / lib / Newslash / Web.pm
index 6457b0e..a497974 100644 (file)
@@ -1,23 +1,39 @@
 package Newslash::Web;
 use Mojo::Base 'Mojolicious';
 use Mojo::Util qw(dumper);
-
-use Newslash::Model;
+use List::Util qw(any);
 
 use constant CONFIG_FILE => '/etc/newslash/newslash.conf';
+has subcommand => "";
 
 # This method will run once at server start
 sub startup {
     my $app = shift;
 
+    if ($ARGV[0]) {
+        $app->subcommand($ARGV[0]);
+    }
+
+    # add commands in Newslash::Command
+    push @{$app->commands->namespaces}, 'Newslash::Command';
+
     # load config file
     # first, check existence of /etc/newslash.conf
     if ($app->mode eq 'production' && -e CONFIG_FILE) {
         $app->plugin('Newslash::Plugin::YAMLConfig', file => CONFIG_FILE);
     }
     else {
-        $app->plugin('Newslash::Plugin::YAMLConfig');
+       if ($app->subcommand eq "configdump") {
+           $app->plugin('Newslash::Plugin::YAMLConfig', default => {});
+       }
+       else {
+           $app->plugin('Newslash::Plugin::YAMLConfig');
+       }
     }
+
+    # load default configuration values
+    $app->plugin('Newslash::Plugin::DefaultConfig');
+
     # TODO: load/save configs with database
 
     if ($app->config->{Log} && $app->config->{Log}->{backtrace}) {
@@ -25,41 +41,49 @@ sub startup {
         Carp::Always->import;
     }
 
-    # set log file
+    # system log config
     if ($app->config->{Log} && $app->config->{Log}->{system_log}) {
-        # check log is writable
-        my $pathname = $app->config->{Log}->{system_log};
-        if (-e $pathname) {
-            if (-w $pathname) {
-                $app->log->debug("logs will be outputed to $pathname ...");
-                $app->log(Mojo::Log->new);
-                $app->log->path($pathname);
-            }
-            else {
-                $app->log->error("cannot write system log to file: $pathname");
-            }
-        }
-        else {
-            if (open(my $fh, ">", $pathname)) {
-                close($fh);
-                $app->log->debug("logs will be outputed to $pathname ...");
-                $app->log(Mojo::Log->new);
-                $app->log->path($pathname);
-            }
-            else {
-                $app->log->error("cannot create system log file: $pathname");
-            }
-        }
-    }
-
-    # set log level
-    if ($app->config->{Log} && $app->config->{Log}->{level}) {
-        my $loglv = $app->config->{Log}->{level};
-        if (grep { $loglv eq $_ } qw(debug info warn error fatal)) {
-            $app->log->level($loglv);
-        }
-        else {
-            $app->log->warn('invalid log level given in config file');
+       my $cnf = $app->config->{Log}->{system_log};
+
+       # set log level
+       if ($cnf->{level}) {
+           my $loglv = $cnf->{level};
+           if (grep { $loglv eq $_ } qw(debug info warn error fatal)) {
+               $app->log->level($loglv);
+           }
+           else {
+               $app->log->warn('invalid log level given in config file');
+           }
+       }
+
+       # file output settings
+       if ($cnf->{mode} eq "local_file") {
+           my $pathname = $cnf->{local_file};
+           # check log is writable
+           if (!$pathname) {
+               $app->log->error("cannot write system log to file: filename not given");
+           }
+           elsif (-e $pathname) {
+               if (-w $pathname) {
+                   $app->log->debug("logs will be outputed to $pathname ...");
+                   $app->log(Mojo::Log->new);
+                   $app->log->path($pathname);
+               }
+               else {
+                   $app->log->error("cannot write system log to file: $pathname");
+               }
+           }
+           else {
+               if (open(my $fh, ">", $pathname)) {
+                   close($fh);
+                   $app->log->debug("logs will be outputed to $pathname ...");
+                   $app->log(Mojo::Log->new);
+                   $app->log->path($pathname);
+               }
+               else {
+                   $app->log->error("cannot create system log file: $pathname");
+               }
+           }
         }
     }
 
@@ -70,15 +94,28 @@ sub startup {
         }
     }
 
+    # secret key for hasing
+    $app->secrets([$app->config->{System}->{secret_key},]);
+
+    # when "test" mode, output debug logs.
+    $app->log->level('debug') if $app->mode eq 'test';
+
+    # check if 'maintenance' mode
+    my $maintenance_mode = 0;
+    if ($app->subcommand
+        && any { $app->subcommand eq $_ } qw[configdump configimport databaseinit
+                                             testdatainsert useradd usermod
+                                           ]) {
+       $maintenance_mode = 1;
+    }
+
     ############################################################
     #
     # Plugin Settings
     #
     ############################################################
 
-    # when "test" mode, output debug logs.
-    $app->log->level('debug') if $app->mode eq 'test';
-
+    # profiler settings
     if ($app->mode eq 'development'
         && $app->config->{Profiler}
         && $app->config->{Profiler}->{enable}) {
@@ -87,12 +124,8 @@ sub startup {
         }
     }
 
-    # enable logging
-    $app->plugin('Newslash::Plugin::AccessLog::Debug', $app->config->{Log} || {});
-    $app->plugin('Newslash::Plugin::AccessLog::LocalFile', $app->config->{Log} || {});
-
-    # secret key for hasing
-    $app->secrets([$app->config->{System}->{secret_key},]);
+    # enable access logging
+    $app->plugin('Newslash::Plugin::AccessLog');
 
     # Helpers for Newslash
     $app->plugin('Newslash::Plugin::NewslashHelpers');
@@ -100,10 +133,8 @@ sub startup {
     # use Epoch
     $app->plugin('Newslash::Plugin::Epoch');
 
-    # use BasicAuth?
-    if ($app->config->{BasicAuth} && $app->config->{BasicAuth}->{enable}) {
-        $app->plugin('Newslash::Plugin::BasicAuth');
-    }
+    # support BasicAuth
+    $app->plugin('Newslash::Plugin::BasicAuth');
 
     # use TimeLimitedCache ($app->cache)
     $app->plugin('Newslash::Plugin::TimeLimitedCache');
@@ -112,10 +143,7 @@ sub startup {
     $app->plugin('Newslash::Plugin::KeyValueStore');
 
     # add Model Loader
-    my $model_opts = $app->config;
-    $model_opts->{Logger} = $app->log;
-    $app->helper(model => Newslash::Model::loader($model_opts));
-    Newslash::Model::startup($model_opts, $app);
+    $app->plugin('Newslash::Plugin::Model', { bypass_startup => $maintenance_mode });
 
     # use Easy Cache ($app->ezcache)
     $app->plugin('Newslash::Plugin::EasyCache');
@@ -123,20 +151,32 @@ sub startup {
     # use Template::Toolkit 2 render
     $app->plugin('Newslash::Plugin::TT2Renderer');
 
+    # use ResponseFilter (Faculities)
+    $app->plugin('Newslash::Plugin::ResponseFilter');
+
     # use CustomBoxes
     $app->plugin('Newslash::Plugin::CustomBoxes');
 
-    # user AntiCsrf ($app->anti_csrf)
-    if ($app->mode ne 'test') {
-        # when test mode, disable AntiCsrf.
-        $app->plugin('Newslash::Plugin::AntiCsrf');
-    }
+    # use AntiCsrf
+    $app->plugin('Newslash::Plugin::AntiCsrf');
 
     # contents preprocessor
     $app->plugin('Newslash::Plugin::Preprocessor');
+    if ($maintenance_mode) {
+        $app->log->info("bypassing preprocessor...");
+    }
+    else {
+       $app->preprocessor->generate_all;
+    }
 
     # javascript loader
     $app->plugin('Newslash::Plugin::JavaScriptLoader');
+    if ($maintenance_mode) {
+        $app->log->info("bypassing javascriptloader...");
+    }
+    else {
+       $app->javascript_loader->load_all_bundles;
+    }
 
     # user authorization
     $app->plugin('Newslash::Plugin::UserAuth');
@@ -145,9 +185,7 @@ sub startup {
     $app->plugin('Newslash::Plugin::AccessControl');
 
     # ReCaptcha control
-    if ($app->mode ne 'test') {
-        $app->plugin('Newslash::Plugin::ReCaptcha');
-    }
+    $app->plugin('Newslash::Plugin::ReCaptcha');
 
     # set canocal (for test.srad.jp)
     $app->plugin('Newslash::Plugin::Canonical');
@@ -173,6 +211,17 @@ sub startup {
     # AD renderer
     $app->plugin('Newslash::Plugin::ADRenderer');
 
+    # HTTP Compression
+    $app->plugin('Newslash::Plugin::HttpCompression');
+
+    # Users helper
+    $app->plugin('Newslash::Plugin::Users');
+
+    # Stories helper
+    $app->plugin('Newslash::Plugin::Stories');
+
+    # Sendmail helper
+    $app->plugin('Newslash::Plugin::Sendmail');
 
     ############################################################
     #
@@ -191,6 +240,9 @@ sub startup {
     $r->get('/submissions')->to('timeline#submissions');
     $r->get('/polls')->to('timeline#polls');
 
+    # RSS
+    $r->get('/rss/sradjp' => [format => ['rss', 'xml']])->to('rss#sradjp');
+
     # Banned page
     $r->get('/banned')->to('index#banned', noindex => 1);
 
@@ -199,10 +251,6 @@ sub startup {
     $r->post('/login')->to('login#login');
     $r->get('/logout')->to('login#logout');
 
-    # User Register
-    $r->get('/my/newuser')->to('login#newuser');
-    $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
-
     # story page
     $r->get('/story/:sid/' => [sid => qr|\d\d/\d\d/\d\d/\d+|])
       ->to('story#single');
@@ -251,6 +299,16 @@ sub startup {
     $r->get('/my/messages')->to('my#messages', seclev => 1);
     $r->get('/my/')->to('user#home', seclev => 1);
 
+    # User Register
+    $r->get('/my/newuser')->to('login#newuser');
+    $r->post('/my/newuser')->to('login#newuser', captcha_check => 1);
+    $r->get('/my/activation')->to('login#activation');
+    $r->get('/my/resetpassword')->to('login#reset_password');
+    $r->post('/my/resetpassword')->to('login#reset_password', captcha_check => 1);
+
+    # Change Email
+    $r->get('/my/change_email')->to('my#change_email', seclev => 1);
+
     # search page
     $r->get('/search')->to('search#search');
 
@@ -300,6 +358,10 @@ sub startup {
     my $api = $r->under('/api/v1');
     $api->post('/login')->to('API::Login#login');
 
+    $api->post('/newuser/validate')->to('API::User#validate_new_user');
+    $api->post('/newuser/create')->to('API::User#create_new_user');
+    $api->post('/newuser/password')->to('API::User#newuser_password');
+
     $api->get('/sidebar/item')->to('API::SidebarItem#get', seclev => 1);
 
     $api->get('/comment')->to('API::Comment#get');