Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

update mongo connection handling #1

Open
wants to merge 1 commit into from

2 participants

Anton Gerasimov Adam Taylor
Anton Gerasimov

MongoDB::Connection was replaced by MongoDB::MongoClient plus added support for multiple (named) connections in dancer app.

Adam Taylor
Owner

Thanks very much, it looks good from a brief look. I assume this has also added support for Dancer2? I'll try and have a proper look at this later and get it merged.

Anton Gerasimov

Dancer2 uses its own namespace Dancer2::* so this patch does not add support of D2. Only compatibility with earlier versions (e.g. 1.9999). See PerlDancer/Dancer2@a0440ff

Adam Taylor
Owner

Okay cool, no worries. I thought I'd seen a bit that look like it had done something to support Dancer2 as well. No problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 27, 2013
  1. Anton Gerasimov

    update mongo connection handling

    Wu-Wu authored
This page is out of date. Refresh to see the latest.
96 lib/Dancer/Plugin/Mongo.pm
View
@@ -4,36 +4,75 @@ package Dancer::Plugin::Mongo;
use strict;
use warnings;
use Dancer::Plugin;
-use MongoDB 0.38;
+use MongoDB 0.502;
+use Carp ();
our $VERSION = 0.03;
-my $settings = plugin_setting;
-my $conn;
+my $settings;
+my $handles;
+
+my $croak = sub { Carp::croak $_[0] };
## return a connected MongoDB object
register mongo => sub {
+ my (undef, $profile) = plugin_args(@_);
+
+ $profile ||= '_default';
+
+ return $handles->{$profile} if exists $handles->{$profile};
+
+ $settings ||= plugin_setting;
- $conn ? $conn : $conn = MongoDB::Connection->new( _slurp_settings() ) ;
+ my $options = $profile eq '_default' ? $settings : $settings->{connections}->{$profile};
+ $croak->("Options for '$profile' not found") unless ref $options eq 'HASH';
- return $conn;
+ my $_handle;
+ eval { $_handle = MongoDB::MongoClient->new(_slurp_settings($options)) };
+ $croak->("Cannot get mongo handle: $@") if $@;
+
+ $handles->{$profile} = $_handle;
+
+ return $_handle;
};
-register_plugin;
+register_plugin for_versions => [1, 2];
sub _slurp_settings {
-
+ my ($settings) = @_;
+
my $args = {};
- for (qw/ host port username password w wtimeout auto_reconnect auto_connect
- timeout db_name query_timeout find_master/) {
- if (exists $settings->{$_}) {
- $args->{$_} = $settings->{$_};
- }
+ my @allowed = qw(
+ host
+ w
+ wtimeout
+ j
+ auto_reconnect
+ auto_connect
+ timeout
+ username
+ password
+ db_name
+ query_timeout
+ max_bson_size
+ find_master
+ ssl
+ dt_type
+ );
+
+ for (@allowed) {
+ $args->{$_} = $settings->{$_} if exists $settings->{$_};
}
return $args;
}
+1;
+
+__END__
+
+=pod
+
=head1 SYNOPSIS
use Dancer;
@@ -43,6 +82,10 @@ sub _slurp_settings {
my $widget = mongo->database->collection->find_one({ id => params->{id} });
}
+ get '/hosts/get' => sub {
+ to_json(mongo('live')->hosts->profiles->find->all);
+ }
+
=head1 DESCRIPTION
Dancer::Plugin::Mongo provides a wrapper around L<MongoDB>. Add the appropriate
@@ -52,29 +95,26 @@ using the 'mongo' keyword.
To query the database, use the standard MongoDB syntax, described in
L<MongoDB::Collection>.
-=head1 CONFIGURATON
+=head1 CONFIGURATION
Connection details will be taken from your Dancer application config file, and
should be specified as follows:
plugins:
Mongo:
- host:
- port:
- username:
- password:
- w:
- wtimeout:
- auto_reconnect:
- auto_connect:
- timeout:
- db_name:
- query_timeout:
- find_master:
+ host: 'mongodb://mongo.example.com:27017'
+ db_name: 'test'
+ connections:
+ devel:
+ host: 'mongodb://devel.example.com:27017'
+ db_name: 'foo_devel'
+ live:
+ host: 'mongodb://live.example.com:27017'
+ db_name: 'foo_live'
+ username: 'appuser'
+ password: 'apppassword'
All these configuration values are optional, full details are in the
-L<MongoDB::Connection> documentation.
+L<MongoDB::MongoClient> documentation.
=cut
-
-1;
12 t/00-load.t
View
@@ -1,10 +1,12 @@
-#!perl
-
+use strict;
+use warnings;
use Test::More;
-BEGIN { use_ok('MongoDB' ); }
-BEGIN { use_ok( 'Dancer::Plugin::Mongo' ); }
+BEGIN {
+ use_ok 'MongoDB';
+ use_ok 'Dancer::Plugin::Mongo';
+}
-diag( "Testing Dancer::Plugin::Mongo and MongoDB" );
+diag 'Testing Dancer::Plugin::Mongo and MongoDB';
done_testing;
36 t/01-settings.t
View
@@ -0,0 +1,36 @@
+use strict;
+use warnings;
+use Test::More import => ['!pass'];
+use Dancer ':syntax';
+use Dancer::Test;
+
+my $test_package = 't::lib::AppMongo';
+
+set show_errors => 1;
+set plugins => {
+ Mongo => {
+ host => 'bogus:27017',
+ connections => {
+ foo => { host => 'bogus:28017' },
+ }
+ }
+};
+
+load_app $test_package;
+
+my @samples = (
+ '/' => 200, $test_package,
+ '/mongo' => 200, 42,
+ '/mongo/get/foo' => 500, 'Cannot.*handle.*bogus:28017',
+ '/mongo/get/bar' => 500, 'Options.*not found',
+ '/mongo/get' => 500, 'Cannot.*handle.*bogus:27017',
+);
+
+while (my($route, $code, $content) = splice(@samples, 0, 3)) {
+ my $resp = dancer_response GET => $route;
+ ok $resp, "response exists for GET $route";
+ is $resp->status, $code, "status code for GET $route is $code";
+ like $resp->content, qr/$content/, "content looks good for GET $route";
+}
+
+done_testing();
20 t/lib/AppMongo.pm
View
@@ -0,0 +1,20 @@
+package
+ t::lib::AppMongo;
+
+use strict;
+use Dancer ':syntax';
+use Dancer::Plugin::Mongo;
+
+get '/' => sub { __PACKAGE__ };
+
+prefix '/mongo';
+
+get '/' => sub {
+ 42;
+};
+
+get '/get/?:profile?/?' => sub {
+ mongo(params->{profile})
+};
+
+1;
Something went wrong with that request. Please try again.