SmartMurphytt/chainmaker_go/main.go

42 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-11-29 21:48:12 +08:00
package main
import (
"chainmaker_go/database"
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
// 数据库初始化
db := database.InitDB()
sqlDB, _ := db.DB()
defer sqlDB.Close()
r := gin.Default()
// 跨域请求
//r.Use(cors.Default())
r.Use(Cors())
r = CollectRoute(r)
// 设置端口
port := ""
if port != "" {
panic(r.Run(":" + port))
}
panic(r.Run()) // listen and serve on 0.0.0.0:8080
}
func Cors() gin.HandlerFunc {
return func(context *gin.Context) {
method := context.Request.Method
context.Header("Access-Control-Allow-Origin", context.GetHeader("Origin"))
context.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token, X-Requested-With")
context.Header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS")
context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
context.Header("Access-Control-Allow-Credentials", "true")
if method == "OPTIONS" {
context.AbortWithStatus(http.StatusNoContent)
}
context.Next()
}
}