diff --git a/example/main.go b/example/main.go index 5f5842e..e56d1e8 100644 --- a/example/main.go +++ b/example/main.go @@ -5,19 +5,30 @@ import ( "flag" "fmt" "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" + http2 "net/http" ) var host string 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() { flag.Parse() runApp(host) @@ -25,32 +36,37 @@ func main() { func runApp(host string) { hs := http.NewServer( http.Address(host), - http.Middleware( - logging.Server(), - validate.Server(), - jwt.JWT(), - ), + //http.Middleware( + //logging.Server(), + //validate.Server(), + //jwt.JWT(), + //), + http.Filter(func(handler http2.Handler) http2.Handler { + return &cros{ + h: handler, + } + }), ) - //route := hs.Route("/") - //route.GET("/", 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)) - // }) - // if tr, ok := transport.FromServerContext(ctx); ok { - // fmt.Println(tr.Operation()) - // } - // out, err := h(ctx, &in) - // if err != nil { - // return err - // } - // reply, _ := out.(*UserAddResponse) - // reply.Id = host - // return ctx.Result(200, reply) - //}) + route := hs.Route("/") + route.HEAD("/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) { + return AddUser(ctx, a.(*UserAddRequest)) + }) + //if tr, ok := transport.FromServerContext(ctx); ok { + // fmt.Println(tr.Operation()) + //} + 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.UserService{}) + //user.RegisterUserHTTPServer(hs, &service.UserService{}) //client, err := clientv3.New(clientv3.Config{ // Endpoints: []string{"127.0.0.1:2379"}, //}) @@ -76,8 +92,8 @@ type UserAddResponse struct { } func AddUser(ctx context.Context, request *UserAddRequest) (*UserAddResponse, error) { - fmt.Println(jwt.FromUserContext(ctx)) - fmt.Println(jwt.FromAuthKeyContext(ctx)) + //fmt.Println(jwt.FromUserContext(ctx)) + //fmt.Println(jwt.FromAuthKeyContext(ctx)) return &UserAddResponse{Id: request.Name}, nil //errors.New(500, "xx", "") } diff --git a/go.mod b/go.mod index f2b5836..0cba202 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,6 @@ require ( github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/go-systemd/v22 v22.3.2 // indirect - github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect diff --git a/middleware/jwt/jwt.go b/middleware/jwt/jwt.go index f0ab65a..a01b013 100644 --- a/middleware/jwt/jwt.go +++ b/middleware/jwt/jwt.go @@ -101,5 +101,9 @@ func SetAuthKeyContext(ctx context.Context, key string) context.Context { return context.WithValue(ctx, authKey{}, key) } func FromAuthKeyContext(ctx context.Context) string { - return ctx.Value(authKey{}).(string) + v := ctx.Value(authKey{}) + if v == nil { + return "" + } + return v.(string) } diff --git a/transport/http/context.go b/transport/http/context.go index 329ae35..8f932fd 100644 --- a/transport/http/context.go +++ b/transport/http/context.go @@ -158,6 +158,10 @@ func (w *wrapper) Returns(i interface{}, err error) error { func (w *wrapper) Result(i int, i2 interface{}) error { w.w.WriteHeader(i) + w.res.Header().Set("Access-Control-Allow-Origin", "*") + w.res.Header().Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,PATCH,DELETE") + w.res.Header().Set("Access-Control-Allow-Credentials", "true") + w.res.Header().Set("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,Access-Control-Allow-Credentials,User-Agent,Content-Length,Authorization") return w.router.srv.enc(&w.w, w.req, i2) }