-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
36 lines (31 loc) · 923 Bytes
/
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
package main
import (
"context"
"github.com/gin-gonic/gin"
"github.com/yongyuth-chuankhuntod/todolists-with-mongodb-in-go/todo"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"time"
)
func main() {
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
clientOptions := options.Client().
ApplyURI("mongodb+srv://mongdb:<password>@einer-cluster.aahjt.mongodb.net/myFirstDatabase?retryWrites=true&w=majority").
SetServerAPIOptions(serverAPIOptions)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
r := gin.Default()
handler := todo.NewTodo(client)
r.POST("/todo", handler.NewTask())
r.GET("/todo", handler.ReadTasks())
r.DELETE("/todo/:id", handler.DeleteTask())
err = r.Run(":3000")
if err != nil {
log.Fatal(err)
}
}