diff --git a/main.go b/main.go index f9479b9..2ea3dff 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ import ( ) const ( - errIPv4Only = "Navigator works for IPv4 only :)" + errIPv4Only = "Navigator works for valid IPv4 only :)" ) type errorMessage struct { @@ -123,6 +123,17 @@ func main() { server := mapping.Get(host) fmt.Fprint(resp, server) }) + http.HandleFunc("/getMapping", func(resp http.ResponseWriter, req *http.Request) { + ip := req.FormValue("ip") + if net.ParseIP(ip).To4() == nil { + responseWithError(resp, http.StatusBadRequest, errIPv4Only) + return + } + resp.Header().Set("Content-Type", "text/plain") + resp.WriteHeader(http.StatusOK) + server := mapping.Get(ip) + fmt.Fprint(resp, server) + }) ipgeo.Init() http.ListenAndServe("0.0.0.0:8086", nil) }