diff --git a/example/main.go b/example/main.go index 8dd4a8a..5707641 100644 --- a/example/main.go +++ b/example/main.go @@ -5,8 +5,9 @@ import ( "flag" "fmt" "git.diulo.com/mogfee/kit" + "git.diulo.com/mogfee/kit/errors" + "git.diulo.com/mogfee/kit/middleware/cors" "git.diulo.com/mogfee/kit/transport/http" - http2 "net/http" ) var host string @@ -15,20 +16,6 @@ func init() { 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) @@ -47,15 +34,16 @@ func runApp(host string) { // h: handler, // } //}), - http.Middleware(), + http.Middleware(cors.Cors()), ) route := hs.Route("/") - route.GET("/api/v1/answer/listCategory", func(ctx http.Context) error { + route.POST("/api/v1/answer/addQuestion", 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 AddUser(ctx, a.(*UserAddRequest)) + return nil, errors.New(400, "BAD", "a") }) out, err := h(ctx, &in) if err != nil { diff --git a/transport/http/codec.go b/transport/http/codec.go index cb0e25d..aa47131 100644 --- a/transport/http/codec.go +++ b/transport/http/codec.go @@ -73,6 +73,7 @@ func DefaultResponseEncoder(w http.ResponseWriter, r *http.Request, v any) error return err } func DefaultErrorEncoder(w http.ResponseWriter, r *http.Request, err error) { + fmt.Printf("%+v\n", w.Header()) se := errors.FromError(err) codec, _ := CodeForRequest(r, "Accept") body, err := codec.Marshal(se) @@ -81,12 +82,6 @@ func DefaultErrorEncoder(w http.ResponseWriter, r *http.Request, err error) { return } w.Header().Set("Content-Type", httputil.ContentType(codec.Name())) - - //w.Header().Set("Access-Control-Allow-Origin", "*") - //w.Header().Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,PATCH,DELETE") - //w.Header().Set("Access-Control-Allow-Credentials", "true") - //w.Header().Set("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,Access-Control-Allow-Credentials,User-Agent,Content-Length,Authorization") - w.WriteHeader(int(se.Status.Code)) _, _ = w.Write(body) } diff --git a/transport/http/server.go b/transport/http/server.go index d073c6d..75c68d7 100644 --- a/transport/http/server.go +++ b/transport/http/server.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "errors" + "fmt" "git.diulo.com/mogfee/kit/internal/endpoint" "git.diulo.com/mogfee/kit/internal/host" "git.diulo.com/mogfee/kit/internal/matcher" @@ -306,6 +307,7 @@ func (s *Server) filter() mux.MiddlewareFunc { if s.endpoint != nil { tr.endpoint = s.endpoint.String() } + fmt.Println("tr") tr.request = req.WithContext(transport.NewServerContext(ctx, tr)) next.ServeHTTP(w, tr.request) })