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.

67 lines
1.2KB

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