Permalink
Browse files

Kossyに移植

  • Loading branch information...
0 parents commit bb21822385046e33866468d2bbf31b7a096c9da1 hisaichi5518 committed Dec 20, 2012
@@ -0,0 +1,15 @@
+cover_db
+META.yml
+Makefile
+blib
+pm_to_blib
+MANIFEST
+Makefile.old
+nytprof.out
+MANIFEST.bak
+*.sw[po]
+*~
+.DS_Store
+MYMETA.*
+config/*.pl
+!config/common.sample.pl
@@ -0,0 +1,13 @@
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+ NAME => 'Iyemon',
+ VERSION_FROM => 'lib/Iyemon.pm',
+ PREREQ_PM => {
+ 'Kossy' => '0.13',
+ 'MongoDB' => '0.45',
+ 'Time::Piece' => '1.20_01',
+ },
+ MIN_PERL_VERSION => '5.008001'
+);
+
@@ -0,0 +1,18 @@
+use FindBin;
+use lib "$FindBin::Bin/extlib/lib/perl5";
+use lib "$FindBin::Bin/lib";
+use File::Basename;
+use Plack::Builder;
+use Iyemon::Web;
+
+my $root_dir = File::Basename::dirname(__FILE__);
+
+my $app = Iyemon::Web->psgi($root_dir);
+builder {
+ enable 'ReverseProxy';
+ enable 'Static',
+ path => qr!^/(?:(?:css|js|img)/|favicon\.ico$)!,
+ root => $root_dir . '/public';
+ $app;
+};
+
@@ -0,0 +1,8 @@
+{
+ 'MongoDB::Connection' => {
+ host => 'localhost',
+ port => 27017,
+ database => 'test',
+ collection => 'test',
+ },
+}
@@ -0,0 +1,10 @@
+package Iyemon;
+
+use strict;
+use warnings;
+use utf8;
+
+our $VERSION = 0.01;
+
+1;
+
@@ -0,0 +1,10 @@
+package Iyemon::Config;
+use strict;
+use warnings;
+use Config::ENV 'PLACK_ENV', export => 'config';
+
+common {
+ load('./config/common.pl'),
+};
+
+1;
@@ -0,0 +1,113 @@
+package Iyemon::Web;
+
+use strict;
+use warnings;
+use utf8;
+use Kossy;
+use Scalar::Util qw/looks_like_number blessed/;
+use DateTime;
+use DateTime::Format::HTTP;
+use MongoDB;
+use Iyemon::Config;
+
+get '/' => sub {
+ my ($self, $c) = @_;
+ $c->render('index.tx');
+};
+
+get '/search' => sub {
+ my ($self, $c) = @_;
+
+ my $mongo_config = config->param('MongoDB::Connection');
+ my $mongo = MongoDB::Connection->new($mongo_config)
+ ->get_database($mongo_config->{database})
+ ->get_collection($mongo_config->{collection});
+
+ my $opts = {limit => 100};
+ my $criteria = {};
+ my @num_keys = qw/uid/;
+ my @str_keys = qw/type/;
+
+ for my $key (@num_keys) {
+ $criteria->{$key} = $c->req->param($key) + 0
+ if $c->req->param($key);
+ }
+ for my $key (@str_keys) {
+ $criteria->{$key} = $c->req->param($key)
+ if $c->req->param($key);
+ }
+
+ my %date;
+ for my $type (qw/start end/) {
+ if (my $date = $c->req->param("$type\_date")) {
+ my $t = DateTime::Format::HTTP->parse_datetime($date);
+ $date{$type} = $t;
+ }
+ else {
+ if ($type eq 'start') {
+ $c->halt(400);
+ }
+ elsif ($type eq 'end') {
+ my $dt = DateTime->now;
+ $date{$type} = $dt;
+ }
+ }
+ }
+ $criteria->{time} = {'$gte' => $date{start}, '$lte' => $date{end}};
+ my $page = $c->req->param('page') || 1;
+ if ($page ne 'NaN' && looks_like_number $page) {
+ $opts->{page} = $page;
+ }
+ else {
+ $c->halt(400);
+ }
+
+ %date = map {
+ my $dt = $date{$_};
+ $dt->set_time_zone('Asia/Tokyo');
+ $_ => "$dt";
+ } keys %date;
+
+ my $obj = {};
+ if ($c->req->param("count")) {
+ my %args = map { $c->req->param($_) ? ($_ => $c->req->param($_)) : () } @num_keys, @str_keys, 'date', 'page';
+ $obj = {
+ %args,
+ %date,
+ result => $mongo->count($criteria) || 0,
+ };
+ }
+ else {
+ my @logs = $mongo->find($criteria, $opts)->all;
+ my $results = [];
+ if (@logs) {
+ $results = [map { _jsonize_log($_) } @logs];
+ }
+
+ $obj = {
+ results => $results,
+ %date,
+ };
+ }
+
+ $c->render_json($obj);
+};
+
+sub _jsonize_log {
+ my $obj = shift;
+
+ for my $key (keys %$obj) {
+ my $v = $obj->{$key};
+ if ( blessed $v && $v->isa("DateTime") ) {
+ $v->set_time_zone(config->param('time_zone') || 'Asia/Tokyo');
+ }
+ $obj->{$key} = sprintf "%s", $v
+ if blessed $v;
+ }
+ $obj;
+}
+
+
+
+
+1;
Oops, something went wrong.

0 comments on commit bb21822

Please sign in to comment.