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.

90 lines
1.9 KiB

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