2024-02-06 09:30:58 +08:00
|
|
|
package starter
|
2023-07-14 10:16:58 +08:00
|
|
|
|
|
|
|
import (
|
2023-10-20 15:08:47 +08:00
|
|
|
"fmt"
|
2024-02-06 09:30:58 +08:00
|
|
|
swaggerFiles "github.com/swaggo/files"
|
|
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
|
|
"joylink.club/bj-rtsts-server/api"
|
|
|
|
"joylink.club/bj-rtsts-server/docs"
|
|
|
|
"joylink.club/bj-rtsts-server/middleware"
|
|
|
|
"joylink.club/bj-rtsts-server/mqtt"
|
|
|
|
"joylink.club/bj-rtsts-server/third_party"
|
2023-10-12 10:10:23 +08:00
|
|
|
"log/slog"
|
|
|
|
"net"
|
2023-07-28 14:45:35 +08:00
|
|
|
"net/http"
|
2023-10-12 10:10:23 +08:00
|
|
|
"os"
|
|
|
|
"runtime/debug"
|
|
|
|
"strings"
|
2023-07-14 10:16:58 +08:00
|
|
|
|
|
|
|
"joylink.club/bj-rtsts-server/db/dbquery"
|
2023-09-04 11:12:57 +08:00
|
|
|
"joylink.club/bj-rtsts-server/dto"
|
2023-10-20 15:08:47 +08:00
|
|
|
"joylink.club/bj-rtsts-server/sys_error"
|
2023-07-14 10:16:58 +08:00
|
|
|
|
2023-07-31 10:28:20 +08:00
|
|
|
"github.com/gin-contrib/cors"
|
2023-07-14 10:16:58 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-10-12 10:10:23 +08:00
|
|
|
sloggin "github.com/samber/slog-gin"
|
2023-07-14 10:16:58 +08:00
|
|
|
"joylink.club/bj-rtsts-server/config"
|
|
|
|
"joylink.club/bj-rtsts-server/db"
|
|
|
|
"joylink.club/bj-rtsts-server/logger"
|
|
|
|
)
|
|
|
|
|
2024-02-06 09:30:58 +08:00
|
|
|
func Start() {
|
|
|
|
engine := initServer()
|
|
|
|
mqtt.Startup(mqtt.NewMqttOptions(config.Config.Messaging.Mqtt.Address, config.Config.Messaging.Mqtt.Username, config.Config.Messaging.Mqtt.Password))
|
|
|
|
third_party.Init()
|
|
|
|
authMiddleware := middleware.InitGinJwtMiddleware()
|
|
|
|
router := engine.Group("/api")
|
|
|
|
api.InitUserRouter(router, authMiddleware)
|
|
|
|
api.InitDraftingRouter(router, authMiddleware)
|
|
|
|
api.InitPublishedGiRouter(router, authMiddleware)
|
|
|
|
api.InitSimulationRouter(router, authMiddleware)
|
|
|
|
api.InitCategoryRouter(router, authMiddleware)
|
|
|
|
api.InitProjectRouter(router, authMiddleware)
|
|
|
|
api.InitTrainManageRouter(router, authMiddleware)
|
|
|
|
api.InitProjectLinkRouter(router, authMiddleware)
|
|
|
|
api.InitAuthRouter(router, authMiddleware)
|
|
|
|
api.InitProjectRunConfigRouter(router, authMiddleware)
|
|
|
|
docs.SwaggerInfo.Title = "CBTC测试系统API"
|
|
|
|
engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
|
|
|
|
|
|
|
serverConfig := config.Config.Server
|
|
|
|
if serverConfig.Port == 0 {
|
|
|
|
serverConfig.Port = 8080
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.Run(fmt.Sprintf(":%d", config.Config.Server.Port))
|
|
|
|
}
|
|
|
|
func initServer() *gin.Engine {
|
2023-07-14 10:16:58 +08:00
|
|
|
config.LoadConfig()
|
|
|
|
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
|
|
|
|
2023-07-31 10:51:44 +08:00
|
|
|
conf := cors.DefaultConfig()
|
|
|
|
conf.AllowHeaders = []string{"*"}
|
|
|
|
conf.AllowAllOrigins = true
|
2023-10-12 10:10:23 +08:00
|
|
|
engine.Use(sloggin.New(slog.Default()))
|
2023-07-31 10:51:44 +08:00
|
|
|
engine.Use(cors.New(conf))
|
2023-10-12 10:10:23 +08:00
|
|
|
// gin panic 异常处理,默认处理为
|
2024-02-06 09:30:58 +08:00
|
|
|
engine.Use(customRecoveryWithSlog(slog.Default(), true, func(c *gin.Context, e interface{}) {
|
2023-10-20 15:08:47 +08:00
|
|
|
be, ok := e.(*sys_error.BusinessError)
|
|
|
|
if !ok {
|
|
|
|
e, ok := e.(error)
|
|
|
|
if ok {
|
|
|
|
be = sys_error.New("未知错误", e)
|
|
|
|
} else {
|
|
|
|
be = sys_error.New("未知错误", fmt.Errorf("%v", e))
|
|
|
|
}
|
2023-07-28 14:45:35 +08:00
|
|
|
}
|
2024-01-31 16:56:48 +08:00
|
|
|
c.Error(be)
|
2024-10-09 18:02:42 +08:00
|
|
|
statusCode := http.StatusInternalServerError
|
|
|
|
if be.ErrorCode > 0 {
|
|
|
|
statusCode = http.StatusForbidden
|
|
|
|
}
|
|
|
|
c.JSON(statusCode, &dto.ErrorDto{
|
2023-10-20 15:08:47 +08:00
|
|
|
Tip: be.UserMsg,
|
|
|
|
Message: be.Error(),
|
|
|
|
})
|
2023-07-28 14:45:35 +08:00
|
|
|
c.Writer.WriteHeaderNow()
|
|
|
|
c.Abort()
|
|
|
|
}))
|
2023-07-14 10:16:58 +08:00
|
|
|
return engine
|
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
|
2024-02-06 09:30:58 +08:00
|
|
|
func customRecoveryWithSlog(logger *slog.Logger, stack bool, recovery gin.RecoveryFunc) gin.HandlerFunc {
|
2023-10-12 10:10:23 +08:00
|
|
|
return func(c *gin.Context) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
// Check for a broken connection, as it is not really a
|
|
|
|
// condition that warrants a panic stack trace.
|
|
|
|
var brokenPipe bool
|
|
|
|
if ne, ok := err.(*net.OpError); ok {
|
|
|
|
if se, ok := ne.Err.(*os.SyscallError); ok {
|
|
|
|
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
|
|
|
|
brokenPipe = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if brokenPipe {
|
|
|
|
logger.Error(c.Request.URL.Path,
|
2023-11-16 16:54:23 +08:00
|
|
|
"error", err,
|
2023-10-12 10:10:23 +08:00
|
|
|
)
|
|
|
|
// If the connection is dead, we can't write a status to it.
|
|
|
|
c.Error(err.(error)) // nolint: errcheck
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if stack {
|
2024-06-16 12:26:48 +08:00
|
|
|
logger.Error("请求处理Panic异常", "error", err, "stack", string(debug.Stack()))
|
2023-10-12 10:10:23 +08:00
|
|
|
} else {
|
2023-11-16 16:54:23 +08:00
|
|
|
logger.Error("请求处理Panic异常",
|
|
|
|
"error", err,
|
2023-10-12 10:10:23 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
recovery(c, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|