func (h Handle) ServeHTTP(w http.ResponseWriter, r *http.Request) { requestLog.Info("%v %v", r.Method, r.RequestURI) for _, initializer := range initializers { initializer(r) } buffer := new(httpbuf.Buffer) err := h(buffer, r) if err != nil { fmt.Println(err) if webErr, ok := err.(WebError); ok { http.Error(w, webErr.Message, webErr.Code) } else { http.Error(w, err.Error(), http.StatusInternalServerError) } } err = Session(r).Save(r, buffer) context.Clear(r) buffer.Apply(w) }
// ServeHTTP builds the context and passes onto the real handler func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { // Create the context ctx, err := NewContext(req) if err != nil { log.LogError("Failed to create context: %v", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } defer ctx.Close() // Run the handler, grab the error, and report it buf := new(httpbuf.Buffer) log.LogTrace("Web: %v %v %v %v", req.RemoteAddr, req.Proto, req.Method, req.RequestURI) err = h(buf, req, ctx) if err != nil { log.LogError("Error handling %v: %v", req.RequestURI, err) http.Error(w, err.Error(), http.StatusInternalServerError) return } // Save the session if err = ctx.Session.Save(req, buf); err != nil { log.LogError("Failed to save session: %v", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } // Apply the buffered response to the writer buf.Apply(w) }
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx, err := models.NewContext(r, store, dbmap, cache) if err != nil { fmt.Println("Call to NewContext() returned error.") log.Println(err) } if ctx.User != nil { activityCache[ctx.User.ID] = time.Now() fmt.Println("Hashed new user ID in ServeHTTP()") log.Println(ctx.User.ID) } defer ctx.Close() buffer := new(httpbuf.Buffer) err = h(buffer, r, ctx) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err = ctx.Session.Save(r, buffer); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } buffer.Apply(w) }
/* Middleware that buffers all http output. This permits output to be written before headers are sent. Downside: no output is sent until it's all ready to be sent, so this breaks streaming. Note: currently ignores errors */ func Buffer() func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { bw := new(httpbuf.Buffer) next.ServeHTTP(bw, r) bw.Apply(w) }) } }
func (h LoggedHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { if dump, err := httputil.DumpRequest(req, true); err != nil { logrus.Errorf("Failed to log request:\n%s", err) } else { logrus.Info(string(dump)) } buf := new(httpbuf.Buffer) h.Handler.ServeHTTP(buf, req) logrus.Infof("response: %s", buf.String()) buf.Apply(w) }
func (a WebAction) ServeHTTP(w http.ResponseWriter, r *http.Request) { buf := new(httpbuf.Buffer) ctx := &WebContext{ Session: context.Get(r, "session").(*sessions.Session), User: context.Get(r, "user").(*domain.User), CSRF: context.Get(r, "csrf").(string), } err := a(buf, r, ctx) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } buf.Apply(w) }
func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { //create the context ctx, err := NewContext(req) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } //run the handler and grab the error, and report it buf := new(httpbuf.Buffer) err = h(buf, req, ctx) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } //save the session if err = ctx.Session.Save(req, buf); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } //apply the buffered response to the writer buf.Apply(w) }
// ServeHTTP mandatory middleware part func (a *Session) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { session, err := a.session.Get(r, "_session") if err != nil { http.SetCookie(rw, &http.Cookie{Name: "_session", Expires: time.Now().Add(-1 * time.Hour)}) http.Error(rw, err.Error(), http.StatusBadRequest) return } log.Print("zero ", session.Values) session.Values["time"] = time.Now().String() context.Set(r, "session", session) buf := new(httpbuf.Buffer) next(buf, r) session.Save(r, rw) buf.Apply(rw) // res := rw.(ResponseWriter) }
func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { w.Header().Set("Expires", "Fri, 02 Oct 1998 20:00:00 GMT") w.Header().Set("Pragma", "no-cache") w.Header().Set("Cache-Control", "no-store, no-cache, max-age=0, must-revalidate") ctx, err := NewContext(req) if err != nil { raven.CaptureError(err, nil) http.Error(w, err.Error(), http.StatusInternalServerError) return } defer ctx.Close() //run the handler and grab the error, and report it buf := new(httpbuf.Buffer) err = h(buf, req, ctx) if err != nil { raven.CaptureError(err, nil) log.Printf("call handler error: %s", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } ctx.afterHandle() //save the session if len(ctx.Session.Values) > 0 { // session not empty only if err = ctx.Session.Save(req, buf); err != nil { log.Printf("session.save error: %s", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } } //apply the buffered response to the writer buf.Apply(w) }