예제 #1
0
func (ctx *requestContext) getFromURL(key interface{}, rest ...string) interface{} {
	if len(rest) == 0 {
		return ctx.Request.URL
	}
	switch rest[0] {
	case "host":
		return ctx.Request.Host
	case "path":
		return ctx.Request.URL.Path
	case "query":
		if len(rest) == 0 {
			return ctx.Request.URL.Query()
		}
		return ctx.Request.URL.Query().Get(strings.Join(rest[1:], "."))
	case "params":
		if len(rest) == 0 {
			return xmux.Params(ctx.Context)
		}
		return xmux.Params(ctx.Context).Get(strings.Join(rest[1:], "."))
	default:
		return ctx.Context.Value(key)
	}
}
예제 #2
0
		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)
		})
	}
}