Skip to content

Commit c6fa5a5

Browse files
masx200claude
andcommitted
重构WebSocket服务器路由架构,优化会话处理逻辑
- 重构pipe-std-ws-server.go中的路由处理逻辑 - 创建generaterouteshttpsessions.go模块化路由生成器 - 将COPY和MOVE路由配置提取到独立函数 - 优化中间件组织结构,提高代码可维护性 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0beee08 commit c6fa5a5

2 files changed

Lines changed: 34 additions & 21 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package go_ws_sh
2+
3+
import (
4+
"context"
5+
"github.com/cloudwego/hertz/pkg/app"
6+
"gorm.io/gorm"
7+
)
8+
9+
func GenerateRoutesHttpsessions(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB, initial_sessions []Session) []RouteConfig {
10+
routes_copy_and_move := []RouteConfig{
11+
12+
{
13+
Path: "/sessions",
14+
Method: "COPY",
15+
MiddleWare: HertzCompose(AuthorizationMiddleware(credentialdb, tokendb, sessiondb), func(c context.Context, r *app.RequestContext, next HertzNext) {
16+
CopyMiddleware(credentialdb, tokendb, sessiondb, c, r, next)
17+
18+
}),
19+
},
20+
{
21+
Path: "/sessions",
22+
Method: "MOVE",
23+
MiddleWare: HertzCompose(AuthorizationMiddleware(credentialdb, tokendb, sessiondb), func(c context.Context, r *app.RequestContext, next HertzNext) {
24+
MoveMiddleware(initial_sessions, credentialdb, tokendb, sessiondb, c, r, next)
25+
26+
}),
27+
},
28+
}
29+
return routes_copy_and_move
30+
}

go_ws_sh/pipe-std-ws-server.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func pipe_std_ws_server(config ConfigServer, credentialdb *gorm.DB, tokendb *gor
4444
// authHandler := AuthorizationHandler(credentialdb, tokendb)
4545
var routes []RouteConfig //{
4646

47-
handlerGet := createhandlerauthorization(credentialdb, tokendb, func(w context.Context, r *app.RequestContext) {
47+
handlerGetWebsocket := createhandlerauthorization(credentialdb, tokendb, func(w context.Context, r *app.RequestContext) {
4848

4949
sessions, err := ReadAllSessions(sessiondb)
5050
if err != nil {
@@ -108,7 +108,7 @@ func pipe_std_ws_server(config ConfigServer, credentialdb *gorm.DB, tokendb *gor
108108
Connection := strings.ToLower(r.Request.Header.Get("Connection"))
109109

110110
if string(r.Method()) == consts.MethodGet && strings.Contains(Connection, "upgrade") && Upgrade == "websocket" {
111-
handlerGet(w, r)
111+
handlerGetWebsocket(w, r)
112112
return
113113
}
114114

@@ -132,25 +132,8 @@ func pipe_std_ws_server(config ConfigServer, credentialdb *gorm.DB, tokendb *gor
132132
middlewares := []app.HandlerFunc{
133133

134134
func(c context.Context, ctx *app.RequestContext) {
135-
routesmiddle := MatchAndRouteMiddleware([]RouteConfig{
136-
137-
{
138-
Path: "/sessions",
139-
Method: "COPY",
140-
MiddleWare: HertzCompose(AuthorizationMiddleware(credentialdb, tokendb, sessiondb), func(c context.Context, r *app.RequestContext, next HertzNext) {
141-
CopyMiddleware(credentialdb, tokendb, sessiondb, c, r, next)
142-
143-
}),
144-
},
145-
{
146-
Path: "/sessions",
147-
Method: "MOVE",
148-
MiddleWare: HertzCompose(AuthorizationMiddleware(credentialdb, tokendb, sessiondb), func(c context.Context, r *app.RequestContext, next HertzNext) {
149-
MoveMiddleware(initial_sessions, credentialdb, tokendb, sessiondb, c, r, next)
150-
151-
}),
152-
},
153-
})
135+
routes_copy_and_move := GenerateRoutesHttpsessions(credentialdb, tokendb, sessiondb, initial_sessions)
136+
routesmiddle := MatchAndRouteMiddleware(routes_copy_and_move)
154137
routesmiddle(c, ctx, func(c context.Context, r *app.RequestContext) {
155138
r.Next(c)
156139
})

0 commit comments

Comments
 (0)