package main import ( "biukop.com/sfm/loan" "encoding/json" "fmt" log "github.com/sirupsen/logrus" "net/http" "time" ) type apiV1Envelop struct { Version string Success bool Msg string TimeStamp string Body map[string]interface{} Session loan.Session } 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, } ret.Body = make(map[string]interface{}) 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 if config.Debug { 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 } func apiV1SendJson(result interface{}, w http.ResponseWriter, r *http.Request, ss *loan.Session) { out, e := json.Marshal(result) if e != nil { log.Warn("Cannot convert result to json ", result) } w.Header().Set("Content-Type", "text/json; charset=utf-8") apiV1AddTrackingCookie(w, r, ss) //always the last one to set cookies fmt.Fprint(w, string(out)) }