|
- package main
-
- import (
- "biukop.com/sfm/loan"
- "encoding/json"
- "fmt"
- "net/http"
- "time"
- )
-
- type apiV1Envelop struct {
- Version string
- Success bool
- Msg string
- TimeStamp string
- Body interface{}
- }
-
- type apiV1Response struct {
- Env apiV1Envelop `json:"_"`
- tmp map[string]interface{}
- }
-
- func apiV1EnvelopBlank() (ret apiV1Envelop) {
- ts := time.Now().Format("2006-01-02 15:04:05")
- ret = apiV1Envelop{
- Version: loan.Version,
- Success: true,
- Msg: "",
- TimeStamp: ts,
- }
- return
- }
-
- func apiV1ResponseBlank() (ret apiV1Response) {
- ret.Env = apiV1EnvelopBlank()
- ret.tmp = make(map[string]interface{})
- return
- }
-
- func (m *apiV1Response) add(key string, value interface{}) {
- m.tmp[key] = value
- }
-
- func (m *apiV1Response) toJson() (ret []byte, e error) {
- tempMap := m.tmp
- m.tmp = nil
- tempMap["_"] = m.Env
- ret, e = json.Marshal(tempMap)
- return
- }
-
- func (m *apiV1Response) sendJson(w http.ResponseWriter) (ret []byte, e error) {
- tempMap := m.tmp
- m.tmp = nil
- tempMap["_"] = m.Env
- ret, e = json.Marshal(tempMap)
- //sent
- w.Header().Set("Content-Type", "text/json; charset=utf-8")
- fmt.Fprint(w, string(ret))
- return
- }
|