goCaptcha/mongoConfig.go
mongoConfig.goRaw
1package main
2
3import (
4 "encoding/json"
5 "fmt"
6 "io/ioutil"
7
8 mgo "gopkg.in/mgo.v2"
9)
10
11//MongoConfig stores the configuration of mongodb to connect
12type MongoConfig struct {
13 Ip string `json:"ip"`
14 Database string `json:"database"`
15}
16
17var mongoConfig MongoConfig
18
19//var captchaCollection *mgo.Collection
20var captchaSolCollection *mgo.Collection
21var imgFakePathCollection *mgo.Collection
22var suspiciousIPCollection *mgo.Collection
23
24func readMongodbConfig(path string) {
25 file, e := ioutil.ReadFile(path)
26 if e != nil {
27 fmt.Println("error:", e)
28 }
29 content := string(file)
30 json.Unmarshal([]byte(content), &mongoConfig)
31}
32
33func getSession() (*mgo.Session, error) {
34 session, err := mgo.Dial("mongodb://" + mongoConfig.Ip)
35 if err != nil {
36 panic(err)
37 }
38 //defer session.Close()
39
40 // Optional. Switch the session to a monotonic behavior.
41 session.SetMode(mgo.Monotonic, true)
42
43 // Optional. Switch the session to a monotonic behavior.
44 session.SetMode(mgo.Monotonic, true)
45
46 return session, err
47}
48func getCollection(session *mgo.Session, collection string) *mgo.Collection {
49
50 c := session.DB(mongoConfig.Database).C(collection)
51 return c
52}