/
example.go
171 lines (153 loc) · 4.43 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// main package is an example of how to use the querybuilder to
// construct filters for MongoDB Find operations.
//
// To run this example, get a running instance of Docker on 27017
// `docker run -d --name example-mongo -p 27017:27017 mongo`
//
// To start the example server:
// `go run examples/example.go`
//
// To query the newly running example API:
// `curl http://localhost:3080/v1/things?filter[attributes]=round`
//
// For more queryoptions info, see: https://github.com/brozeph/queryoptions
//
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
querybuilder "github.com/brozeph/mongoquerybuilder"
"github.com/brozeph/queryoptions"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// schema for things collection (used by mongo query builder)
var thingsSchema = bson.M{
"$jsonSchema": bson.M{
"bsonType": "object",
"required": []string{"thingID"},
"properties": bson.M{
"thingID": bson.M{
"bsonType": "string",
"description": "primary identifier for the thing",
},
"created": bson.M{
"bsonType": "date",
"description": "time at which the thing was created",
},
"name": bson.M{
"bsonType": "string",
"description": "name of the thing",
},
"attributes": bson.M{
"bsonType": "array",
"description": "type tags for the thing",
},
},
},
}
// golang type for the things...
type thing struct {
ThingID string `bson:"thingID"`
Name string `bson:"name"`
Created time.Time `bson:"created"`
Attributes []string `bson:"attributes"`
}
// create a new MongoDB QueryBuilder (with strict validation set to true)
var builder = querybuilder.NewQueryBuilder("things", thingsSchema, true)
//pointer for the mongo collection to query from
var collection *mongo.Collection
func getThings(w http.ResponseWriter, r *http.Request) {
opt, err := queryoptions.FromQuerystring(r.URL.RawQuery)
if err != nil {
fmt.Fprint(w, err)
return
}
// build a bson.M filter for the Find based on queryoptions filters
filter, err := builder.Filter(opt)
if err != nil {
// NOTE: will only error when strictValidation is true
fmt.Fprint(w, err)
return
}
// build options (pagination, sorting, field projection) based on queryoptions
fo, err := builder.FindOptions(opt)
if err != nil {
// NOTE: will only error when strictValidation is true
fmt.Fprint(w, err)
return
}
// now use the filter and options in a Find call to the Mongo collection
cur, err := collection.Find(context.TODO(), filter, fo)
if err != nil {
fmt.Fprint(w, err)
return
}
defer cur.Close(context.TODO())
data := []thing{}
if err = cur.All(context.TODO(), &data); err != nil {
fmt.Fprint(w, err)
return
}
re, _ := json.Marshal(data)
fmt.Fprint(w, string(re))
}
func main() {
// create a MongoDB client
mc, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
log.Fatal(err)
}
// connect to MongoDB
if err := mc.Connect(context.TODO()); err != nil {
log.Fatal(err)
}
// create a collection with the schema
colOpts := options.CreateCollection().SetValidator(thingsSchema)
if err := mc.Database("things-db").CreateCollection(context.TODO(), "things", colOpts); err == nil {
// if err is nil, this is the first time the program is running... insert data
// I know... kinda whack, but this is just an example
data := []interface{}{
thing{
ThingID: "123456",
Name: "basketball",
Created: time.Now(),
Attributes: []string{"round", "orange", "bouncey"},
},
thing{
ThingID: "123455",
Name: "computer",
Created: time.Now(),
Attributes: []string{"square", "metal", "heavy"},
},
thing{
ThingID: "123454",
Name: "superball",
Created: time.Now(),
Attributes: []string{"round", "bouncey", "small"},
},
thing{
ThingID: "123453",
Name: "glass",
Created: time.Now(),
Attributes: []string{"glass", "container", "transparent"},
},
thing{
ThingID: "123452",
Name: "can",
Created: time.Now(),
Attributes: []string{"metal", "cylinder", "empty"},
},
}
mc.Database("things-db").Collection("things").InsertMany(context.TODO(), data)
}
// set the collection pointer
collection = mc.Database("things-db").Collection("things")
http.HandleFunc("/v1/things", getThings)
log.Fatal(http.ListenAndServe(":8080", nil))
}