func GenJsonpResult(r *http.Request, rt *Ret) []byte { bs, err := json.Marshal(rt) if err != nil { panic(err) } return []byte(sw.GetParameter(r, "callback") + "(" + string(bs) + ")") }
func DlmHandler(rw http.ResponseWriter, r *http.Request, p httprouter.Params) { db := ConnectDB("./middle.db") defer func() { db.Close() err := recover() if err != nil { rw.Write(GenJsonpResult(r, &Ret{false, err.(string), nil})) log.Println(err) } }() LogClient(r.RemoteAddr, db) // print query string log.Println(r.URL.RawQuery) switcher := sw.Dispatch(db) var ret []byte if Authorize(r) { msg, data := switcher[sw.GetParameter(r, "cmd")](r) ret = GenJsonpResult(r, &Ret{true, msg, data}) } else { panic("Not authorized!") } rw.Write(ret) }