package v1 import ( "encoding/json" "fmt" "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" ) var ( serveMux = http.NewServeMux() ) func init() { serveMux.HandleFunc("/getNodes", func(resp http.ResponseWriter, req *http.Request) { host := utils.GetRemoteIP(req) nodes := mapping.GetNodes() if nodes == nil { utils.ResponseWithJsonError(resp, http.StatusInternalServerError, "Unable to get nodes") return } suffix := mapping.GetSuffix(host) resp.Header().Set("Content-Type", "application/json") resp.WriteHeader(http.StatusOK) 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.Header().Set("Content-Type", "application/json") resp.WriteHeader(http.StatusOK) fmt.Fprint(resp, "true") }) beacon.RegisterApi("v1", serveMux) }