initial commit
This commit is contained in:
28
gateway/internal/handler/getuserinfohandler.go
Normal file
28
gateway/internal/handler/getuserinfohandler.go
Normal file
@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"godemo/gateway/internal/logic"
|
||||
"godemo/gateway/internal/svc"
|
||||
"godemo/gateway/internal/types"
|
||||
)
|
||||
|
||||
func getUserInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetUserInfoReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewGetUserInfoLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetUserInfo(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
51
gateway/internal/handler/loginhandler.go
Normal file
51
gateway/internal/handler/loginhandler.go
Normal file
@ -0,0 +1,51 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"godemo/gateway/internal/logic"
|
||||
"godemo/gateway/internal/svc"
|
||||
"godemo/gateway/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func loginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.LoginReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewLoginLogic(r.Context(), svcCtx)
|
||||
resp, err := l.Login(&req)
|
||||
|
||||
if err != nil {
|
||||
if s, ok := status.FromError(err); ok {
|
||||
// 判断 gRPC 错误码,并自定义 HTTP 返回
|
||||
var code int
|
||||
switch s.Code() {
|
||||
case codes.NotFound:
|
||||
code = http.StatusNotFound
|
||||
case codes.InvalidArgument:
|
||||
code = http.StatusBadRequest
|
||||
default:
|
||||
code = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
// 自定义 JSON 错误格式
|
||||
httpx.WriteJson(w, code, map[string]interface{}{
|
||||
"code": code,
|
||||
"message": s.Message(),
|
||||
})
|
||||
} else {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
}
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
28
gateway/internal/handler/logouthandler.go
Normal file
28
gateway/internal/handler/logouthandler.go
Normal file
@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"godemo/gateway/internal/logic"
|
||||
"godemo/gateway/internal/svc"
|
||||
"godemo/gateway/internal/types"
|
||||
)
|
||||
|
||||
func logoutHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.LogoutReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewLogoutLogic(r.Context(), svcCtx)
|
||||
resp, err := l.Logout(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
28
gateway/internal/handler/registerhandler.go
Normal file
28
gateway/internal/handler/registerhandler.go
Normal file
@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"godemo/gateway/internal/logic"
|
||||
"godemo/gateway/internal/svc"
|
||||
"godemo/gateway/internal/types"
|
||||
)
|
||||
|
||||
func registerHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.RegisterReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewRegisterLogic(r.Context(), svcCtx)
|
||||
resp, err := l.Register(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
45
gateway/internal/handler/routes.go
Normal file
45
gateway/internal/handler/routes.go
Normal file
@ -0,0 +1,45 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.8.3
|
||||
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"godemo/gateway/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/user/login",
|
||||
Handler: loginHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/user/register",
|
||||
Handler: registerHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/user/:user_id",
|
||||
Handler: getUserInfoHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/user/logout",
|
||||
Handler: logoutHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithJwt(serverCtx.Config.JwtAuth.AccessSecret),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user