This commit is contained in:
“xHuPo” 2025-05-22 12:06:34 +08:00
parent 53e59ddf89
commit 079542e431
9 changed files with 191 additions and 57 deletions

View file

@ -1,9 +1,27 @@
package handlers
import (
"encoding/json"
"net/http"
"github.com/jmoiron/sqlx"
)
type Handler struct {
DB *sqlx.DB
}
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
func WriteJSON(w http.ResponseWriter, data interface{}, code int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
json.NewEncoder(w).Encode(data)
}
func WriteError(w http.ResponseWriter, message string, code int) {
WriteJSON(w, Response{Code: code, Message: message}, code)
}