李伟乐 2 years ago
parent 6b22d0b25e
commit a52f57a6fd
  1. 5
      example/main.go
  2. 1
      transport/http/codec.go
  3. 2
      transport/http/server.go

@ -35,15 +35,16 @@ func runApp(host string) {
// } // }
//}), //}),
http.Middleware( http.Middleware(
cors.Cors(cors.WithHeader("Content-Type,X-Requested-With,Access-Control-Allow-Credentials,User-Agent,Content-Length,Authorization,token"))), cors.Cors()),
) )
route := hs.Route("/") route := hs.Route("/")
route.POST("/api/v1/answer/addQuestion", func(ctx http.Context) error { route.GET("/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) {
//tr, _ := transport.FromServerContext(ctx) //tr, _ := transport.FromServerContext(ctx)
//return AddUser(ctx, a.(*UserAddRequest)) //return AddUser(ctx, a.(*UserAddRequest))
return &UserAddResponse{Id: "xxx"}, nil
return nil, errors.New(400, "BAD", "a") return nil, errors.New(400, "BAD", "a")
}) })
out, err := h(ctx, &in) out, err := h(ctx, &in)

@ -73,7 +73,6 @@ func DefaultResponseEncoder(w http.ResponseWriter, r *http.Request, v any) error
return err return err
} }
func DefaultErrorEncoder(w http.ResponseWriter, r *http.Request, err error) { func DefaultErrorEncoder(w http.ResponseWriter, r *http.Request, err error) {
fmt.Printf("%+v\n", w.Header())
se := errors.FromError(err) se := errors.FromError(err)
codec, _ := CodeForRequest(r, "Accept") codec, _ := CodeForRequest(r, "Accept")
body, err := codec.Marshal(se) body, err := codec.Marshal(se)

@ -4,7 +4,6 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"errors" "errors"
"fmt"
"git.diulo.com/mogfee/kit/internal/endpoint" "git.diulo.com/mogfee/kit/internal/endpoint"
"git.diulo.com/mogfee/kit/internal/host" "git.diulo.com/mogfee/kit/internal/host"
"git.diulo.com/mogfee/kit/internal/matcher" "git.diulo.com/mogfee/kit/internal/matcher"
@ -307,7 +306,6 @@ func (s *Server) filter() mux.MiddlewareFunc {
if s.endpoint != nil { if s.endpoint != nil {
tr.endpoint = s.endpoint.String() tr.endpoint = s.endpoint.String()
} }
fmt.Println("tr")
tr.request = req.WithContext(transport.NewServerContext(ctx, tr)) tr.request = req.WithContext(transport.NewServerContext(ctx, tr))
next.ServeHTTP(w, tr.request) next.ServeHTTP(w, tr.request)
}) })

Loading…
Cancel
Save