Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

queryHandlerBytes.go 974B

123456789101112131415161718192021222324252627282930313233343536
  1. package myhandle
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "git.x2erp.com/qdy/go-base/types"
  6. "git.x2erp.com/qdy/go-db/factory/database"
  7. )
  8. // 最简单的 queryHandler,只处理 []byte 返回
  9. func QueryHandlerBytes(
  10. w http.ResponseWriter,
  11. r *http.Request,
  12. dbFactory *database.DBFactory,
  13. handlerFunc func(*database.DBFactory, types.QueryRequest) []byte,
  14. ) {
  15. // 解析请求参数
  16. var req types.QueryRequest
  17. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  18. // 返回 CSV 格式的错误信息
  19. errorCSV := "error,Invalid request body\n"
  20. w.Header().Set("Content-Type", "text/csv")
  21. w.WriteHeader(http.StatusBadRequest)
  22. w.Write([]byte(errorCSV))
  23. return
  24. }
  25. // 调用业务逻辑函数
  26. csvData := handlerFunc(dbFactory, req)
  27. // 直接返回 CSV 数据(包含错误信息时也会被正确处理)
  28. w.Header().Set("Content-Type", "text/csv")
  29. w.Header().Set("Content-Disposition", "attachment; filename=query_result.csv")
  30. w.Write(csvData)
  31. }