You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.6 KiB

2 years ago
package user
import (
2 years ago
"context"
"git.diulo.com/mogfee/protoc-gen-kit/middleware"
2 years ago
"git.diulo.com/mogfee/protoc-gen-kit/pkg/xerrors"
2 years ago
"github.com/gin-gonic/gin"
)
2 years ago
func RegisterUserHandler(app *gin.Engine, srv UserServer, m ...middleware.Middleware) {
app.GET("/api/v1/user/list", httpListHandler(srv, m...))
app.POST("/api/v1/user/login", httpLoginHandler(srv, m...))
2 years ago
}
2 years ago
func httpListHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Context) {
2 years ago
return func(c *gin.Context) {
2 years ago
var post LoginRequest
2 years ago
resp := response.New(c)
2 years ago
if err := resp.BindQuery(&post); err != nil {
resp.Error(err)
return
}
2 years ago
h := func(ctx context.Context, a any) (any, error) {
return srv.List(ctx, a.(*LoginRequest))
}
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post)
2 years ago
if err != nil {
resp.Error(err)
2 years ago
} else {
if v, ok := out.(*LoginResponse); ok {
resp.Success(v)
} else {
resp.Error(xerrors.InternalServer("RESULT_TYPE_ERROR", "LoginResponse"))
}
2 years ago
}
2 years ago
}
2 years ago
}
2 years ago
func httpLoginHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Context) {
2 years ago
return func(c *gin.Context) {
2 years ago
var post LoginRequest
2 years ago
resp := response.New(c)
2 years ago
if err := resp.BindJSON(&post); err != nil {
resp.Error(err)
return
}
2 years ago
h := func(ctx context.Context, a any) (any, error) {
2 years ago
return srv.Login(ctx, a.(*LoginRequest))
2 years ago
}
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post)
2 years ago
if err != nil {
resp.Error(err)
2 years ago
} else {
if v, ok := out.(*LoginResponse); ok {
resp.Success(v)
} else {
resp.Error(xerrors.InternalServer("RESULT_TYPE_ERROR", "LoginResponse"))
}
2 years ago
}
2 years ago
}
2 years ago
}