-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
44 lines (32 loc) · 1008 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
37
38
39
40
41
42
43
44
package main
import (
"context"
"log"
"time"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"goapi/controllers"
)
const itemIDPath = "/items/:id"
func main() {
gin.SetMode(gin.ReleaseMode)
// Connect to MongoDB
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
log.Fatal("Error creating MongoDB client:", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err = client.Connect(ctx); err != nil {
log.Fatal("Error connecting to MongoDB:", err)
}
collection := client.Database("GOtestdb").Collection("items")
r := gin.Default()
r.GET("/items", controllers.GetItems(collection))
r.POST("/items", controllers.CreateItem(collection))
r.GET(itemIDPath, controllers.GetItem(collection))
r.PUT(itemIDPath, controllers.UpdateItem(collection))
r.DELETE(itemIDPath, controllers.DeleteItem(collection))
r.Run(":8080")
}