예제 #1
0
func (s store) get(r *http.Request) (se *sessions.Session, e error) {
	log.TraceR(r, "getting session", nil)
	se, e = s.CookieStore.Get(r, s.name)
	if e != nil {
		log.ErrorR(r, e, nil)
		return
	}
	log.TraceR(r, "got session", log.Data{"session": se})
	return se, e
}
예제 #2
0
// Vtom converts validator.v5 errors to a map[string]interface{}
func Vtom(req *http.Request, errs *validator.StructErrors) func(field string) map[string]interface{} {
	log.TraceR(req, "flattening errors", log.Data{"errors": errs})
	ef := errs.Flatten()
	return func(field string) map[string]interface{} {
		if e, ok := ef[field]; ok {
			log.TraceR(req, "got form error", log.Data{"field": field, "tag": e.Tag})
			return map[string]interface{}{e.Tag: e}
		}
		log.TraceR(req, "error not found in map", log.Data{"field": field})
		return nil
	}
}
예제 #3
0
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	done := make(chan bool, 1)
	tw := &writer{w: w}
	go func() {
		h.handler.ServeHTTP(tw, r)
		done <- true
	}()
	select {
	case <-done:
		return
	case <-h.timeout():
		tw.mu.Lock()
		defer tw.mu.Unlock()
		log.TraceR(r, "request timed out", nil)
		if !tw.wroteHeader {
			log.TraceR(r, "headers not written, calling failure handler", nil)
			h.failHandler.ServeHTTP(w, r)
		}
		tw.timedOut = true
	}
}
예제 #4
0
// DefaultVars adds the default vars (User and Session) to the data map
func (r Renderer) DefaultVars(req *http.Request, m Vars) map[string]interface{} {
	if m == nil {
		log.TraceR(req, "creating template data map", nil)
		m = make(map[string]interface{})
	}

	s, _ := session.Get(req)
	if s == nil {
		log.TraceR(req, "session not found", nil)
		return m
	}

	m["Config"] = r.config.Config()

	log.TraceR(req, "adding session to template data map", nil)
	m["Session"] = s

	if u, ok := s.Values["User"]; ok {
		log.TraceR(req, "adding user to template data map", nil)
		m["User"] = u
	}

	return m
}
예제 #5
0
// Ftos converts form data into a model
func Ftos(req *http.Request, model interface{}) error {
	log.TraceR(req, "parsing request form", nil)
	err := req.ParseForm()

	if err != nil {
		log.ErrorR(req, err, nil)
		return err
	}

	err = decoder.Decode(model, req.PostForm)
	if err != nil {
		log.ErrorR(req, err, nil)
	}

	return err
}
예제 #6
0
// DefaultHandler returns a Handler with a default timeout
func DefaultHandler(h http.Handler) http.Handler {
	return Handler(h, 1*time.Second, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		log.TraceR(req, "timed out", nil)
		w.WriteHeader(http.StatusRequestTimeout)
	}))
}
예제 #7
0
// Form creates a htmlform.Form from a model and http.Request
func Form(req *http.Request, model interface{}, errs *validator.StructErrors) htmlform.Form {
	log.TraceR(req, "creating form from model", log.Data{"model": model, "errors": errs})
	return htmlform.Create(model, Vtom(req, errs), []string{}, []string{}).WithCSRF(nosurf.FormFieldName, nosurf.Token(req))
}