-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
74 lines (59 loc) · 1.47 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/itsjamie/gin-cors"
"net/http"
"os"
"runtime"
"time"
"todo_center/go_todo_api/app"
"todo_center/go_todo_api/controllers"
"todo_center/go_todo_api/middlewares"
)
func main() {
ConfigRuntime()
StartGin()
}
func ConfigRuntime() {
nuCPU := runtime.NumCPU()
runtime.GOMAXPROCS(nuCPU)
fmt.Printf("Running with %d CPUs\n", nuCPU)
}
func StartGin() {
app.InitApp()
defer app.CloseApp()
router := gin.Default()
router.Use(middlewares.ErrorHandler())
router.Use(middlewares.ConnectDB(app.DBSession))
router.Use(cors.Middleware(cors.Config{
Origins: "*",
Methods: "GET, PUT, POST, DELETE",
RequestHeaders: "Origin, Authorization, Content-Type",
ExposedHeaders: "",
MaxAge: 50 * time.Second,
Credentials: true,
ValidateHeaders: false,
}))
router.LoadHTMLGlob("templates/*")
router.GET("/", func(c *gin.Context) {
c.String(200, "Hello Go TODO API")
})
v1 := router.Group("/v1")
{
v1.GET("/todos", controllers.IndexTodos)
v1.GET("/todos/:id", controllers.ViewTodo)
v1.POST("/todos", controllers.CreateTodo)
v1.PUT("/todos/:id", controllers.UpdateTodo)
v1.DELETE("/todos/:id", controllers.DeleteTodo)
v1.POST("/todos/:id/move", controllers.MoveTodo)
v1.GET("/swagger.yml", func(c *gin.Context) {
c.HTML(http.StatusOK, "swagger.yml", gin.H{})
})
}
port := os.Getenv("PORT")
if len(port) == 0 {
port = "3000"
}
router.Run(":" + port)
}