navigator/api/beacon/v1/api.go

34 lines
804 B
Go

package v1
import (
"encoding/json"
"git.eve.moe/jackyyf/navigator/api/beacon"
"git.eve.moe/jackyyf/navigator/mapping"
"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)
jsonEncoder := json.NewEncoder(resp)
jsonEncoder.Encode(map[string]interface{}{
"nodes": nodes,
"suffix": suffix,
})
})
beacon.RegisterApi("v1", serveMux)
}