// 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) }) }
// WriteRenderedHTML - Writes rendered component from context to response writer. func WriteRenderedHTML(ctx context.Context, w http.ResponseWriter, r *http.Request) { res, ok := components.RenderedFromContext(ctx) if !ok { helpers.WriteError(w, r, http.StatusBadRequest, "component not rendered") return } w.Header().Set("Content-Type", "text/html") w.Write([]byte(res.HTML())) }
// WriteRenderedJSON - Writes rendered component from context to response writer. func WriteRenderedJSON(ctx context.Context, w http.ResponseWriter, r *http.Request) { res, ok := components.RenderedFromContext(ctx) if !ok { helpers.WriteError(w, r, http.StatusBadRequest, "component not rendered") return } w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(res); err != nil { glog.Warningf("[api] response encode error: %v", err) } }
// 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) }) }
// 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) }) }