burguillos.info/lib/BurguillosInfo.pm

118 lines
4.3 KiB
Perl

package BurguillosInfo;
use BurguillosInfo::Controller::Metrics;
use Mojo::Base 'Mojolicious', -signatures;
# This method will run once at server start
sub startup ($self) {
my $metrics = BurguillosInfo::Controller::Metrics->new;
$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(
before_render => sub ( $c, $args ) {
my $current_route = $c->url_for;
$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};
my $base_url = $self->config->{base_url};
if ( !defined $onion_base_url ) {
return;
}
$current_route =~ s/^$base_url//;
$c->res->headers->header(
'Onion-Location' => $onion_base_url . $current_route );
}
);
my $config = $self->plugin('JSONConfig');
$self->config(
hypnotoad => {
proxy => 1,
listen => [ $self->config('listen') // 'http://localhost:3000' ]
}
);
$self->config( css_version => int( rand(10000) ) );
$self->secrets( $self->config->{secrets} );
$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;
}
);
# Router
my $r = $self->routes;
# Normal route to controller
$r->get('/')->to('Page#index');
$r->get('/privacy.txt')->to('Privacy#index');
$r->get('/sitemap.xml')->to('Sitemap#sitemap');
$r->get('/robots.txt')->to('Robots#robots');
$r->get('/stats')->to('Metrics#stats');
$r->get('/conquer')->to('Conquer#index');
$r->put('/conquer/user')->to('UserConquer#create');
$r->post('/conquer/user/coordinates')->to('UserConquer#setCoordinates');
$r->put('/conquer/node')->to('ConquerNode#create');
$r->get('/conquer/node/near')->to('ConquerNode#nearbyNodes');
$r->get('/conquer/user')->to('UserConquer#get_self');
$r->post('/conquer/user/login')->to('UserConquer#login');
$r->get('/conquer/tile/<zoom>/<x>/<y>.png')->to('ConquerTile#tile');
$r->get('/search.json')->to('Search#search');
$r->get('/farmacia-guardia.json')->to('FarmaciaGuardia#current');
$r->get('/<:category>.rss')->to('Page#category_rss');
$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');
$r->get('/:category')->to('Page#category');
$r->get('/posts/<:slug>-preview.png')->to('Page#get_post_preview');
$r->get('/posts/:slug')->to('Page#post');
$r->get('/next-ad.json')->to('Ads#next_ad');
$r->get('/filtros')->to('Filter#list');
$r->get('/filtros/:slug')->to('Filter#get');
$r->get('/stats/login')->to('Metrics#login');
$r->post('/stats/login')->to('Metrics#submit_login');
}
1;