Last active
December 12, 2015 10:09
-
-
Save kraih/4756855 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
use Mojolicious::Lite; | |
use Mango; | |
use Mango::BSON ':bson'; | |
my $uri = 'mongodb://<user>:<pass>@<server>/<database>'; | |
helper mango => sub { state $mango = Mango->new($uri) }; | |
# Store and retrieve information non-blocking | |
get '/' => sub { | |
my $self = shift; | |
my $collection = $self->mango->db->collection('visitors'); | |
my $ip = $self->tx->remote_address; | |
# Store information about current visitor | |
$collection->insert({when => bson_time, from => $ip} => sub { | |
my ($collection, $err, $oid) = @_; | |
return $self->render_exception($err) if $err; | |
# Retrieve information about previous visitors | |
$collection->find->sort({when => -1})->fields({_id => 0})->all(sub { | |
my ($collection, $err, $docs) = @_; | |
return $self->render_exception($err) if $err; | |
# And show it to current visitor | |
$self->render(json => $docs); | |
}); | |
}); | |
}; | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment