Skip to content

Commit 55d75bf

Browse files
masx200claude
andcommitted
优化WebSocket服务器路由和授权处理逻辑
- 优化AuthorizationMiddleware中的Bearer token处理 - 重构createTaskServer函数,简化handler参数处理 - 重命名createhandlerauthorization为createhandlerauthorization_websocket - 优化WebSocket路由处理,使用strings.TrimPrefix替代Param获取路径 - 改进路由中间件组合逻辑 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c6fa5a5 commit 55d75bf

4 files changed

Lines changed: 21 additions & 24 deletions

File tree

go_ws_sh/authorizationmiddleware.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ import (
1212
func AuthorizationMiddleware(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB) HertzMiddleWare {
1313
return func(c context.Context, r *app.RequestContext, next HertzNext) {
1414

15+
bearertoken := r.Request.Header.Get("authorization")
1516

17+
if bearertoken != "" {
1618

17-
bearertoken:=r.Request.Header.Get("authorization")
18-
19-
20-
21-
if bearertoken!=""{
22-
2319
}
2420
var req struct {
2521
Authorization CredentialsClient `json:"authorization"`

go_ws_sh/createTaskServer.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package go_ws_sh
22

33
import (
4-
"context"
4+
// "context"
55
"crypto/tls"
66
"log"
77
"time"
@@ -16,6 +16,7 @@ import (
1616
"github.com/hertz-contrib/http2/config"
1717
factoryh2 "github.com/hertz-contrib/http2/factory"
1818
"github.com/hertz-contrib/logger/accesslog"
19+
1920
quic "github.com/masx200/go_ws_sh/network/quic-go"
2021
http3 "github.com/masx200/go_ws_sh/server/quic-go"
2122
factoryh3 "github.com/masx200/go_ws_sh/server/quic-go/factory"
@@ -72,7 +73,7 @@ func InitHertzApp(hertzapp *server.Hertz) {
7273
// ctx.Next(c)
7374
// })
7475
}
75-
func createTaskServer(serverconfig ServerConfig, handler func(w context.Context, r *app.RequestContext), middlewares ...app.HandlerFunc) func() (interface{}, error) {
76+
func createTaskServer(serverconfig ServerConfig /* handler func(w context.Context, r *app.RequestContext), */, middlewares ...app.HandlerFunc) func() (any, error) {
7677
if serverconfig.Alpn == "h2" {
7778

7879
return func() (interface{}, error) {
@@ -120,9 +121,9 @@ func createTaskServer(serverconfig ServerConfig, handler func(w context.Context,
120121
for _, middleware := range middlewares {
121122
hertzapp.Use(middleware)
122123
}
123-
hertzapp.Any("/*name", func(c context.Context, ctx *app.RequestContext) {
124-
handler(c, ctx)
125-
})
124+
// hertzapp.Any("/*name", func(c context.Context, ctx *app.RequestContext) {
125+
// handler(c, ctx)
126+
// })
126127
x := hertzapp.Run()
127128
if x != nil {
128129
log.Fatal(x)
@@ -180,9 +181,9 @@ func createTaskServer(serverconfig ServerConfig, handler func(w context.Context,
180181
for _, middleware := range middlewares {
181182
hertzapp.Use(middleware)
182183
}
183-
hertzapp.Any("/*name", func(c context.Context, ctx *app.RequestContext) {
184-
handler(c, ctx)
185-
})
184+
// hertzapp.Any("/*name", func(c context.Context, ctx *app.RequestContext) {
185+
// handler(c, ctx)
186+
// })
186187
x := hertzapp.Run()
187188
if x != nil {
188189
log.Fatal(x)
@@ -202,9 +203,9 @@ func createTaskServer(serverconfig ServerConfig, handler func(w context.Context,
202203
for _, middleware := range middlewares {
203204
hertzapp.Use(middleware)
204205
}
205-
hertzapp.Any("/*name", func(c context.Context, ctx *app.RequestContext) {
206-
handler(c, ctx)
207-
})
206+
// hertzapp.Any("/*name", func(c context.Context, ctx *app.RequestContext) {
207+
// handler(c, ctx)
208+
// })
208209
x := hertzapp.Run()
209210
if x != nil {
210211
log.Fatal(x)

go_ws_sh/createhandlerauthorization.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"gorm.io/gorm"
1313
)
1414

15-
func createhandlerauthorization(credentialdb *gorm.DB, tokendb *gorm.DB, next func(w context.Context, r *app.RequestContext)) func(w context.Context, r *app.RequestContext) {
15+
func createhandlerauthorization_websocket(credentialdb *gorm.DB, tokendb *gorm.DB, next func(w context.Context, r *app.RequestContext)) func(w context.Context, r *app.RequestContext) {
1616

1717
return func(w context.Context, r *app.RequestContext) {
1818

go_ws_sh/pipe-std-ws-server.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ func RequestLoggerMiddleware() app.HandlerFunc {
4242
func pipe_std_ws_server(config ConfigServer, credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB) {
4343
// var listtokensHandler = ListTokensHandler(credentialdb, tokendb)
4444
// authHandler := AuthorizationHandler(credentialdb, tokendb)
45-
var routes []RouteConfig //{
45+
//{
4646

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

4949
sessions, err := ReadAllSessions(sessiondb)
5050
if err != nil {
@@ -55,7 +55,7 @@ func pipe_std_ws_server(config ConfigServer, credentialdb *gorm.DB, tokendb *gor
5555
for _, session := range sessions {
5656
handlermap[session.Name] = createhandleWebSocket(session)
5757
}
58-
var name = r.Param("name")
58+
var name = strings.TrimPrefix(string(r.Path()), "/") //r.Param("name")
5959
if handler2, ok := handlermap[name]; ok {
6060

6161
handler2(w, r)
@@ -80,7 +80,7 @@ func pipe_std_ws_server(config ConfigServer, credentialdb *gorm.DB, tokendb *gor
8080
// })
8181
var initial_credentials = config.InitialCredentials
8282
var initial_sessions = config.InitialSessions
83-
routes = GenerateRoutesHttp(credentialdb, tokendb, sessiondb, initial_credentials, initial_sessions)
83+
var routeshttp []RouteConfig = GenerateRoutesHttp(credentialdb, tokendb, sessiondb, initial_credentials, initial_sessions)
8484
// routes = append(gr, routes...)
8585
composedMiddleware := HertzCompose(
8686
MatchAndRouteMiddleware([]RouteConfig{
@@ -101,7 +101,7 @@ func pipe_std_ws_server(config ConfigServer, credentialdb *gorm.DB, tokendb *gor
101101
},
102102
}),
103103

104-
MatchAndRouteMiddleware(routes))
104+
MatchAndRouteMiddleware(routeshttp))
105105
handler := func(w context.Context, r *app.RequestContext) {
106106

107107
Upgrade := strings.ToLower(r.Request.Header.Get("Upgrade"))
@@ -142,7 +142,7 @@ func pipe_std_ws_server(config ConfigServer, credentialdb *gorm.DB, tokendb *gor
142142
for _, serverconfig := range config.Servers {
143143

144144
tasks = append(tasks, createTaskServer(serverconfig,
145-
handler, middlewares...))
145+
append(middlewares, handler)...))
146146
}
147147
// 启动服务器
148148
result, ok := PromiseAll(tasks).Receive()

0 commit comments

Comments
 (0)