| @@ -0,0 +1,62 @@ | |||
| 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 | |||
| } | |||
| @@ -0,0 +1,53 @@ | |||
| package main | |||
| import ( | |||
| log "github.com/sirupsen/logrus" | |||
| "net/http" | |||
| "time" | |||
| ) | |||
| type login struct { | |||
| user string | |||
| pass string | |||
| buser string //admin | |||
| bpass string //always_correct_md5 => YWRtaW46YWx3YXlzX2NvcnJlY3RfbWQ1 | |||
| token string | |||
| ts time.Time | |||
| } | |||
| func apiV1Login(w http.ResponseWriter, r *http.Request) { | |||
| res := apiV1ResponseBlank() | |||
| l := login{} | |||
| e := l.initRequest(r) | |||
| apiV1ErrorCheck(e) | |||
| res.add("user", l.user) | |||
| res.add("pass", l.pass) | |||
| res.add("buser", l.buser) | |||
| res.add("bpass", l.bpass) | |||
| bs := r.Header.Get("Biukop-Session") | |||
| bst := r.Header.Get("Biukop-Session-Token") | |||
| bse := r.Header.Get("Biukop-Session-Expire") | |||
| res.add("bs", bs) | |||
| res.add("bse", bse) | |||
| res.add("bst", bst) | |||
| res.sendJson(w) | |||
| } | |||
| func (m *login) initRequest(r *http.Request) (e error) { | |||
| e = r.ParseForm() | |||
| if e != nil { | |||
| log.Error(e) | |||
| return | |||
| } | |||
| m.user = r.PostForm.Get("u") | |||
| m.pass = r.PostForm.Get("p") | |||
| m.buser, m.bpass, _ = r.BasicAuth() | |||
| return | |||
| } | |||
| @@ -1,13 +1,8 @@ | |||
| package main | |||
| import ( | |||
| "biukop.com/sfm/loan" | |||
| "encoding/json" | |||
| "fmt" | |||
| "net/http" | |||
| "net/http/httputil" | |||
| "strings" | |||
| "time" | |||
| ) | |||
| const apiV1Prefix = "/api/v1/" | |||
| @@ -18,20 +13,6 @@ type apiV1HandlerMap struct { | |||
| Handler func(http.ResponseWriter, *http.Request) | |||
| } | |||
| type apiV1Envelop struct { | |||
| Version string | |||
| Success bool | |||
| Msg string | |||
| TimeStamp string | |||
| } | |||
| type apiV1Response struct { | |||
| Env apiV1Envelop | |||
| Body interface{} | |||
| Ext interface{} | |||
| Map map[string]interface{} | |||
| } | |||
| var apiV1Handler = []apiV1HandlerMap{ | |||
| {"POST", "login", apiV1Login}, | |||
| {"GET", "login", apiV1DumpRequest}, | |||
| @@ -54,52 +35,8 @@ func apiV1Main(w http.ResponseWriter, r *http.Request) { | |||
| apiV1DumpRequest(w, r) | |||
| } | |||
| func apiV1Login(w http.ResponseWriter, r *http.Request) { | |||
| p := loan.User{} | |||
| p.FakeNew() | |||
| res := apiV1ResponseBlank() | |||
| res.Body = p | |||
| res.Map["some"] = p | |||
| apiSendJson(res, w) | |||
| } | |||
| func apiSendJson(p interface{}, w http.ResponseWriter) { | |||
| b, e := json.Marshal(p) | |||
| w.Header().Set("Content-Type", "text/json; charset=utf-8") | |||
| if e == nil { | |||
| fmt.Fprint(w, string(b)) | |||
| } else { | |||
| apiV1DumpRequest(w, nil) | |||
| func apiV1ErrorCheck(e error) { | |||
| if nil != e { | |||
| panic(e.Error()) | |||
| } | |||
| } | |||
| func apiV1DumpRequest(w http.ResponseWriter, r *http.Request) { | |||
| dump := logRequestDebug(httputil.DumpRequest(r, true)) | |||
| dump = strings.TrimSpace(dump) | |||
| msg := fmt.Sprintf("Unhandled Protocol = %s path= %s", r.Method, r.URL.Path) | |||
| dumpLines := strings.Split(dump, "\r\n") | |||
| ar := apiV1ResponseBlank() | |||
| ar.Env.Msg = msg | |||
| ar.Body = dumpLines | |||
| b, _ := json.Marshal(ar) | |||
| fmt.Fprintf(w, "%s\n", b) | |||
| } | |||
| 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.Map = make(map[string]interface{}) | |||
| return | |||
| } | |||
| @@ -2,9 +2,12 @@ package main | |||
| import ( | |||
| "biukop.com/sfm/loan" | |||
| "encoding/json" | |||
| "fmt" | |||
| log "github.com/sirupsen/logrus" | |||
| "net/http" | |||
| "net/http/httputil" | |||
| "strings" | |||
| ) | |||
| type httpEntry func(http.ResponseWriter, *http.Request) | |||
| @@ -54,3 +57,25 @@ func dummyHandler(w http.ResponseWriter, r *http.Request) { | |||
| p.FakeNew() | |||
| fmt.Fprintf(w, "Hello, there %s, %+v\n", loan.Version, p) | |||
| } | |||
| func apiSendJson(p interface{}, w http.ResponseWriter) { | |||
| b, e := json.Marshal(p) | |||
| w.Header().Set("Content-Type", "text/json; charset=utf-8") | |||
| if e == nil { | |||
| fmt.Fprint(w, string(b)) | |||
| } else { | |||
| apiV1DumpRequest(w, nil) | |||
| } | |||
| } | |||
| func apiV1DumpRequest(w http.ResponseWriter, r *http.Request) { | |||
| dump := logRequestDebug(httputil.DumpRequest(r, true)) | |||
| dump = strings.TrimSpace(dump) | |||
| msg := fmt.Sprintf("Unhandled Protocol = %s path= %s", r.Method, r.URL.Path) | |||
| dumpLines := strings.Split(dump, "\r\n") | |||
| ar := apiV1ResponseBlank() | |||
| ar.Env.Msg = msg | |||
| ar.add("Body", dumpLines) | |||
| b, _ := ar.toJson() | |||
| fmt.Fprintf(w, "%s\n", b) | |||
| } | |||