This repository has been archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathlogbot-redirect
executable file
·73 lines (60 loc) · 1.96 KB
/
logbot-redirect
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/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;