| 1234567891011121314151617181920212223242526272829303132333435363738 |
- package myhandle
-
- import (
- "encoding/json"
- "net/http"
- "time"
-
- "git.x2erp.com/qdy/go-base/types"
- "git.x2erp.com/qdy/go-db/factory/database"
- )
-
- // queryHandler - 最简单的版本
- func QueryHandlerJson(
- w http.ResponseWriter,
- r *http.Request,
- dbFactory *database.DBFactory,
- handlerFunc func(*database.DBFactory, types.QueryRequest) *types.QueryResult,
- ) {
- // 解析请求参数
- var req types.QueryRequest
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- // 如果解析失败,也返回 QueryResult 格式
- w.Header().Set("Content-Type", "application/json")
- json.NewEncoder(w).Encode(&types.QueryResult{
- Success: false,
- Error: "Invalid request body: " + err.Error(),
- Time: time.Now().Format(time.RFC3339),
- })
- return
- }
-
- // 执行业务逻辑
- result := handlerFunc(dbFactory, req)
-
- // 设置头,写入结果
- w.Header().Set("Content-Type", "application/json")
- json.NewEncoder(w).Encode(result)
- }
|