forked from BIT_SCST_STIA/SmartMurphytt
42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
|
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()
|
||
|
}
|
||
|
}
|