优化结构
This commit is contained in:
+15
-14
@@ -26,7 +26,7 @@ func NewGinEngine(runtime *conf.Runtime, access *service.AccessControlService, h
|
||||
return NewGinEngineWithRuntime(runtime, access, auth, security, audit, logger, version, platformmodule.NewRuntime(router.NewRoutes(handlers)), nil)
|
||||
}
|
||||
|
||||
func NewGinEngineWithRuntime(runtime *conf.Runtime, access *service.AccessControlService, auth *service.AuthService, security *service.SecurityService, audit *service.AuditRecorder, logger *slog.Logger, version string, routes *platformmodule.Runtime, ws *websocket.Server) *gin.Engine {
|
||||
func NewGinEngineWithRuntime(runtime *conf.Runtime, access *service.AccessControlService, auth middleware.TokenAuthenticator, security *service.SecurityService, audit *service.AuditRecorder, logger *slog.Logger, version string, routes *platformmodule.Runtime, ws *websocket.Server) *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
engine := gin.New()
|
||||
if err := engine.SetTrustedProxies(nil); err != nil && logger != nil {
|
||||
@@ -51,17 +51,20 @@ func NewGinEngineWithRuntime(runtime *conf.Runtime, access *service.AccessContro
|
||||
if routes != nil {
|
||||
routes.RegisterRoutes(public, private, engine)
|
||||
}
|
||||
handleWebSocket := func(c *gin.Context) {
|
||||
if ws == nil || !ws.Enabled() || c.Request.URL.Path != ws.Path() {
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if !middleware.AuthenticateWebSocket(c, auth) {
|
||||
return
|
||||
}
|
||||
if err := ws.HandleRequest(c.Writer, c.Request); err != nil && logger != nil {
|
||||
logger.Warn("websocket request failed", "mod", "websocket", "error", err)
|
||||
}
|
||||
}
|
||||
if ws != nil && ws.Enabled() {
|
||||
path := ws.Path()
|
||||
engine.GET(path, func(c *gin.Context) {
|
||||
if !ws.Enabled() || c.Request.URL.Path != ws.Path() {
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err := ws.HandleRequest(c.Writer, c.Request); err != nil && logger != nil {
|
||||
logger.Warn("websocket request failed", "mod", "websocket", "error", err)
|
||||
}
|
||||
})
|
||||
engine.GET(ws.Path(), handleWebSocket)
|
||||
}
|
||||
registerSwagger(engine, prefix, version, logger)
|
||||
registerLocalStorage(engine, runtime)
|
||||
@@ -74,9 +77,7 @@ func NewGinEngineWithRuntime(runtime *conf.Runtime, access *service.AccessContro
|
||||
|
||||
engine.NoRoute(func(c *gin.Context) {
|
||||
if ws != nil && ws.Enabled() && c.Request.Method == http.MethodGet && c.Request.URL.Path == ws.Path() {
|
||||
if err := ws.HandleRequest(c.Writer, c.Request); err != nil && logger != nil {
|
||||
logger.Warn("websocket request failed", "mod", "websocket", "error", err)
|
||||
}
|
||||
handleWebSocket(c)
|
||||
return
|
||||
}
|
||||
if serveLocalStorage(c, runtime) {
|
||||
|
||||
Reference in New Issue
Block a user