Skip to content

Instantly share code, notes, and snippets.

@marcusramberg
Created February 4, 2019 14:59
Show Gist options
  • Save marcusramberg/a060ec04325aa4467f08b6c2dd72bd97 to your computer and use it in GitHub Desktop.
Save marcusramberg/a060ec04325aa4467f08b6c2dd72bd97 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Mojolicious::Lite;
_setup_status_page(app);
get '/' => sub {
my $c = shift;
$c->render_later;
$c->ua->get('/internal', sub {
my ($ua,$tx)=@_;
$c->render(text=>$tx->res->json('/res'));
});
};
get '/internal' => sub {
my $c = shift;
$c->render( json=> {res=>'2'} );
};
sub _setup_status_page {
# Secure access to the server status ui with Basic authentication
my $self = shift;
my $under = $self->routes->under('/status' =>sub {
my $c = shift;
return 1 if $c->req->url->to_abs->userinfo eq 'admin:password';
$c->res->headers->www_authenticate('Basic');
$c->render(text => 'Authentication required!', status => 401);
return undef;
});
$self->plugin('Status', {route => $under });
}
app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment