otpm/handlers/handler.go
“xHuPo” 079542e431 alpha
2025-05-22 12:06:34 +08:00

27 lines
574 B
Go

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)
}