mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-07-31 06:48: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
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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue