Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

update mongo connection handling #1

Open
wants to merge 1 commit into from

2 participants

Anton Gerasimov Adam Taylor
Anton Gerasimov
Wu-Wu commented March 27, 2013

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
Wu-Wu commented March 27, 2013

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

Showing 1 unique commit by 1 author.

Mar 27, 2013
Anton Gerasimov update mongo connection handling 3763b0b
This page is out of date. Refresh to see the latest.
96  lib/Dancer/Plugin/Mongo.pm
@@ -4,36 +4,75 @@ package Dancer::Plugin::Mongo;
4 4
 use strict;
5 5
 use warnings;
6 6
 use Dancer::Plugin;
7  
-use MongoDB 0.38;
  7
+use MongoDB 0.502;
  8
+use Carp ();
8 9
 
9 10
 our $VERSION = 0.03;
10 11
 
11  
-my $settings = plugin_setting;
12  
-my $conn;
  12
+my $settings;
  13
+my $handles;
  14
+
  15
+my $croak = sub { Carp::croak $_[0] };
13 16
 
14 17
 ## return a connected MongoDB object
15 18
 register mongo => sub {
  19
+    my (undef, $profile) = plugin_args(@_);
  20
+
  21
+    $profile ||= '_default';
  22
+
  23
+    return $handles->{$profile} if exists $handles->{$profile};
  24
+
  25
+    $settings ||= plugin_setting;
16 26
 
17  
-    $conn ? $conn : $conn = MongoDB::Connection->new( _slurp_settings() ) ;
  27
+    my $options = $profile eq '_default' ? $settings : $settings->{connections}->{$profile};
  28
+    $croak->("Options for '$profile' not found") unless ref $options eq 'HASH';
18 29
 
19  
-    return $conn;
  30
+    my $_handle;
  31
+    eval { $_handle = MongoDB::MongoClient->new(_slurp_settings($options)) };
  32
+    $croak->("Cannot get mongo handle: $@") if $@;
  33
+
  34
+    $handles->{$profile} = $_handle;
  35
+
  36
+    return $_handle;
20 37
 };
21 38
 
22  
-register_plugin;
  39
+register_plugin for_versions => [1, 2];
23 40
 
24 41
 sub _slurp_settings {
25  
-    
  42
+    my ($settings) = @_;
  43
+
26 44
     my $args = {};
27  
-    for (qw/ host port username password w wtimeout auto_reconnect auto_connect
28  
-        timeout db_name query_timeout find_master/) {
29  
-        if (exists $settings->{$_}) {
30  
-            $args->{$_} = $settings->{$_};
31  
-        }
  45
+    my @allowed = qw(
  46
+        host
  47
+        w
  48
+        wtimeout
  49
+        j
  50
+        auto_reconnect
  51
+        auto_connect
  52
+        timeout
  53
+        username
  54
+        password
  55
+        db_name
  56
+        query_timeout
  57
+        max_bson_size
  58
+        find_master
  59
+        ssl
  60
+        dt_type
  61
+    );
  62
+
  63
+    for (@allowed) {
  64
+        $args->{$_} = $settings->{$_} if exists $settings->{$_};
32 65
     }
33 66
 
34 67
     return $args;
35 68
 }
36 69
 
  70
+1;
  71
+
  72
+__END__
  73
+
  74
+=pod
  75
+
37 76
 =head1 SYNOPSIS
38 77
 
39 78
     use Dancer;
@@ -43,6 +82,10 @@ sub _slurp_settings {
43 82
         my $widget = mongo->database->collection->find_one({ id => params->{id} });
44 83
     }
45 84
 
  85
+    get '/hosts/get' => sub {
  86
+        to_json(mongo('live')->hosts->profiles->find->all);
  87
+    }
  88
+
46 89
 =head1 DESCRIPTION
47 90
 
48 91
 Dancer::Plugin::Mongo provides a wrapper around L<MongoDB>. Add the appropriate
@@ -52,29 +95,26 @@ using the 'mongo' keyword.
52 95
 To query the database, use the standard MongoDB syntax, described in
53 96
 L<MongoDB::Collection>.
54 97
 
55  
-=head1 CONFIGURATON
  98
+=head1 CONFIGURATION
56 99
 
57 100
 Connection details will be taken from your Dancer application config file, and
58 101
 should be specified as follows:
59 102
 
60 103
     plugins:
61 104
         Mongo:
62  
-            host:
63  
-            port:
64  
-            username:
65  
-            password:
66  
-            w:
67  
-            wtimeout:
68  
-            auto_reconnect:
69  
-            auto_connect:
70  
-            timeout:
71  
-            db_name:
72  
-            query_timeout:
73  
-            find_master:
  105
+            host: 'mongodb://mongo.example.com:27017'
  106
+            db_name: 'test'
  107
+            connections:
  108
+                devel:
  109
+                    host: 'mongodb://devel.example.com:27017'
  110
+                    db_name: 'foo_devel'
  111
+                live:
  112
+                    host: 'mongodb://live.example.com:27017'
  113
+                    db_name: 'foo_live'
  114
+                    username: 'appuser'
  115
+                    password: 'apppassword'
74 116
 
75 117
 All these configuration values are optional, full details are in the
76  
-L<MongoDB::Connection> documentation.
  118
+L<MongoDB::MongoClient> documentation.
77 119
 
78 120
 =cut
79  
-
80  
-1;
12  t/00-load.t
... ...
@@ -1,10 +1,12 @@
1  
-#!perl 
2  
-
  1
+use strict;
  2
+use warnings;
3 3
 use Test::More;
4 4
 
5  
-BEGIN { use_ok('MongoDB' ); }
6  
-BEGIN { use_ok( 'Dancer::Plugin::Mongo' ); }
  5
+BEGIN {
  6
+    use_ok 'MongoDB';
  7
+    use_ok 'Dancer::Plugin::Mongo';
  8
+}
7 9
 
8  
-diag( "Testing Dancer::Plugin::Mongo and MongoDB" );
  10
+diag 'Testing Dancer::Plugin::Mongo and MongoDB';
9 11
 
10 12
 done_testing;
36  t/01-settings.t
... ...
@@ -0,0 +1,36 @@
  1
+use strict;
  2
+use warnings;
  3
+use Test::More import => ['!pass'];
  4
+use Dancer ':syntax';
  5
+use Dancer::Test;
  6
+
  7
+my $test_package = 't::lib::AppMongo';
  8
+
  9
+set show_errors => 1;
  10
+set plugins     => {
  11
+    Mongo => {
  12
+        host        => 'bogus:27017',
  13
+        connections => {
  14
+            foo => { host => 'bogus:28017' },
  15
+        }
  16
+    }
  17
+};
  18
+
  19
+load_app $test_package;
  20
+
  21
+my @samples = (
  22
+    '/'                 => 200, $test_package,
  23
+    '/mongo'            => 200, 42,
  24
+    '/mongo/get/foo'    => 500, 'Cannot.*handle.*bogus:28017',
  25
+    '/mongo/get/bar'    => 500, 'Options.*not found',
  26
+    '/mongo/get'        => 500, 'Cannot.*handle.*bogus:27017',
  27
+);
  28
+
  29
+while (my($route, $code, $content) = splice(@samples, 0, 3)) {
  30
+    my $resp = dancer_response GET => $route;
  31
+    ok   $resp,                         "response exists for GET $route";
  32
+    is   $resp->status,   $code,        "status code for GET $route is $code";
  33
+    like $resp->content,  qr/$content/, "content looks good for GET $route";
  34
+}
  35
+
  36
+done_testing();
20  t/lib/AppMongo.pm
... ...
@@ -0,0 +1,20 @@
  1
+package
  2
+    t::lib::AppMongo;
  3
+
  4
+use strict;
  5
+use Dancer ':syntax';
  6
+use Dancer::Plugin::Mongo;
  7
+
  8
+get '/' => sub {  __PACKAGE__ };
  9
+
  10
+prefix '/mongo';
  11
+
  12
+get '/' => sub  {
  13
+    42;
  14
+};
  15
+
  16
+get '/get/?:profile?/?' => sub  {
  17
+    mongo(params->{profile})
  18
+};
  19
+
  20
+1;
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.