No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

78 líneas
1.6KB

  1. package main
  2. import (
  3. "biukop.com/sfm/loan"
  4. "encoding/json"
  5. "fmt"
  6. log "github.com/sirupsen/logrus"
  7. "net/http"
  8. "time"
  9. )
  10. type apiV1Envelop struct {
  11. Version string
  12. Success bool
  13. Msg string
  14. TimeStamp string
  15. Body map[string]interface{}
  16. Session loan.Session
  17. }
  18. type apiV1Response struct {
  19. Env apiV1Envelop `json:"_"`
  20. tmp map[string]interface{}
  21. }
  22. func apiV1EnvelopBlank() (ret apiV1Envelop) {
  23. ts := time.Now().Format("2006-01-02 15:04:05")
  24. ret = apiV1Envelop{
  25. Version: loan.Version,
  26. Success: true,
  27. Msg: "",
  28. TimeStamp: ts,
  29. }
  30. ret.Body = make(map[string]interface{})
  31. return
  32. }
  33. func apiV1ResponseBlank() (ret apiV1Response) {
  34. ret.Env = apiV1EnvelopBlank()
  35. ret.tmp = make(map[string]interface{})
  36. return
  37. }
  38. func (m *apiV1Response) add(key string, value interface{}) {
  39. m.tmp[key] = value
  40. }
  41. func (m *apiV1Response) toJson() (ret []byte, e error) {
  42. tempMap := m.tmp
  43. m.tmp = nil
  44. tempMap["_"] = m.Env
  45. ret, e = json.Marshal(tempMap)
  46. return
  47. }
  48. func (m *apiV1Response) sendJson(w http.ResponseWriter) (ret []byte, e error) {
  49. tempMap := m.tmp
  50. m.tmp = nil
  51. if config.Debug {
  52. tempMap["_"] = m.Env
  53. }
  54. ret, e = json.Marshal(tempMap)
  55. //sent
  56. w.Header().Set("Content-Type", "text/json; charset=utf-8")
  57. fmt.Fprint(w, string(ret))
  58. return
  59. }
  60. func apiV1SendJson(result interface{}, w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  61. out, e := json.Marshal(result)
  62. if e != nil {
  63. log.Warn("Cannot convert result to json ", result)
  64. }
  65. w.Header().Set("Content-Type", "text/json; charset=utf-8")
  66. apiV1AddTrackingCookie(w, r, ss) //always the last one to set cookies
  67. fmt.Fprint(w, string(out))
  68. }