-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (39 loc) · 1.08 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
// inspired from https://gist.github.com/thedevsaddam/a863148ff9de4cd3fcc628191005ab2e#file-rest_api-go
package main
import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"main/config"
"main/controllers"
"main/models"
)
func main() {
// replace by init() inside config package
config.LoadConfig()
// set default middleware for logging
router := gin.Default()
if config.DebugMode {
gin.SetMode(gin.DebugMode)
// allow all origins and methods
router.Use(cors.Default())
// todo graceful shutdown
} else {
gin.SetMode(gin.ReleaseMode)
}
// replace by init() inside models package
// connect to db and does the migrations
models.ConnectDataBase()
// default routes of APIs
v1 := router.Group("/api/v1/todos")
{
v1.POST("/", controllers.CreateTodo)
v1.GET("/", controllers.FetchAllTodo)
v1.GET("/:id", controllers.FetchSingleTodo)
v1.PATCH("/:id", controllers.UpdateTodo)
v1.DELETE("/:id", controllers.DeleteTodo)
}
// kick start the server and handle the errors, like port under 1024
if err := router.Run(":3000"); err != nil {
panic(err)
}
}