Ejemplo n.º 1
0
func routeTracing(route Route, handler xhandler.HandlerC) xhandler.HandlerC {
	rs := route.String()
	return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		tr := trace.New(rs, fmt.Sprintf("%s %s", r.Method, r.URL.Path))
		ctx = trace.NewContext(ctx, tr)
		handler.ServeHTTPC(ctx, w, r)
		tr.Finish()
	})
}
Ejemplo n.º 2
0
// CompileInContext - Compiles component from context.
// Stores result in context to be retrieved with `components.FromContext`.
func CompileInContext(next xhandler.HandlerC) xhandler.HandlerC {
	return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		compiled, err := compiler.Compile(ctx)
		if err != nil {
			helpers.WriteError(w, r, http.StatusExpectationFailed, fmt.Sprintf("compile error: %v", err))
			return
		}
		ctx = components.NewCompiledContext(ctx, compiled)
		next.ServeHTTPC(ctx, w, r)
	})
}
Ejemplo n.º 3
0
// RenderInContext - Renders compiled component from context.
// Stores result in context to be retrieved with `components.ContextRendered`.
func RenderInContext(next xhandler.HandlerC) xhandler.HandlerC {
	return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		c, ok := components.CompiledFromContext(ctx)
		if !ok {
			helpers.WriteError(w, r, http.StatusBadRequest, "component not compiled")
			return
		}
		t, _ := components.TemplateContext(ctx)
		res, err := components.Render(c, t)
		if err != nil {
			helpers.WriteError(w, r, http.StatusExpectationFailed, fmt.Sprintf("render error: %v", err))
			return
		}
		ctx = components.NewRenderedContext(ctx, res)
		next.ServeHTTPC(ctx, w, r)
	})
}
Ejemplo n.º 4
0
// HandlerC is net/context aware handler
func (c *Cors) HandlerC(h xhandler.HandlerC) xhandler.HandlerC {
	return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		if r.Method == "OPTIONS" {
			c.logf("Handler: Preflight request")
			c.handlePreflight(w, r)
			// Preflight requests are standalone and should stop the chain as some other
			// middleware may not handle OPTIONS requests correctly. One typical example
			// is authentication middleware ; OPTIONS requests won't carry authentication
			// headers (see #1)
			if c.optionPassthrough {
				h.ServeHTTPC(ctx, w, r)
			}
		} else {
			c.logf("Handler: Actual request")
			c.handleActualRequest(w, r)
			h.ServeHTTPC(ctx, w, r)
		}
	})
}
Ejemplo n.º 5
0
Archivo: main.go Proyecto: blang/posty
// handler transformation xhandler.HandlerC -> web.Handler
func handle(ctx context.Context, handlerc xhandler.HandlerC) web.Handler {
	return web.HandlerFunc(func(c web.C, w http.ResponseWriter, r *http.Request) {
		newctx := context.WithValue(ctx, "urlparams", c.URLParams)
		handlerc.ServeHTTPC(newctx, w, r)
	})
}