-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
45 lines (36 loc) · 1.02 KB
/
init.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
package main
import (
"context"
"flag"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func init() {
setCLIParams()
// reading config variables
if err := envSetup(); err != nil {
log.Fatal(fmt.Errorf("cannot detect env: %w", err))
}
// connecting to mongodb
mongoClient, err := mongo.Connect(context.Background(), c.Mongo.MongoDBConnectionOptions())
if err != nil {
log.Fatal("Unable to connect to document store:", err)
}
// pinging mongo instance
pingCtx, cancelPingCtx := context.WithTimeout(context.Background(), 1*time.Second)
defer cancelPingCtx()
err = mongoClient.Ping(pingCtx, readpref.Primary())
if err != nil {
log.Fatal("Unable to ping document store:", err)
}
database := mongoClient.Database(c.Mongo.DatabaseName)
postCollection = database.Collection("posts")
requestedPosts = make(map[string]requestedPost)
}
func setCLIParams() {
flag.StringVar(&configFilePath, "config", "./config.toml", "configuration file path")
flag.Parse()
}