-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
51 lines (42 loc) · 1.25 KB
/
Copy pathmain.go
File metadata and controls
51 lines (42 loc) · 1.25 KB
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
package main
import (
"log"
"todo_api/internal/config"
"todo_api/internal/database"
"todo_api/internal/handlers"
"todo_api/internal/middleware"
"github.com/gin-gonic/gin"
)
func main() {
cfg, err := config.Load()
if err != nil {
log.Fatalf("load config: %v", err)
}
pool, err := database.Connect(cfg.DatabaseURL)
if err != nil {
log.Fatalf("connect database: %v", err)
}
defer pool.Close()
var router *gin.Engine = gin.Default()
router.SetTrustedProxies(nil)
router.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Todo API is Running!",
"status": "Success",
"database": "Connected",
})
})
router.POST("/auth/register", handlers.CreateUserHandler(pool))
router.POST("/auth/login", handlers.LoginUserHandler(pool, cfg))
protected := router.Group("/todos")
protected.Use(middleware.AuthMiddleware(cfg))
{
protected.POST("/", handlers.CreateTodoHandler(pool))
protected.GET("/", handlers.GetAllTodosHandler(pool))
protected.GET("/:id", handlers.GetTodoByIDHandler(pool))
protected.PUT("/:id", handlers.UpdateTodoHandler(pool))
protected.DELETE("/:id", handlers.DeleteTodoHandler(pool))
}
router.GET("/protected", middleware.AuthMiddleware(cfg), handlers.TestProtectedHandler())
router.Run(":" + cfg.Port)
}