| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package mongodocstore |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"net/url" |
| 22 |
"os" |
| 23 |
"strings" |
| 24 |
"sync" |
| 25 |
|
| 26 |
"go.mongodb.org/mongo-driver/mongo" |
| 27 |
"gocloud.dev/docstore" |
| 28 |
) |
| 29 |
|
| 30 |
func init() { |
| 31 |
docstore.DefaultURLMux().RegisterCollection(Scheme, new(defaultDialer)) |
| 32 |
} |
| 33 |
|
| 34 |
// defaultDialer dials a default Mongo server based on the environment variable |
| 35 |
// MONGO_SERVER_URL. |
| 36 |
type defaultDialer struct { |
| 37 |
mongoServerURL string |
| 38 |
mu sync.Mutex |
| 39 |
opener *URLOpener |
| 40 |
err error |
| 41 |
} |
| 42 |
|
| 43 |
func (o *defaultDialer) OpenCollectionURL(ctx context.Context, u *url.URL) (*docstore.Collection, error) { |
| 44 |
o.mu.Lock() |
| 45 |
defer o.mu.Unlock() |
| 46 |
currentEnv := os.Getenv("MONGO_SERVER_URL") |
| 47 |
|
| 48 |
if currentEnv == "" { |
| 49 |
o.err = errors.New("MONGO_SERVER_URL environment variable is not set") |
| 50 |
return nil, fmt.Errorf("open collection %s: %v", u, o.err) |
| 51 |
} |
| 52 |
|
| 53 |
// If MONGO_SERVER_URL has been updated, then update o.opener as well |
| 54 |
if currentEnv != o.mongoServerURL { |
| 55 |
client, err := Dial(ctx, currentEnv) |
| 56 |
if err != nil { |
| 57 |
o.err = fmt.Errorf("failed to dial default Mongo server at %q: %v", currentEnv, err) |
| 58 |
return nil, fmt.Errorf("open collection %s: %v", u, o.err) |
| 59 |
} |
| 60 |
o.mongoServerURL = currentEnv |
| 61 |
o.opener = &URLOpener{Client: client} |
| 62 |
} |
| 63 |
return o.opener.OpenCollectionURL(ctx, u) |
| 64 |
} |
| 65 |
|
| 66 |
// Scheme is the URL scheme mongodocstore registers its URLOpener under on |
| 67 |
// docstore.DefaultMux. |
| 68 |
const Scheme = "mongo" |
| 69 |
|
| 70 |
// URLOpener opens URLs like "mongo://mydb/mycollection". |
| 71 |
// See https://docs.mongodb.com/manual/reference/limits/#naming-restrictions for |
| 72 |
// naming restrictions. |
| 73 |
// |
| 74 |
// The URL Host is used as the database name. |
| 75 |
// The URL Path is used as the collection name. |
| 76 |
// |
| 77 |
// The following query parameters are supported: |
| 78 |
// |
| 79 |
// - id_field (optional): the field name to use for the "_id" field. |
| 80 |
type URLOpener struct { |
| 81 |
// A Client is a MongoDB client that performs operations on the db, must be |
| 82 |
// non-nil. |
| 83 |
Client *mongo.Client |
| 84 |
|
| 85 |
// Options specifies the options to pass to OpenCollection. |
| 86 |
Options Options |
| 87 |
} |
| 88 |
|
| 89 |
// OpenCollectionURL opens the Collection URL. |
| 90 |
func (o *URLOpener) OpenCollectionURL(ctx context.Context, u *url.URL) (*docstore.Collection, error) { |
| 91 |
q := u.Query() |
| 92 |
idField := q.Get("id_field") |
| 93 |
q.Del("id_field") |
| 94 |
o.Options.RevisionField = q.Get("revision_field") |
| 95 |
q.Del("revision_field") |
| 96 |
|
| 97 |
for param := range q { |
| 98 |
return nil, fmt.Errorf("open collection %s: invalid query parameter %q", u, param) |
| 99 |
} |
| 100 |
|
| 101 |
dbName := u.Host |
| 102 |
if dbName == "" { |
| 103 |
return nil, fmt.Errorf("open collection %s: URL must have a non-empty Host (database name)", u) |
| 104 |
} |
| 105 |
collName := strings.TrimPrefix(u.Path, "/") |
| 106 |
if collName == "" { |
| 107 |
return nil, fmt.Errorf("open collection %s: URL must have a non-empty Path (collection name)", u) |
| 108 |
} |
| 109 |
return OpenCollection(o.Client.Database(dbName).Collection(collName), idField, &o.Options) |
| 110 |
} |