|
|
|
|
|
|
|
|
package main |
|
|
package main |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"biukop.com/sfm/loan" |
|
|
|
|
|
"encoding/json" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"net/http" |
|
|
"net/http" |
|
|
"net/http/httputil" |
|
|
"net/http/httputil" |
|
|
|
|
|
"strings" |
|
|
|
|
|
"time" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
const apiV1Prefix = "/api/v1/" |
|
|
const apiV1Prefix = "/api/v1/" |
|
|
|
|
|
|
|
|
//apiV1Main version 1 main entry for all wechat callbacks |
|
|
|
|
|
|
|
|
type apiV1HandlerMap struct { |
|
|
|
|
|
Method string |
|
|
|
|
|
Path string //regex |
|
|
|
|
|
Handler func(http.ResponseWriter, *http.Request) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type apiV1Envelop struct { |
|
|
|
|
|
Version string |
|
|
|
|
|
Success bool |
|
|
|
|
|
Msg string |
|
|
|
|
|
TimeStamp string |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type apiV1Response struct { |
|
|
|
|
|
apiV1Envelop |
|
|
|
|
|
Body interface{} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var apiV1Handler = []apiV1HandlerMap{ |
|
|
|
|
|
{"POST", "login", apiV1Login}, |
|
|
|
|
|
{"GET", "login", dummyHandler}, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//apiV1Main version 1 main entry for all REST API |
|
|
// |
|
|
// |
|
|
func apiV1Main(w http.ResponseWriter, r *http.Request) { |
|
|
func apiV1Main(w http.ResponseWriter, r *http.Request) { |
|
|
logRequestDebug(httputil.DumpRequest(r, true)) |
|
|
logRequestDebug(httputil.DumpRequest(r, true)) |
|
|
|
|
|
w.Header().Set("Content-Type", "application/json;charset=UTF-8") |
|
|
|
|
|
path := r.URL.Path[len(apiV1Prefix):] //strip API prefix |
|
|
|
|
|
|
|
|
|
|
|
for _, node := range apiV1Handler { |
|
|
|
|
|
if r.Method == node.Method && path == node.Path { |
|
|
|
|
|
node.Handler(w, r) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
//Catch for all |
|
|
|
|
|
apiV1Error(w, r) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func apiV1Login(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
p := loan.User{} |
|
|
|
|
|
p.FakeNew() |
|
|
|
|
|
|
|
|
|
|
|
res := apiV1Response{} |
|
|
|
|
|
res.apiV1Envelop = apiV1EnvelopBlank() |
|
|
|
|
|
res.Body = 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 { |
|
|
|
|
|
apiV1Error(w, nil) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func apiV1Error(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
dump := logRequestDebug(httputil.DumpRequest(r, true)) |
|
|
|
|
|
msg := fmt.Sprintf("Unhandled Protocol = %s path= %s", r.Method, r.URL.Path) |
|
|
|
|
|
dumpLines := strings.Split(dump, "\r\n") |
|
|
|
|
|
ar := apiV1ResponseBlank() |
|
|
|
|
|
ar.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 |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
path := r.URL.Path[len(apiV1Prefix):] |
|
|
|
|
|
fmt.Fprintf(w, "Protocol = %s, path= %s \n", r.Method, path) |
|
|
|
|
|
|
|
|
func apiV1ResponseBlank() (ret apiV1Response) { |
|
|
|
|
|
ret.apiV1Envelop = apiV1EnvelopBlank() |
|
|
|
|
|
return |
|
|
} |
|
|
} |