// ComponentMiddleware - Creates a middleware that sets given component in ctx. func ComponentMiddleware(c *components.Component) middlewares.Handler { return middlewares.ToHandler(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next xhandler.HandlerC) { if c != nil { ctx = components.NewContext(ctx, c) } next.ServeHTTPC(ctx, w, r) }) }
// NewUnmarshalFromRequest - Unmarshals component using `UnmarshalFromQuery` on `GET` // method and `` on `POST` method. func NewUnmarshalFromRequest() middlewares.Handler { get, post := UnmarshalFromQuery("GET"), UnmarshalFromBody("POST") return middlewares.ToHandler(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next xhandler.HandlerC) { if r.Method == "GET" { get(next).ServeHTTPC(ctx, w, r) } else if r.Method == "POST" { post(next).ServeHTTPC(ctx, w, r) } }) }
func init() { middlewares.Register(middlewares.Descriptor{ Name: "my_test_middleware", Options: []*options.Option{ {Name: "opts1", Type: options.TypeString}, }, }, func(o middlewares.Options) (middlewares.Handler, error) { return middlewares.ToHandler(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next xhandler.HandlerC) { ctx = components.WithTemplateKey(ctx, "some1", "test1") ctx = components.WithTemplateKey(ctx, "some2", "test2") next.ServeHTTPC(ctx, w, r) }), nil }) }
// UnmarshalFromBody - Unmarshals component from request bodyCompileInContext() on certain methods. // Stores result in context to be retrieved with `components.FromContext`. func UnmarshalFromBody(methods ...string) middlewares.Handler { return middlewares.ToHandler(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next xhandler.HandlerC) { if !helpers.Contain(methods, r.Method) { next.ServeHTTPC(ctx, w, r) return } c := new(components.Component) err := json.NewDecoder(r.Body).Decode(c) if err != nil { helpers.WriteError(w, r, http.StatusBadRequest, err.Error()) return } ctx = components.NewContext(ctx, c) next.ServeHTTPC(ctx, w, r) }) }
// UnmarshalFromQuery - Unmarshals component from `json` query on certain methods. // Stores result in context to be retrieved with `components.FromContext`. func UnmarshalFromQuery(methods ...string) middlewares.Handler { return middlewares.ToHandler(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next xhandler.HandlerC) { if len(methods) != 0 && !helpers.Contain(methods, r.Method) { next.ServeHTTPC(ctx, w, r) return } // Read component from request c, err := readComponent(r) if err != nil { helpers.WriteError(w, r, http.StatusBadRequest, err.Error()) return } // Create a context with component and move to next handler ctx = components.NewContext(ctx, c) next.ServeHTTPC(ctx, w, r) }) }
func defaultCtxSetter(o *webOptions) middlewares.Handler { return middlewares.ToHandler(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next xhandler.HandlerC) { ctx = components.NewTemplateContext(ctx, o.defaultCtx.Clone()) next.ServeHTTPC(ctx, w, r) }) }
middleware, err := middlewares.Construct(md) if err != nil { return nil, err } if tracing { middleware = tracingMiddleware(md, middleware) } opts = append(opts, WithMiddleware(middleware)) } return New(opts...), nil } var initMiddleware = middlewares.ToHandler(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next xhandler.HandlerC) { ctx = NewRequestContext(ctx, r) ctx = components.WithTemplateKey(ctx, "request", r) ctx = components.WithTemplateKey(ctx, "params", xmux.Params(ctx)) next.ServeHTTPC(ctx, w, r) }) // tracingMiddleware - Tracing for middlewares. func tracingMiddleware(md *middlewares.Middleware, handler middlewares.Handler) middlewares.Handler { return func(next xhandler.HandlerC) xhandler.HandlerC { h := handler(next) return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { tr, _ := trace.FromContext(ctx) tr.LazyPrintf("%s", md.Name) h.ServeHTTPC(ctx, w, r) }) } }