Refactored API.
This commit is contained in:
@ -8,6 +8,7 @@ go_library(
|
||||
deps = [
|
||||
"//api/beacon:go_default_library",
|
||||
"//mapping:go_default_library",
|
||||
"//mapping/firefly:go_default_library",
|
||||
"//utils:go_default_library",
|
||||
],
|
||||
)
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"git.eve.moe/jackyyf/navigator/api/beacon"
|
||||
"git.eve.moe/jackyyf/navigator/mapping"
|
||||
"git.eve.moe/jackyyf/navigator/mapping/firefly"
|
||||
"git.eve.moe/jackyyf/navigator/utils"
|
||||
"net/http"
|
||||
)
|
||||
@ -23,11 +24,25 @@ func init() {
|
||||
suffix := mapping.GetSuffix(host)
|
||||
resp.Header().Set("Content-Type", "application/json")
|
||||
resp.WriteHeader(http.StatusOK)
|
||||
jsonEncoder := json.NewEncoder(resp)
|
||||
jsonEncoder.Encode(map[string]interface{}{
|
||||
json.NewEncoder(resp).Encode(map[string]interface{}{
|
||||
"nodes": nodes,
|
||||
"suffix": suffix,
|
||||
})
|
||||
})
|
||||
serveMux.HandleFunc("/submit", func(resp http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != "POST" {
|
||||
utils.ResponseWithJsonError(resp, http.StatusMethodNotAllowed, "Method is not accepted with this method.")
|
||||
return
|
||||
}
|
||||
request := make(map[string]float64)
|
||||
decoder := json.NewDecoder(req.Body)
|
||||
if err := decoder.Decode(&request); err != nil {
|
||||
utils.ResponseWithJsonError(resp, http.StatusBadRequest, "Malformed JSON payload.")
|
||||
return
|
||||
}
|
||||
remoteIp := utils.GetRemoteIP(req)
|
||||
go firefly.AddSpeedTestResult(remoteIp, request)
|
||||
resp.WriteHeader(http.StatusOK)
|
||||
})
|
||||
beacon.RegisterApi("v1", serveMux)
|
||||
}
|
||||
|
Reference in New Issue
Block a user