-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
61 lines (49 loc) · 1.53 KB
/
server.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
package main
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
// "github.com/nipat01/chaorai-go-gin-gorm-mysql-api/controllers"
"github.com/nipat01/chaorai-go-gin-gorm-mysql-api/database"
"github.com/nipat01/chaorai-go-gin-gorm-mysql-api/routes"
)
func main() {
server := gin.Default()
server.Use(CORSMiddleware())
var envs map[string]string
envs, err := godotenv.Read(".env")
if err != nil {
log.Println("Error to load .env file")
}
log.Println("envs: ", envs)
dbConnect := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", envs["DB_USER"], envs["DB_PASS"], envs["DB_URL"], envs["DB_PORT"], envs["DB_NAME"])
log.Println("dbConnect: ", dbConnect)
database.Connect(dbConnect)
database.Migrate()
server.GET("/", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"data": "hello world",
})
})
routes.CustomerRoute(server)
routes.FarmerRoute(server)
routes.OrderRoute(server)
routes.ProductRoute(server)
server.Run(":" + envs["PORT"])
}
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Header("Access-Control-Allow-Methods", "POST,HEAD,PATCH, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}