|
|
@ -5,19 +5,30 @@ import ( |
|
|
|
"flag" |
|
|
|
"flag" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"git.diulo.com/mogfee/kit" |
|
|
|
"git.diulo.com/mogfee/kit" |
|
|
|
user "git.diulo.com/mogfee/kit/api" |
|
|
|
|
|
|
|
"git.diulo.com/mogfee/kit/example/service" |
|
|
|
|
|
|
|
"git.diulo.com/mogfee/kit/middleware/jwt" |
|
|
|
|
|
|
|
"git.diulo.com/mogfee/kit/middleware/logging" |
|
|
|
|
|
|
|
"git.diulo.com/mogfee/kit/middleware/validate" |
|
|
|
|
|
|
|
"git.diulo.com/mogfee/kit/transport/http" |
|
|
|
"git.diulo.com/mogfee/kit/transport/http" |
|
|
|
|
|
|
|
http2 "net/http" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var host string |
|
|
|
var host string |
|
|
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
func init() { |
|
|
|
flag.StringVar(&host, "h", "localhost:9093", "") |
|
|
|
flag.StringVar(&host, "h", "localhost:9922", "") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type cros struct { |
|
|
|
|
|
|
|
h http2.Handler |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *cros) ServeHTTP(writer http2.ResponseWriter, request *http2.Request) { |
|
|
|
|
|
|
|
request.Header.Set("Access-Control-Allow-Origin", "*") |
|
|
|
|
|
|
|
request.Header.Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,PATCH,DELETE") |
|
|
|
|
|
|
|
request.Header.Set("Access-Control-Allow-Credentials", "true") |
|
|
|
|
|
|
|
request.Header.Set("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,Access-Control-Allow-Credentials,User-Agent,Content-Length,Authorization") |
|
|
|
|
|
|
|
request.Header.Set("xxxxx", "11") |
|
|
|
|
|
|
|
fmt.Println("xxx") |
|
|
|
|
|
|
|
c.h.ServeHTTP(writer, request) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
|
flag.Parse() |
|
|
|
flag.Parse() |
|
|
|
runApp(host) |
|
|
|
runApp(host) |
|
|
@ -25,32 +36,37 @@ func main() { |
|
|
|
func runApp(host string) { |
|
|
|
func runApp(host string) { |
|
|
|
hs := http.NewServer( |
|
|
|
hs := http.NewServer( |
|
|
|
http.Address(host), |
|
|
|
http.Address(host), |
|
|
|
http.Middleware( |
|
|
|
//http.Middleware(
|
|
|
|
logging.Server(), |
|
|
|
//logging.Server(),
|
|
|
|
validate.Server(), |
|
|
|
//validate.Server(),
|
|
|
|
jwt.JWT(), |
|
|
|
//jwt.JWT(),
|
|
|
|
), |
|
|
|
//),
|
|
|
|
|
|
|
|
http.Filter(func(handler http2.Handler) http2.Handler { |
|
|
|
|
|
|
|
return &cros{ |
|
|
|
|
|
|
|
h: handler, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}), |
|
|
|
) |
|
|
|
) |
|
|
|
//route := hs.Route("/")
|
|
|
|
route := hs.Route("/") |
|
|
|
//route.GET("/", func(ctx http.Context) error {
|
|
|
|
route.HEAD("/api/v1/answer/listCategory", func(ctx http.Context) error { |
|
|
|
// in := UserAddRequest{Name: "tom"}
|
|
|
|
in := UserAddRequest{Name: "tom"} |
|
|
|
// http.SetOperation(ctx, "/api/abc")
|
|
|
|
http.SetOperation(ctx, "/api/abc") |
|
|
|
// h := ctx.Middleware(func(ctx context.Context, a any) (any, error) {
|
|
|
|
h := ctx.Middleware(func(ctx context.Context, a any) (any, error) { |
|
|
|
// return AddUser(ctx, a.(*UserAddRequest))
|
|
|
|
return AddUser(ctx, a.(*UserAddRequest)) |
|
|
|
// })
|
|
|
|
}) |
|
|
|
//if tr, ok := transport.FromServerContext(ctx); ok {
|
|
|
|
//if tr, ok := transport.FromServerContext(ctx); ok {
|
|
|
|
// fmt.Println(tr.Operation())
|
|
|
|
// fmt.Println(tr.Operation())
|
|
|
|
//}
|
|
|
|
//}
|
|
|
|
// out, err := h(ctx, &in)
|
|
|
|
out, err := h(ctx, &in) |
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// return err
|
|
|
|
return err |
|
|
|
// }
|
|
|
|
} |
|
|
|
// reply, _ := out.(*UserAddResponse)
|
|
|
|
reply, _ := out.(*UserAddResponse) |
|
|
|
// reply.Id = host
|
|
|
|
reply.Id = host |
|
|
|
// return ctx.Result(200, reply)
|
|
|
|
return ctx.Result(200, reply) |
|
|
|
//})
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
user.RegisterUserHTTPServer(hs, &service.UserService{}) |
|
|
|
//user.RegisterUserHTTPServer(hs, &service.UserService{})
|
|
|
|
//client, err := clientv3.New(clientv3.Config{
|
|
|
|
//client, err := clientv3.New(clientv3.Config{
|
|
|
|
// Endpoints: []string{"127.0.0.1:2379"},
|
|
|
|
// Endpoints: []string{"127.0.0.1:2379"},
|
|
|
|
//})
|
|
|
|
//})
|
|
|
@ -76,8 +92,8 @@ type UserAddResponse struct { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func AddUser(ctx context.Context, request *UserAddRequest) (*UserAddResponse, error) { |
|
|
|
func AddUser(ctx context.Context, request *UserAddRequest) (*UserAddResponse, error) { |
|
|
|
fmt.Println(jwt.FromUserContext(ctx)) |
|
|
|
//fmt.Println(jwt.FromUserContext(ctx))
|
|
|
|
fmt.Println(jwt.FromAuthKeyContext(ctx)) |
|
|
|
//fmt.Println(jwt.FromAuthKeyContext(ctx))
|
|
|
|
return &UserAddResponse{Id: request.Name}, nil |
|
|
|
return &UserAddResponse{Id: request.Name}, nil |
|
|
|
//errors.New(500, "xx", "")
|
|
|
|
//errors.New(500, "xx", "")
|
|
|
|
} |
|
|
|
} |
|
|
|