Files
eightbf-analysis-go/internal/api/response.go
T
gozerosandCursor 0c45b86d67 chore: 初始化 eightbf-analysis-go(对齐 Java 会商作业服务)
Go 实现 /api/v1/analysis 契约、Demo/Live Worker、DeerFlow SSE;含设计文档与冒烟脚本。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-03 00:13:46 +08:00

32 lines
888 B
Go

package api
import (
"encoding/json"
"net/http"
)
// ApiResponse 与 Java 一致:{ code, message, data };成功 code=0。
type ApiResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func writeJSON(w http.ResponseWriter, status int, body interface{}) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(body)
}
func OK(w http.ResponseWriter, data interface{}) {
writeJSON(w, http.StatusOK, ApiResponse{Code: 0, Message: "ok", Data: data})
}
func Created(w http.ResponseWriter, data interface{}) {
writeJSON(w, http.StatusCreated, ApiResponse{Code: 0, Message: "ok", Data: data})
}
func Fail(w http.ResponseWriter, httpStatus, code int, msg string) {
writeJSON(w, httpStatus, ApiResponse{Code: code, Message: msg, Data: nil})
}