-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (42 loc) · 1.49 KB
/
main.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
//In main.go we will basically have routes
//the functions that will help us to work with these routes will be in controllers
//our models will be very simple, it will just have struct of how the user model will look like
//main.go is the entry point to our project
package main
//there should be space after import
import (
"context"
"fmt"
"github.com/julienschmidt/httprouter"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"net/http"
"log-ingestor/controllers"
)
func main(){
r := httprouter.New()
//we need a new session
uc := controllers.NewLogDataController(getClient())
// r.GET("/LogData/:id", uc.GetLogData)
r.POST("/LogData", uc.CreateLogData )
// r.DELETE("/LogData/:id", uc.DeleteLogData)
http.ListenAndServe("localhost:9000", r)
}
//it will return a mongoDb session
func getClient() *mongo.Client {
// clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
// clientOptions := options.Client().ApplyURI("mongodb://writeUser:secret@localhost:27017")
clientOptions := options.Client().ApplyURI("mongodb://writeUser:secret@mongo1:27017,mongo2:27017,mongo3:27017/?replicaSet=myReplicaSet")
clientOptions.SetRetryWrites(true) // Enable retryable writes
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
panic(err)
}
// Ping the server to ensure that the client can successfully connect
err = client.Ping(context.TODO(), nil)
if err != nil {
panic(err)
}
fmt.Println("Connected to MongoDB")
return client
}