Created
October 15, 2013 00:00
-
-
Save jberger/6984421 to your computer and use it in GitHub Desktop.
Mojolicious response to http://jmorano.moretrix.com/2013/10/google-geochart-json-perl/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
use DBIx::Connector; | |
helper db => sub { | |
state $db = DBIx::Connector->connect("dbi:mysql:host=localhost;db=testdb", 'testuser', 'xxxsecret') | |
}; | |
helper get_data => sub { | |
my $db = shift->db; | |
my $query = "SELECT country_name, count(id) as total from geo_data group by country_name"; | |
my $data = $db->selectall_arrayref($query); | |
$_->[1] = int($_->[1]) foreach @$data; | |
unshift(@$data, ['Country', 'Attacks']); | |
return $data; | |
}; | |
any '/' => sub { | |
my $c = shift; | |
my $data = $c->get_data; | |
$c->render( json => $data ); | |
}; | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment