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.
132 lines
3.5 KiB
132 lines
3.5 KiB
package user |
|
|
|
import ( |
|
"github.com/gin-gonic/gin" |
|
"context" |
|
"git.diulo.com/mogfee/kit/middleware" |
|
"git.diulo.com/mogfee/kit/errors" |
|
"git.diulo.com/mogfee/kit/response" |
|
) |
|
|
|
func RegisterUserHandler(app *gin.Engine, srv UserServer, m ...middleware.Middleware) { |
|
app.GET("/api/v1/user/list", httpListHandler(srv, m...)) |
|
app.GET("/api/v1/user/all", httpAllHandler(srv, m...)) |
|
app.GET("/api/v1/user/auto", httpAutoHandler(srv, m...)) |
|
app.GET("/api/v1/user/login_list", httpLoginWithListHandler(srv, m...)) |
|
app.GET("/api/v1/user/login", httpLoginHandler(srv, m...)) |
|
} |
|
func httpListHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Context) { |
|
return func(c *gin.Context) { |
|
var post Request |
|
resp := response.New(c) |
|
if err := resp.BindQuery(&post); err != nil { |
|
resp.Error(err) |
|
return |
|
} |
|
h := func(ctx context.Context, a any) (any, error) { |
|
return srv.List(ctx, a.(*Request)) |
|
} |
|
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post) |
|
if err != nil { |
|
resp.Error(err) |
|
} else { |
|
if v, ok := out.(*Response); ok { |
|
resp.Success(v) |
|
} else { |
|
resp.Error(errors.InternalServer("RESULT_TYPE_ERROR", "Response")) |
|
} |
|
} |
|
} |
|
} |
|
func httpAllHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Context) { |
|
return func(c *gin.Context) { |
|
var post Request |
|
resp := response.New(c) |
|
if err := resp.BindQuery(&post); err != nil { |
|
resp.Error(err) |
|
return |
|
} |
|
h := func(ctx context.Context, a any) (any, error) { |
|
return srv.All(ctx, a.(*Request)) |
|
} |
|
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post) |
|
if err != nil { |
|
resp.Error(err) |
|
} else { |
|
if v, ok := out.(*Response); ok { |
|
resp.Success(v) |
|
} else { |
|
resp.Error(errors.InternalServer("RESULT_TYPE_ERROR", "Response")) |
|
} |
|
} |
|
} |
|
} |
|
func httpAutoHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Context) { |
|
return func(c *gin.Context) { |
|
var post Request |
|
resp := response.New(c) |
|
if err := resp.BindQuery(&post); err != nil { |
|
resp.Error(err) |
|
return |
|
} |
|
h := func(ctx context.Context, a any) (any, error) { |
|
return srv.Auto(ctx, a.(*Request)) |
|
} |
|
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post) |
|
if err != nil { |
|
resp.Error(err) |
|
} else { |
|
if v, ok := out.(*Response); ok { |
|
resp.Success(v) |
|
} else { |
|
resp.Error(errors.InternalServer("RESULT_TYPE_ERROR", "Response")) |
|
} |
|
} |
|
} |
|
} |
|
func httpLoginWithListHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Context) { |
|
return func(c *gin.Context) { |
|
var post Request |
|
resp := response.New(c) |
|
if err := resp.BindQuery(&post); err != nil { |
|
resp.Error(err) |
|
return |
|
} |
|
h := func(ctx context.Context, a any) (any, error) { |
|
return srv.LoginWithList(ctx, a.(*Request)) |
|
} |
|
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post) |
|
if err != nil { |
|
resp.Error(err) |
|
} else { |
|
if v, ok := out.(*Response); ok { |
|
resp.Success(v) |
|
} else { |
|
resp.Error(errors.InternalServer("RESULT_TYPE_ERROR", "Response")) |
|
} |
|
} |
|
} |
|
} |
|
func httpLoginHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Context) { |
|
return func(c *gin.Context) { |
|
var post Request |
|
resp := response.New(c) |
|
if err := resp.BindQuery(&post); err != nil { |
|
resp.Error(err) |
|
return |
|
} |
|
h := func(ctx context.Context, a any) (any, error) { |
|
return srv.Login(ctx, a.(*Request)) |
|
} |
|
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post) |
|
if err != nil { |
|
resp.Error(err) |
|
} else { |
|
if v, ok := out.(*Response); ok { |
|
resp.Success(v) |
|
} else { |
|
resp.Error(errors.InternalServer("RESULT_TYPE_ERROR", "Response")) |
|
} |
|
} |
|
} |
|
}
|
|
|