func ExecTemplate(c *TemplateConfig) error { templatesMutex.Lock() defer templatesMutex.Unlock() cname := "" for i, name := range c.Names { c.Names[i] = filepath.Join(c.Dir, name+".html") cname += name } t, ok := templatesCache[cname] if !ok || appengine.IsDevAppServer() { var err error t = template.New(cname).Delims(c.LeftDelim, c.RightDelim) t, err = t.Funcs(templatesFuncs).ParseFiles(c.Names...) if err != nil { return errors.New(err) } templatesCache[cname] = t } if err := t.ExecuteTemplate(c.W, "base", c.Data); err != nil { return errors.New(err) } return nil }
// Load the request data using gorilla schema into a struct func (r *Request) LoadData(data interface{}) error { if err := r.Req.ParseForm(); err != nil { return errors.New(err) } if err := schemaDecoder.Decode(data, r.Req.Form); err != nil { e, ok := err.(schema.MultiError) if ok { // Delete the invalid path errors for k, v := range e { if strings.Contains(v.Error(), "schema: invalid path") { delete(e, k) } } // Return directly if there are no other kind of errors if len(e) == 0 { return nil } } // Not a MultiError, log it if err != nil { return errors.New(err) } } return nil }
// Validate the form. // Returns a boolean indicating if the data was valid according // to the validations defined on f. It returns an error too. func Validate(r *http.Request, f Form) (bool, error) { // Copy the body to a buffer so we can use it twice buf := new(bytes.Buffer) if _, err := buf.ReadFrom(r.Body); err != nil { return false, errors.New(err) } nbuf := ioutil.NopCloser(bytes.NewBuffer(buf.Bytes())) r.Body = nbuf m := make(map[string]interface{}) if err := json.NewDecoder(buf).Decode(&m); err != nil { return false, errors.New(err) } fields := f.Fields() validations := f.Validations() for _, field := range fields { id := getId(field) if id == "" { continue } // Skip fields without validation constrainst if _, ok := validations[id]; !ok { continue } value := normalizeValue(id, m) for _, val := range validations[id] { if !val.Func(value) { return false, nil } } f.SetValue(id, value) } if err := json.NewDecoder(r.Body).Decode(f); err != nil { return false, errors.New(err) } return true, nil }
func (r *Request) EmitJson(data interface{}) error { // XSSI protection fmt.Fprintln(r.W, ")]}',") // Encode the output if err := json.NewEncoder(r.W).Encode(data); err != nil { return errors.New(err) } return nil }
func (r *Request) LoadJsonData(data interface{}) error { if err := json.NewDecoder(r.Req.Body).Decode(data); err != nil { if err == io.EOF { return nil } return errors.New(err) } return nil }
// Send a mail using the SendGrid API func SendMail(c appengine.Context, mail *Mail) error { client := &http.Client{ Transport: &urlfetch.Transport{ Context: c, Deadline: time.Duration(20) * time.Second, }, } // Build the data needed for the API call data := url.Values{ "api_user": []string{conf.MAIL_API_USER}, "api_key": []string{conf.MAIL_API_KEY}, "to": []string{mail.To}, "toname": []string{mail.ToName}, "subject": []string{mail.Subject}, "html": []string{mail.Html}, "from": []string{mail.From}, "fromname": []string{mail.FromName}, } // Request the SendGrid API resp, err := client.PostForm(conf.MAIL_SEND_API, data) if err != nil { return errors.New(err) } defer resp.Body.Close() // Decode the Mail API response var r mailAPI if err := json.NewDecoder(resp.Body).Decode(&r); err != nil { return errors.New(err) } // Test for errors in the api call if r.Message != "success" { return errors.Format("cannot send the mail: api %s message: %v", r.Message, r.Errors) } return nil }
func (r *Request) processError(err error) { e := errors.New(err).(*errors.Error) LogError(r.C, e) h, ok := errorHandlers[e.Code] if ok { if err := h(r); err == nil { return } } http.Error(r.W, "", e.Code) }
func LogError(c appengine.Context, err error) { e := errors.New(err).(*errors.Error) c.Errorf("%s", e.Error()) sendErrorByEmail(c, e.Error()) }
func (r *Request) JsonResponse(data interface{}) error { if err := json.NewEncoder(r.W).Encode(data); err != nil { return errors.New(err) } return nil }