You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 9 година
123456789101112131415161718192021222324252627282930313233343536373839
  1. package rex
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "path"
  7. "github.com/goanywhere/env"
  8. "github.com/goanywhere/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. }