-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
59 lines (49 loc) · 1.52 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
50
51
52
53
54
55
56
57
58
59
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/GDSC-UIT/egreenbin-api/component"
"github.com/GDSC-UIT/egreenbin-api/handlers"
middleware "github.com/GDSC-UIT/egreenbin-api/middlewares"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// const uri = "mongodb://admin:123123123@localhost:27017/?maxPoolSize=20&w=majority"
// const uri = "mongodb+srv://admin:[email protected]/?retryWrites=true&w=majority"
func main() {
// Set up the MongoDB connection
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Some error occured. Err: %s", err)
}
uriDb := os.Getenv("CONNECTION_STRING")
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uriDb))
if err != nil {
panic(err)
}
defer func() {
if err = client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()
// Ping the primary
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
panic(err)
}
fmt.Println("Successfully connected and pinged.")
router := gin.Default()
db := client.Database("egreenbin")
appContext := component.NewAppContext(db)
router.Use(middleware.Recover(appContext))
apiR := router.Group("/api")
handlers.NewStudentHandler(apiR, appContext, db)
handlers.NewCommentHandler(apiR, appContext, db)
handlers.NewTeacherHandler(apiR, appContext, db)
handlers.NewGarbageHandler(apiR, appContext, db)
log.Fatal(router.Run(":3000"))
}