add
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"common/httpx"
|
"common/httpx"
|
||||||
"common/codes"
|
"common/codes"
|
||||||
|
"common/logger"
|
||||||
"user/internal/model"
|
"user/internal/model"
|
||||||
"user/internal/service"
|
"user/internal/service"
|
||||||
)
|
)
|
||||||
@@ -20,74 +21,92 @@ 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 {
|
||||||
httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed))
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("method not allowed path=%s", r.URL.Path)
|
||||||
|
httpx.MethodNotAllowed(w, r, 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 {
|
||||||
httpx.BadRequest(w, "invalid json")
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("invalid json path=%s", r.URL.Path)
|
||||||
|
httpx.BadRequest(w, r, "invalid json")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userID, token, err := h.S.Register(req.Account, req.Password)
|
userID, token, err := h.S.Register(req.Account, req.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch err {
|
switch err {
|
||||||
case service.ErrInvalidInput:
|
case service.ErrInvalidInput:
|
||||||
httpx.BadRequest(w, "invalid account or password")
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("invalid input account=%s", req.Account)
|
||||||
|
httpx.BadRequest(w, r, "invalid account or password")
|
||||||
case service.ErrAccountExists:
|
case service.ErrAccountExists:
|
||||||
httpx.Conflict(w, "account exists")
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("account exists account=%s", req.Account)
|
||||||
|
httpx.Conflict(w, r, "account exists")
|
||||||
default:
|
default:
|
||||||
httpx.InternalError(w)
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("internal error account=%s", req.Account)
|
||||||
|
httpx.InternalError(w, r)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
httpx.Created(w, map[string]string{"user_id": userID, "token": token})
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("register success user=%s", userID)
|
||||||
|
httpx.Created(w, r, map[string]string{"user_id": userID, "token": token})
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed))
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("method not allowed path=%s", r.URL.Path)
|
||||||
|
httpx.MethodNotAllowed(w, r, 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 {
|
||||||
httpx.BadRequest(w, "invalid json")
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("invalid json path=%s", r.URL.Path)
|
||||||
|
httpx.BadRequest(w, r, "invalid json")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userID, token, err := h.S.Login(req.Account, req.Password)
|
userID, token, err := h.S.Login(req.Account, req.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch err {
|
switch err {
|
||||||
case service.ErrInvalidInput:
|
case service.ErrInvalidInput:
|
||||||
httpx.BadRequest(w, "invalid account or password")
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("invalid input account=%s", req.Account)
|
||||||
|
httpx.BadRequest(w, r, "invalid account or password")
|
||||||
case service.ErrUnauthorized:
|
case service.ErrUnauthorized:
|
||||||
httpx.Unauthorized(w, "unauthorized")
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("unauthorized account=%s", req.Account)
|
||||||
|
httpx.Unauthorized(w, r, "unauthorized")
|
||||||
default:
|
default:
|
||||||
httpx.InternalError(w)
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("internal error account=%s", req.Account)
|
||||||
|
httpx.InternalError(w, r)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
httpx.OK(w, map[string]string{"user_id": userID, "token": token})
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("login success user=%s", userID)
|
||||||
|
httpx.OK(w, r, map[string]string{"user_id": userID, "token": token})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) Healthz(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) Healthz(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed))
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("method not allowed path=%s", r.URL.Path)
|
||||||
|
httpx.MethodNotAllowed(w, r, string(codes.MethodNotAllowed))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
httpx.OK(w, nil)
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("healthz ok")
|
||||||
|
httpx.OK(w, r, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) Version(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) Version(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed))
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("method not allowed path=%s", r.URL.Path)
|
||||||
|
httpx.MethodNotAllowed(w, r, string(codes.MethodNotAllowed))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
httpx.OK(w, map[string]string{"version": "user-service v0.1.0"})
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("version ok")
|
||||||
|
httpx.OK(w, r, 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) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed))
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("method not allowed path=%s", r.URL.Path)
|
||||||
|
httpx.MethodNotAllowed(w, r, string(codes.MethodNotAllowed))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
httpx.OK(w, map[string]string{"service": "user", "user_id": httpx.UserID(r)})
|
logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("root ok user=%s", httpx.UserID(r))
|
||||||
|
httpx.OK(w, r, map[string]string{"service": "user", "user_id": httpx.UserID(r)})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user