12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #!/usr/bin/perl
- use local::lib;
- use v5.10;
- use strict;
- use warnings;
- use FindBin qw( $RealBin );
- use lib "$RealBin/lib";
- BEGIN { $ENV{TZ} = 'UTC' }
- use LogBot::Config qw( find_config load_config );
- use LogBot::Util qw( file_time );
- use LogBot::Web::Util qw( rewrite_old_urls );
- use Mojo::URL ();
- use Mojolicious::Lite qw( app );
- # build default url
- my $default_network = $ENV{LOGBOT_DEFAULT_NETWORK} // die 'LOGBOT_DEFAULT_NETWORK not set';
- my $default_config = load_config(find_config($default_network), web => 1);
- my $default_url = Mojo::URL->new($default_config->{url});
- sub redirect {
- my ($c) = @_;
- $c->stash(config => $default_config);
- my $redirect_url = rewrite_old_urls($c);
- # redirect old server-side url
- if (defined($c->req->query_params->param('cid'))) {
- say "redirecting old url to $redirect_url" if $ENV{DEBUG};
- return $c->redirect_to($redirect_url);
- }
- # redirect old urls
- if ($redirect_url) {
- say "redirecting old url to $redirect_url" if $ENV{DEBUG};
- $c->stash(redirect_to => $redirect_url);
- return $c->render('redirect');
- }
- # redirect to default network
- my $req_url = $c->req->url;
- my $url = $default_url->clone();
- $url->path($req_url->path);
- $url->query($req_url->query);
- say "redirecting to $url" if $ENV{DEBUG};
- $c->redirect_to($url);
- }
- # static file with timestamp
- my %cache;
- helper static => sub {
- my ($self, $file) = @_;
- return $cache{static}->{$file} //= '/static/' . $file . '?' . file_time($RealBin . '/web/public/static/' . $file);
- };
- # configure mojo
- app->secrets('!logbot!');
- app->renderer->paths([$RealBin . '/web/templates']);
- app->static->paths([$RealBin . '/web/public']);
- app->config(
- hypnotoad => {
- listen => ['http://127.0.0.1:' . ($ENV{LOGBOT_PORT} // 3001)],
- pid_file => ($ENV{LOGBOT_PID_FILE} // $RealBin . '/logbot-web.pid'),
- workers => 2,
- },
- );
- get '/' => \&redirect;
- get '/*' => \&redirect;
- app->start;
|