in game/go-server-gate/cmd/server.go [88:145]
func startHttp() {
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
res, err := pkg.Login(context.TODO(), r.URL.Query().Get("name"))
if err != nil {
responseWithOrigin(w, r, 200, []byte(err.Error()))
return
}
b, err := json.Marshal(res)
if err != nil {
responseWithOrigin(w, r, 200, []byte(err.Error()))
return
}
responseWithOrigin(w, r, 200, b)
})
http.HandleFunc("/score", func(w http.ResponseWriter, r *http.Request) {
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
logger.Error(err.Error())
}
var info pojo.Info
err = json.Unmarshal(reqBody, &info)
if err != nil {
responseWithOrigin(w, r, 500, []byte(err.Error()))
return
}
res, err := pkg.Score(context.TODO(), info.Name, strconv.Itoa(info.Score))
if err != nil {
responseWithOrigin(w, r, 200, []byte(err.Error()))
return
}
b, err := json.Marshal(res)
if err != nil {
responseWithOrigin(w, r, 200, []byte(err.Error()))
return
}
responseWithOrigin(w, r, 200, b)
})
http.HandleFunc("/rank", func(w http.ResponseWriter, r *http.Request) {
res, err := pkg.Rank(context.TODO(), r.URL.Query().Get("name"))
if err != nil {
responseWithOrigin(w, r, 200, []byte(err.Error()))
return
}
b, err := json.Marshal(res)
if err != nil {
responseWithOrigin(w, r, 200, []byte(err.Error()))
return
}
responseWithOrigin(w, r, 200, b)
})
_ = http.ListenAndServe("127.0.0.1:8089", nil)
}