| 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 | |||||
| } |
| 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 | |||||
| } |
| package main | package main | ||||
| import ( | import ( | ||||
| "biukop.com/sfm/loan" | |||||
| "encoding/json" | |||||
| "fmt" | |||||
| "net/http" | "net/http" | ||||
| "net/http/httputil" | "net/http/httputil" | ||||
| "strings" | |||||
| "time" | |||||
| ) | ) | ||||
| const apiV1Prefix = "/api/v1/" | const apiV1Prefix = "/api/v1/" | ||||
| Handler func(http.ResponseWriter, *http.Request) | 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{ | var apiV1Handler = []apiV1HandlerMap{ | ||||
| {"POST", "login", apiV1Login}, | {"POST", "login", apiV1Login}, | ||||
| {"GET", "login", apiV1DumpRequest}, | {"GET", "login", apiV1DumpRequest}, | ||||
| apiV1DumpRequest(w, r) | 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 | |||||
| } |
| import ( | import ( | ||||
| "biukop.com/sfm/loan" | "biukop.com/sfm/loan" | ||||
| "encoding/json" | |||||
| "fmt" | "fmt" | ||||
| log "github.com/sirupsen/logrus" | log "github.com/sirupsen/logrus" | ||||
| "net/http" | "net/http" | ||||
| "net/http/httputil" | |||||
| "strings" | |||||
| ) | ) | ||||
| type httpEntry func(http.ResponseWriter, *http.Request) | type httpEntry func(http.ResponseWriter, *http.Request) | ||||
| p.FakeNew() | p.FakeNew() | ||||
| fmt.Fprintf(w, "Hello, there %s, %+v\n", loan.Version, p) | 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) | |||||
| } |