From 51ee66871fb82cebab536cc3d689b8c8b296e018 Mon Sep 17 00:00:00 2001 From: wjqserver <114663932+WJQSERVER@users.noreply.github.com> Date: Wed, 15 Jul 2026 02:15:54 +0800 Subject: [PATCH] feat: add HTTP QUERY method support (RFC 10008) - Add MethodQuery constant to touka.go - Add QUERY() convenience method to Engine and RouterGroup - Add examples/query/main.go demonstrating usage - All existing tests pass --- engine.go | 9 +++++++ examples/query/main.go | 53 ++++++++++++++++++++++++++++++++++++++++++ touka.go | 2 ++ 3 files changed, 64 insertions(+) create mode 100644 examples/query/main.go diff --git a/engine.go b/engine.go index 15df162..e2b6188 100644 --- a/engine.go +++ b/engine.go @@ -733,6 +733,12 @@ func (engine *Engine) OPTIONS(relativePath string, handlers ...HandlerFunc) { engine.Handle(http.MethodOptions, relativePath, handlers...) } +// QUERY 注册 QUERY 方法的路由 (RFC 10008) +// QUERY 方法是安全且幂等的,可以携带请求体,适用于复杂查询场景 +func (engine *Engine) QUERY(relativePath string, handlers ...HandlerFunc) { + engine.Handle(MethodQuery, relativePath, handlers...) +} + // ANY 注册所有常见 HTTP 方法的路由 func (engine *Engine) ANY(relativePath string, handlers ...HandlerFunc) { engine.Handle(http.MethodGet, relativePath, handlers...) @@ -804,6 +810,9 @@ func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) { func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) { group.Handle(http.MethodOptions, relativePath, handlers...) } +func (group *RouterGroup) QUERY(relativePath string, handlers ...HandlerFunc) { + group.Handle(MethodQuery, relativePath, handlers...) +} func (group *RouterGroup) ANY(relativePath string, handlers ...HandlerFunc) { group.Handle(http.MethodGet, relativePath, handlers...) group.Handle(http.MethodPost, relativePath, handlers...) diff --git a/examples/query/main.go b/examples/query/main.go new file mode 100644 index 0000000..ade9430 --- /dev/null +++ b/examples/query/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "fmt" + "net/http" + + "github.com/infinite-iroha/touka" +) + +func main() { + r := touka.New() + + // 注册QUERY路由 + // QUERY方法是RFC 10008定义的新HTTP方法 + // 它是安全且幂等的,可以携带请求体,适用于复杂查询场景 + r.QUERY("/search", func(c *touka.Context) { + // 获取请求体 + body, err := c.GetReqBodyFull() + if err != nil { + c.JSON(http.StatusBadRequest, touka.H{ + "error": "Failed to read request body", + }) + return + } + + // 返回查询结果 + c.JSON(http.StatusOK, touka.H{ + "method": c.Request.Method, + "query_body": string(body), + "description": "This is a QUERY request (RFC 10008)", + }) + }) + + // 注册路由组 + api := r.Group("/api").(*touka.RouterGroup) + { + // 在路由组中使用QUERY + api.QUERY("/data", func(c *touka.Context) { + body, _ := c.GetReqBodyFull() + c.JSON(http.StatusOK, touka.H{ + "endpoint": "/api/data", + "payload": string(body), + }) + }) + } + + fmt.Println("Server starting on :8080") + fmt.Println("Try: curl -X QUERY http://localhost:8080/search -d '{\"query\": \"test\"}'") + + if err := r.Run(touka.WithAddr(":8080")); err != nil { + fmt.Printf("Server error: %v\n", err) + } +} diff --git a/touka.go b/touka.go index 4ad81da..ebc9a90 100644 --- a/touka.go +++ b/touka.go @@ -58,6 +58,7 @@ var ( MethodConnect = "CONNECT" MethodOptions = "OPTIONS" MethodTrace = "TRACE" + MethodQuery = "QUERY" // RFC 10008 - The HTTP QUERY Method ) var MethodsSet = map[string]struct{}{ @@ -70,4 +71,5 @@ var MethodsSet = map[string]struct{}{ MethodConnect: {}, MethodOptions: {}, MethodTrace: {}, + MethodQuery: {}, }