mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-07-30 22:38:18 +08:00
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
This commit is contained in:
parent
d439662adf
commit
51ee66871f
3 changed files with 64 additions and 0 deletions
|
|
@ -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...)
|
||||
|
|
|
|||
53
examples/query/main.go
Normal file
53
examples/query/main.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
2
touka.go
2
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: {},
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue