|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"git.diulo.com/mogfee/kit"
|
|
|
|
"git.diulo.com/mogfee/kit/errors"
|
|
|
|
"git.diulo.com/mogfee/kit/example/service"
|
|
|
|
"git.diulo.com/mogfee/kit/middleware"
|
|
|
|
"git.diulo.com/mogfee/kit/middleware/logging"
|
|
|
|
user "git.diulo.com/mogfee/kit/proto/v1"
|
|
|
|
"git.diulo.com/mogfee/kit/transport/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
hs := http.NewServer(
|
|
|
|
http.Address("localhost:9093"),
|
|
|
|
http.Middleware(
|
|
|
|
logging.Server(),
|
|
|
|
func(handler middleware.Handler) middleware.Handler {
|
|
|
|
return func(ctx context.Context, a any) (any, error) {
|
|
|
|
if v, ok := a.(interface {
|
|
|
|
Validate() error
|
|
|
|
}); ok {
|
|
|
|
if err := v.Validate(); err != nil {
|
|
|
|
if a, ok := err.(interface {
|
|
|
|
Field() string
|
|
|
|
Reason() string
|
|
|
|
}); ok {
|
|
|
|
field := a.Field()
|
|
|
|
return nil, errors.New(400, "InvalidArgument", "").WithMetadata(map[string]string{
|
|
|
|
strings.ToLower(field[0:1]) + field[1:]: a.Reason(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
//if vv, ok := err.(ValidateError); ok {
|
|
|
|
// s.Result(http.StatusBadRequest, "InvalidArgument", vv.ErrorName(), map[string]string{
|
|
|
|
// lcfirst(vv.Field()): vv.Reason(),
|
|
|
|
// })
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return handler(ctx, a)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
route := hs.Route("/")
|
|
|
|
route.GET("/api/abc", func(ctx http.Context) error {
|
|
|
|
in := UserAddRequest{Name: "tom"}
|
|
|
|
http.SetOperation(ctx, "/api/abc")
|
|
|
|
h := ctx.Middleware(func(ctx context.Context, a any) (any, error) {
|
|
|
|
return AddUser(ctx, a.(*UserAddRequest))
|
|
|
|
})
|
|
|
|
out, err := h(ctx, &in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply, _ := out.(*UserAddResponse)
|
|
|
|
return ctx.Result(200, reply)
|
|
|
|
})
|
|
|
|
|
|
|
|
user.RegisterUserHTTPServer(hs, &service.UserService{})
|
|
|
|
|
|
|
|
app := kit.New(
|
|
|
|
kit.Name("kit-server"),
|
|
|
|
kit.Version("v1.0"),
|
|
|
|
kit.Server(hs),
|
|
|
|
)
|
|
|
|
fmt.Println(app.Run())
|
|
|
|
fmt.Println(app.Stop())
|
|
|
|
}
|
|
|
|
func ma1in() {
|
|
|
|
//gin.SetMode(gin.ReleaseMode)
|
|
|
|
//app := gin.Default()
|
|
|
|
//srv := service.UserService{}
|
|
|
|
//logger := log.With(log.DefaultLogger)
|
|
|
|
//user.RegisterUserHandler(app, &srv, middleware.Logger(logger), middleware.Validate())
|
|
|
|
//fmt.Println("http://localhost:8888")
|
|
|
|
//app.Run("localhost:8888")
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserAddRequest struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
type UserAddResponse struct {
|
|
|
|
Id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddUser(ctx context.Context, request *UserAddRequest) (*UserAddResponse, error) {
|
|
|
|
return &UserAddResponse{Id: request.Name}, errors.New(500, "xx", "")
|
|
|
|
}
|