diff --git a/net/http/httpServer/f_group.go b/net/http/httpServer/f_group.go index 5c10726..7ee919a 100644 --- a/net/http/httpServer/f_group.go +++ b/net/http/httpServer/f_group.go @@ -51,7 +51,7 @@ func (g *RouterGroup) addRoute(method, p string, handler http.Handler) { // Ws 添加 WebSocket 支持 func (g *RouterGroup) Ws(p string, handler func(ws *websocket.Conn)) { fullPath := g.calculatePrefix(p) - g.service.addWSRoute(fullPath, handler) + g.service.addWSRoute("GET "+fullPath, handler) } // Use 支持链式调用 diff --git a/net/http/server/group.go b/net/http/server/group.go index 438629c..a494e4f 100644 --- a/net/http/server/group.go +++ b/net/http/server/group.go @@ -1,6 +1,7 @@ package server import ( + "fmt" "net/http" "strings" @@ -132,19 +133,25 @@ func (g *Group[T]) TraceWithHandler(p string, handler http.Handler, descriptions } func (g *Group[T]) Any(p string, handler http.HandlerFunc, descriptions ...Description[T]) { - g.addRoute("", p, handler, descriptions...) + for _, method := range HttpMethodList { + g.addRoute(method, p, handler, descriptions...) + } } func (g *Group[T]) AnyWithHandler(p string, handler http.Handler, descriptions ...Description[T]) { - g.addRoute("", p, handler, descriptions...) + for _, method := range HttpMethodList { + g.addRoute(method, p, handler, descriptions...) + } } +// WS 对于WS类型,由于新版本的http server,不支持混合路由。 +// 所以指定成GET func (g *Group[T]) WS(p string, handler func(ws *websocket.Conn)) { fullPath := g.calculatePrefix(strings.TrimSpace(p)) - g.serv.AddWebsocketRoute(fullPath, handler) + g.serv.AddWebsocketRoute(fmt.Sprintf("%s %s", http.MethodGet, fullPath), handler) } func (g *Group[T]) WSWithDescription(p string, handler func(ws *websocket.Conn), description Description[T]) { fullPath := g.calculatePrefix(strings.TrimSpace(p)) - g.serv.AddWebsocketRouteWithDescription(fullPath, handler, description) + g.serv.AddWebsocketRouteWithDescription(fmt.Sprintf("%s %s", http.MethodGet, fullPath), handler, description) } diff --git a/net/http/server/types.go b/net/http/server/types.go index 589c465..6ce1601 100644 --- a/net/http/server/types.go +++ b/net/http/server/types.go @@ -105,3 +105,15 @@ func NewDesc[T any](key, code string, meta *T) Description[T] { Metadata: meta, } } + +var HttpMethodList = []string{ + http.MethodGet, + http.MethodHead, + http.MethodPost, + http.MethodPut, + http.MethodPatch, + http.MethodDelete, + http.MethodConnect, + http.MethodOptions, + http.MethodTrace, +} diff --git a/net/http/session/storage/carrier_rdbms/rdbms.go b/net/http/session/storage/carrier_rdbms/rdbms.go index de7f461..fc0adbd 100644 --- a/net/http/session/storage/carrier_rdbms/rdbms.go +++ b/net/http/session/storage/carrier_rdbms/rdbms.go @@ -19,7 +19,7 @@ func New(db *gorm.DB) *Instance { ins := &Instance{ db: db.Session(&gorm.Session{}), } - userDb.AutoCreateTableWithStruct(ins.db, session.Session{}, "创建session表失败") + userDb.AutoCreateTableWithStruct(ins.db, session.Session{}, "创建 session 表失败") return ins }