This commit is contained in:
vipg
2026-02-09 16:59:04 +08:00
parent 611b3b307c
commit ec07824a4d
3 changed files with 72 additions and 22 deletions

View File

@@ -0,0 +1,12 @@
package codes
type Code string
const (
OK Code = "ok"
InvalidInput Code = "invalid_input"
Unauthorized Code = "unauthorized"
Conflict Code = "conflict"
InternalError Code = "internal_error"
MethodNotAllowed Code = "method_not_allowed"
)

View File

@@ -0,0 +1,43 @@
package httpx
import (
"encoding/json"
"net/http"
"common/types"
"common/codes"
)
func WriteJSON(w http.ResponseWriter, status int, ok bool, msg string, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(types.Response{Status: ok, Message: msg, Data: data})
}
func OK(w http.ResponseWriter, data interface{}) {
WriteJSON(w, http.StatusOK, true, string(codes.OK), data)
}
func Created(w http.ResponseWriter, data interface{}) {
WriteJSON(w, http.StatusCreated, true, string(codes.OK), data)
}
func BadRequest(w http.ResponseWriter, msg string) {
WriteJSON(w, http.StatusBadRequest, false, msg, map[string]string{"code": string(codes.InvalidInput)})
}
func Unauthorized(w http.ResponseWriter, msg string) {
WriteJSON(w, http.StatusUnauthorized, false, msg, map[string]string{"code": string(codes.Unauthorized)})
}
func Conflict(w http.ResponseWriter, msg string) {
WriteJSON(w, http.StatusConflict, false, msg, map[string]string{"code": string(codes.Conflict)})
}
func MethodNotAllowed(w http.ResponseWriter, msg string) {
WriteJSON(w, http.StatusMethodNotAllowed, false, msg, map[string]string{"code": string(codes.MethodNotAllowed)})
}
func InternalError(w http.ResponseWriter) {
WriteJSON(w, http.StatusInternalServerError, false, string(codes.InternalError), map[string]string{"code": string(codes.InternalError)})
}

View File

@@ -4,7 +4,8 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"common/types" "common/httpx"
"common/codes"
"user/internal/model" "user/internal/model"
"user/internal/service" "user/internal/service"
) )
@@ -19,68 +20,62 @@ func New(s *service.Service) *Handler {
func (h *Handler) Register(w http.ResponseWriter, r *http.Request) { func (h *Handler) Register(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
writeJSON(w, http.StatusMethodNotAllowed, false, "method not allowed", nil) httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed))
return return
} }
var req model.RegisterReq var req model.RegisterReq
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, http.StatusBadRequest, false, "invalid json", nil) httpx.BadRequest(w, "invalid json")
return return
} }
userID, err := h.S.Register(req.Account, req.Password) userID, err := h.S.Register(req.Account, req.Password)
if err != nil { if err != nil {
switch err { switch err {
case service.ErrInvalidInput: case service.ErrInvalidInput:
writeJSON(w, http.StatusBadRequest, false, "invalid account or password", nil) httpx.BadRequest(w, "invalid account or password")
case service.ErrAccountExists: case service.ErrAccountExists:
writeJSON(w, http.StatusConflict, false, "account exists", nil) httpx.Conflict(w, "account exists")
default: default:
writeJSON(w, http.StatusInternalServerError, false, "internal error", nil) httpx.InternalError(w)
} }
return return
} }
writeJSON(w, http.StatusCreated, true, "ok", map[string]string{"user_id": userID}) httpx.Created(w, map[string]string{"user_id": userID})
} }
func (h *Handler) Login(w http.ResponseWriter, r *http.Request) { func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
writeJSON(w, http.StatusMethodNotAllowed, false, "method not allowed", nil) httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed))
return return
} }
var req model.LoginReq var req model.LoginReq
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, http.StatusBadRequest, false, "invalid json", nil) httpx.BadRequest(w, "invalid json")
return return
} }
userID, err := h.S.Login(req.Account, req.Password) userID, err := h.S.Login(req.Account, req.Password)
if err != nil { if err != nil {
switch err { switch err {
case service.ErrInvalidInput: case service.ErrInvalidInput:
writeJSON(w, http.StatusBadRequest, false, "invalid account or password", nil) httpx.BadRequest(w, "invalid account or password")
case service.ErrUnauthorized: case service.ErrUnauthorized:
writeJSON(w, http.StatusUnauthorized, false, "unauthorized", nil) httpx.Unauthorized(w, "unauthorized")
default: default:
writeJSON(w, http.StatusInternalServerError, false, "internal error", nil) httpx.InternalError(w)
} }
return return
} }
writeJSON(w, http.StatusOK, true, "ok", map[string]string{"user_id": userID}) httpx.OK(w, map[string]string{"user_id": userID})
} }
func (h *Handler) Healthz(w http.ResponseWriter, r *http.Request) { func (h *Handler) Healthz(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, true, "ok", nil) httpx.OK(w, nil)
} }
func (h *Handler) Version(w http.ResponseWriter, r *http.Request) { func (h *Handler) Version(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, true, "ok", map[string]string{"version": "user-service v0.1.0"}) httpx.OK(w, map[string]string{"version": "user-service v0.1.0"})
} }
func (h *Handler) Root(w http.ResponseWriter, r *http.Request) { func (h *Handler) Root(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, true, "ok", map[string]string{"service": "user"}) httpx.OK(w, map[string]string{"service": "user"})
}
func writeJSON(w http.ResponseWriter, code int, status bool, msg string, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
json.NewEncoder(w).Encode(types.Response{Status: status, Message: msg, Data: data})
} }