您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

40 行
879B

  1. package rex
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "path"
  7. "git.thevis.us/skepto/env"
  8. "git.thevis.us/skepto/fs"
  9. )
  10. // Shortcut for string based map.
  11. type M map[string]interface{}
  12. // Sends the HTTP response in JSON.
  13. func Send(w http.ResponseWriter, v interface{}) {
  14. var buffer = new(bytes.Buffer)
  15. defer func() {
  16. buffer.Reset()
  17. }()
  18. w.Header().Set("Content-type", "application/json; charset=utf-8")
  19. if err := json.NewEncoder(buffer).Encode(v); err == nil {
  20. w.Write(buffer.Bytes())
  21. } else {
  22. http.Error(w, err.Error(), http.StatusInternalServerError)
  23. }
  24. }
  25. // Renders a view (Pongo) and sends the rendered HTML string to the client.
  26. // Optional parameters: value, local variables for the view.
  27. // func Render(filename string, v ...interface{}) {}
  28. func init() {
  29. var basedir = fs.Getcd(2)
  30. env.Set("basedir", basedir)
  31. env.Load(path.Join(basedir, ".env"))
  32. }