2023-07-14 10:16:58 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-07-28 14:45:35 +08:00
|
|
|
"net/http"
|
2023-07-17 11:12:23 +08:00
|
|
|
"time"
|
2023-07-14 10:16:58 +08:00
|
|
|
|
2023-07-17 11:12:23 +08:00
|
|
|
"go.uber.org/zap"
|
2023-07-14 10:16:58 +08:00
|
|
|
"joylink.club/bj-rtsts-server/db/dbquery"
|
|
|
|
|
2023-07-31 10:28:20 +08:00
|
|
|
"github.com/gin-contrib/cors"
|
2023-07-17 11:12:23 +08:00
|
|
|
ginzap "github.com/gin-contrib/zap"
|
2023-07-14 10:16:58 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"joylink.club/bj-rtsts-server/config"
|
|
|
|
"joylink.club/bj-rtsts-server/db"
|
|
|
|
"joylink.club/bj-rtsts-server/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
func InitServer() *gin.Engine {
|
|
|
|
config.LoadConfig()
|
|
|
|
fmt.Println("数据源配置为:", config.Config.Datasource.Dsn)
|
|
|
|
err := logger.InitLogger()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.InitDb()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
dbquery.SetDefault(db.DB)
|
|
|
|
|
|
|
|
engine := gin.New()
|
2023-07-31 10:28:20 +08:00
|
|
|
|
|
|
|
engine.Use(cors.Default())
|
|
|
|
|
2023-07-17 11:12:23 +08:00
|
|
|
engine.Use(ginzap.Ginzap(zap.L(), time.DateTime, false))
|
2023-07-28 14:45:35 +08:00
|
|
|
// gin panic 异常处理,默认处理为 engine.Use(ginzap.RecoveryWithZap(zap.L(), true))
|
|
|
|
engine.Use(ginzap.CustomRecoveryWithZap(zap.L(), true, func(c *gin.Context, e interface{}) {
|
|
|
|
switch e.(type) {
|
|
|
|
case error:
|
|
|
|
err := e.(error)
|
|
|
|
c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
default:
|
|
|
|
c.JSON(http.StatusInternalServerError, e)
|
|
|
|
}
|
|
|
|
c.Writer.WriteHeaderNow()
|
|
|
|
c.Abort()
|
|
|
|
}))
|
2023-07-14 10:16:58 +08:00
|
|
|
return engine
|
|
|
|
}
|