-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
38 lines (31 loc) · 953 Bytes
/
main.go
File metadata and controls
38 lines (31 loc) · 953 Bytes
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
package main
import (
"github.com/gin-gonic/gin"
"github.com/chanmineom/todo-api-with-gin/controllers"
"github.com/chanmineom/todo-api-with-gin/middleware"
"github.com/chanmineom/todo-api-with-gin/models"
"github.com/chanmineom/todo-api-with-gin/utils"
)
func main() {
r := gin.Default()
r.Use(utils.LoggerMiddleware())
if err := utils.InitDB(); err != nil {
panic("failed to connect database: " + err.Error())
}
utils.DB.AutoMigrate(&models.User{}, &models.Todo{})
public := r.Group("/api")
{
public.POST("/register", controllers.UserRegister)
public.POST("/login", controllers.UserLogin)
}
protected := r.Group("/api/todos")
protected.Use(middleware.AuthMiddleware())
{
protected.POST("", controllers.CreatedTodo)
protected.GET("", controllers.GetTodos)
protected.GET("/:id", controllers.GetTodo)
protected.PUT("/:id", controllers.UpdateTodo)
protected.DELETE("/:id", controllers.DeleteTodo)
}
r.Run(":8080")
}