burguillos.info/lib/BurguillosInfo.pm

128 lines
4.9 KiB
Perl
Raw Permalink Normal View History

2022-11-09 13:31:58 +01:00
package BurguillosInfo;
use BurguillosInfo::Controller::Metrics;
2022-11-09 13:31:58 +01:00
use Mojo::Base 'Mojolicious', -signatures;
# This method will run once at server start
sub startup ($self) {
2022-11-15 23:57:20 +01:00
my $metrics = BurguillosInfo::Controller::Metrics->new;
2024-01-13 01:17:57 +01:00
$self->sessions->default_expiration(0);
2022-11-15 23:57:20 +01:00
$self->hook(
around_dispatch => sub {
my $next = shift;
my $c = shift;
$metrics->request($c);
if ( defined $next ) {
$next->();
}
}
);
push @{ $self->commands->namespaces }, 'BurguillosInfo::Command';
$self->hook(
2023-11-20 20:20:04 +01:00
before_render => sub ( $c, $args ) {
my $current_route = $c->url_for;
2023-11-20 20:20:04 +01:00
$c->stash( current_route => $current_route );
my $is_android = $c->req->headers->user_agent =~ /android/i;
$c->stash( is_android => $is_android );
my $onion_base_url = $self->config->{onion_base_url};
2023-11-20 20:20:04 +01:00
my $base_url = $self->config->{base_url};
if ( !defined $onion_base_url ) {
return;
}
$current_route =~ s/^$base_url//;
2023-11-20 20:20:04 +01:00
$c->res->headers->header(
'Onion-Location' => $onion_base_url . $current_route );
}
);
2022-11-15 23:57:20 +01:00
my $config = $self->plugin('JSONConfig');
$self->config(
2023-11-20 20:20:04 +01:00
hypnotoad => {
proxy => 1,
listen => [ $self->config('listen') // 'http://localhost:3000' ]
}
);
$self->config( css_version => int( rand(10000) ) );
$self->secrets( $self->config->{secrets} );
2022-11-15 23:57:20 +01:00
2023-11-20 20:20:04 +01:00
$self->helper(
current_user => sub ($c) {
use BurguillosInfo::Schema;
$self->session(expiration => 0);
my $user_uuid = $c->session->{conquer}{user};
if ( !defined $user_uuid ) {
return;
}
my $user_resultset =
BurguillosInfo::Schema->Schema->resultset('ConquerUser');
my @user_candidates =
$user_resultset->search( { uuid => $user_uuid } );
my $user = $user_candidates[0];
# Just to make clear what happens if there is no user we return.
if ( !defined $user ) {
return;
}
return $user;
}
);
$self->helper(
set_current_user => sub ( $c, $user ) {
$self->session(expiration => 0);
if ( !defined $user
|| !$user->can('uuid')
|| !$user->can('get_from_storage') )
{
die "$user is not a valid user for it's usage in a session.";
}
$user = $user->get_from_storage;
$c->session->{conquer}{user} = $user->uuid;
}
);
2022-11-15 23:57:20 +01:00
# Router
my $r = $self->routes;
# Normal route to controller
$r->get('/')->to('Page#index');
2023-09-01 01:37:27 +02:00
$r->get('/privacy.txt')->to('Privacy#index');
$r->get('/sitemap.xml')->to('Sitemap#sitemap');
2023-05-03 01:02:51 +02:00
$r->get('/robots.txt')->to('Robots#robots');
2022-11-15 23:57:20 +01:00
2022-11-17 00:44:20 +01:00
$r->get('/stats')->to('Metrics#stats');
2023-11-13 17:32:12 +01:00
$r->get('/conquer')->to('Conquer#index');
2023-11-17 23:16:54 +01:00
$r->put('/conquer/user')->to('UserConquer#create');
2024-01-13 01:17:57 +01:00
$r->get('/conquer/user/team')->to('UserConquer#getSelfTeam');
$r->post('/conquer/user/team')->to('UserConquer#setTeamForUser');
2023-12-31 20:43:53 +01:00
$r->post('/conquer/user/coordinates')->to('UserConquer#setCoordinates');
2024-01-13 01:17:57 +01:00
$r->get('/conquer/team/<uuid>')->to('ConquerTeam#get');
$r->put('/conquer/team')->to('ConquerTeam#put');
2024-01-13 23:14:14 +01:00
$r->get('/conquer/teams')->to('ConquerTeam#getAll');
2023-12-31 20:43:53 +01:00
$r->put('/conquer/node')->to('ConquerNode#create');
$r->get('/conquer/node/near')->to('ConquerNode#nearbyNodes');
$r->get('/conquer/user/enemies/global')->to('ConquerUserCurrentEnemy#listEnemiesGlobal');
2024-01-23 20:19:26 +01:00
$r->post('/conquer/user/enemies/fight')->to('ConquerUserCurrentEnemy#fightEnemy');
2024-01-13 01:17:57 +01:00
$r->get('/conquer/node/<uuid>')->to('ConquerNode#get');
$r->post('/conquer/node/<uuid>/try-conquer')->to('ConquerNode#tryConquer');
2023-11-20 20:20:04 +01:00
$r->get('/conquer/user')->to('UserConquer#get_self');
2023-11-19 23:14:02 +01:00
$r->post('/conquer/user/login')->to('UserConquer#login');
2023-11-29 19:43:56 +01:00
$r->get('/conquer/tile/<zoom>/<x>/<y>.png')->to('ConquerTile#tile');
2023-09-04 12:54:38 +02:00
$r->get('/search.json')->to('Search#search');
2023-09-07 18:12:28 +02:00
$r->get('/farmacia-guardia.json')->to('FarmaciaGuardia#current');
2022-11-15 23:57:20 +01:00
$r->get('/<:category>.rss')->to('Page#category_rss');
2023-11-20 20:20:04 +01:00
$r->get('/:category_slug/atributo/<:attribute_slug>-preview.png')
->to('Attribute#get_attribute_preview');
$r->get('/:category_slug/atributo/:attribute_slug')->to('Attribute#get');
$r->get('/<:category>-preview.png')->to('Page#get_category_preview');
2022-11-15 23:57:20 +01:00
$r->get('/:category')->to('Page#category');
2023-05-04 00:13:49 +02:00
$r->get('/posts/<:slug>-preview.png')->to('Page#get_post_preview');
2022-11-15 23:57:20 +01:00
$r->get('/posts/:slug')->to('Page#post');
2023-08-21 00:58:11 +02:00
$r->get('/next-ad.json')->to('Ads#next_ad');
$r->get('/filtros')->to('Filter#list');
$r->get('/filtros/:slug')->to('Filter#get');
2022-11-17 00:44:20 +01:00
$r->get('/stats/login')->to('Metrics#login');
$r->post('/stats/login')->to('Metrics#submit_login');
2022-11-09 13:31:58 +01:00
}
1;