李伟乐 2 years ago
parent 26dd3cca29
commit 387d1b1dea
  1. 28
      example/main.go
  2. 4
      transport/http/context.go
  3. 2
      transport/http/server.go

@ -5,6 +5,8 @@ import (
"flag"
"fmt"
"git.diulo.com/mogfee/kit"
"git.diulo.com/mogfee/kit/middleware"
"git.diulo.com/mogfee/kit/transport"
"git.diulo.com/mogfee/kit/transport/http"
http2 "net/http"
)
@ -35,28 +37,40 @@ func main() {
}
func runApp(host string) {
hs := http.NewServer(
http.Address(host),
//http.Middleware(
//logging.Server(),
//validate.Server(),
//jwt.JWT(),
//),
http.Filter(func(handler http2.Handler) http2.Handler {
return &cros{
h: handler,
//http.Filter(func(handler http2.Handler) http2.Handler {
// return &cros{
// h: handler,
// }
//}),
http.Middleware(func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, a any) (any, error) {
if tr, ok := transport.FromServerContext(ctx); ok {
header := tr.ReplyHeader()
header.Set("Access-Control-Allow-Origin", "*")
header.Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,PATCH,DELETE")
header.Set("Access-Control-Allow-Credentials", "true")
header.Set("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,Access-Control-Allow-Credentials,User-Agent,Content-Length,Authorization")
}
return handler(ctx, a)
}
}),
)
route := hs.Route("/")
route.HEAD("/api/v1/answer/listCategory", func(ctx http.Context) error {
route.GET("/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) {
//tr, _ := transport.FromServerContext(ctx)
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

@ -158,10 +158,6 @@ 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)
}

@ -300,7 +300,7 @@ func (s *Server) filter() mux.MiddlewareFunc {
operation: pathTemplate,
pathTemplate: pathTemplate,
reqHeader: headerCarrier(req.Header),
replyHeader: headerCarrier(req.Header),
replyHeader: headerCarrier(w.Header()),
request: req,
}
if s.endpoint != nil {

Loading…
Cancel
Save