#!/usr/bin/env perl #------------- #--- MODEL --- #------------- package PGapp::PGmodel; use Data::Dumper; use DBI; use strict; use warnings; sub new { my ($class, $app, $pghost, $username, $password) = @_; my $self = { app => $app, pghost => $pghost, dsn => "dbi:Pg:dbname=postgres;host=$pghost", username => $username, password => $password, }; bless $self, $class; return $self; } sub dsn { return shift->{dsn}; } sub username { return shift->{username}; } sub password { return shift->{password}; } sub dbsize { my ($self, $dbname) = @_; my $db = DBI->connect($self->dsn, $self->username, $self->password) or return -1; my $query = "select pg_database_size('$dbname');"; my $sth = $db->prepare($query); my $rows = $sth->execute or return -1; my $row = $sth->fetchrow_array; $sth->finish; $db->disconnect; my $dbsize = int($row/1024/1024); return $dbsize; } sub dblist { my $self = shift; my $db = DBI->connect($self->dsn, $self->username, $self->password) or return -1; my $query = "select datname from pg_stat_database;"; my $sth = $db->prepare($query); my $rows = $sth->execute; my @dblist; while (my $row = $sth->fetchrow_hashref) { my $dbname = $row->{'datname'}; push @dblist, $dbname; } $sth->finish; $db->disconnect; return \@dblist; } 1; #------------------ #--- CONTROLLER --- #------------------ package PGapp::PGcont; use utf8; use strict; use warnings; use Mojo::Base 'Mojolicious::Controller'; use Mojo::Util qw(b64_encode b64_decode md5_sum dumper url_escape); use Mojo::JSON qw(encode_json decode_json); sub hello { my $self = shift; $self->render(text => 'Hello'); } sub confdump { my $self = shift; my $pg = $self->app->pg; my $list = $pg->dblist; $self->render(text => encode_json($self->app->config)); } sub dblist { my $self = shift; my $pg = $self->app->pg; my $dblist = $pg->dblist; $self->render(text => encode_json($dblist)); } 1; #------------ #--- APP --- #------------ package PGapp; use utf8; use strict; use warnings; use Mojo::Base 'Mojolicious'; sub startup { my $self = shift; } 1; #------------ #--- MAIN --- #------------ use strict; use warnings; use utf8; use Mojo::Util qw(monkey_patch b64_encode b64_decode md5_sum getopt dumper); use Mojo::Server::Daemon; my $server = Mojo::Server::Daemon->new; my $app = $server->build_app('PGapp'); $app->config( pghost => "127.0.0.1", pguser => "postgres", pgpasswd => "superpasssword" ); $app->helper(pg => sub { state $pg = PGapp::PGmodel->new( $app, $app->config("pghost"), $app->config("pguser"), $app->config("pgpasswd") ); } ); my $r = $app->routes; $r->any('/')->to('PGcont#hello'); $r->any('/dblist')->to('PGcont#dblist'); $r->any('/confdump')->to('PGcont#confdump'); $server->run; #EOF
# ./mojo-appl.pl [Mon Sep 25 13:36:58 2017] [info] Listening at "http://*:3000" Server available at http://127.0.0.1:3000 [Mon Sep 25 13:37:02 2017] [debug] GET "/dblist" [Mon Sep 25 13:37:02 2017] [debug] Routing to controller "PGapp::PGcont" and action "dblist" [Mon Sep 25 13:37:02 2017] [debug] 200 OK (0.045402s, 22.025/s)
# curl http://127.0.0.1:3000/dblist ["postgres","asterisk","template0","dashboard_development","dashboard_production","dashboard_test"]
[<>]