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.
 
 

93 lines
2.1 KiB

package main
import (
"context"
"flag"
"fmt"
"git.diulo.com/mogfee/kit"
user "git.diulo.com/mogfee/kit/api"
"git.diulo.com/mogfee/kit/errors"
"git.diulo.com/mogfee/kit/example/service"
"git.diulo.com/mogfee/kit/middleware/jwt"
"git.diulo.com/mogfee/kit/transport/http"
)
var host string
func init() {
flag.StringVar(&host, "h", "localhost:9922", "")
}
func main() {
flag.Parse()
runApp(host)
}
func runApp(host string) {
tokenKey := "1234567890123456"
hs := http.NewServer(
http.Address(host),
//http.Middleware(
//logging.Server(),
//validate.Server(),
//jwt.JWT(),
//),
//http.Filter(func(handler http2.Handler) http2.Handler {
// return &cros{
// h: handler,
// }
//}),
http.Middleware(
jwt.JWT(jwt.WithJwtKey(tokenKey), jwt.WithFromKey("query:token")),
),
)
route := hs.Route("/")
route.GET("/api/v1/answer/listCategory", 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) {
//tr, _ := transport.FromServerContext(ctx)
//return AddUser(ctx, a.(*UserAddRequest))
return &UserAddResponse{Id: "xxx"}, nil
return nil, errors.New(400, "BAD", "a")
})
out, err := h(ctx, &in)
if err != nil {
return err
}
reply, _ := out.(*UserAddResponse)
reply.Id = host
return ctx.Result(200, reply)
})
user.RegisterUserHTTPServer(hs, service.NewUserService(tokenKey))
//client, err := clientv3.New(clientv3.Config{
// Endpoints: []string{"127.0.0.1:2379"},
//})
//if err != nil {
// panic(err)
//}
app := kit.New(
kit.Name("kit-server"),
kit.Version("v1.0"),
kit.ID(host),
kit.Server(hs),
//kit.Registrar(etcd.New(client)),
)
fmt.Println(app.Run())
fmt.Println(app.Stop())
}
type UserAddRequest struct {
Name string
}
type UserAddResponse struct {
Id string
}
func AddUser(ctx context.Context, request *UserAddRequest) (*UserAddResponse, error) {
//fmt.Println(jwt.FromUserContext(ctx))
//fmt.Println(jwt.FromAuthKeyContext(ctx))
return &UserAddResponse{Id: request.Name}, nil
//errors.New(500, "xx", "")
}