From 1018ff016f3699a0e573443fed5ad378c9c727be Mon Sep 17 00:00:00 2001 From: Yifu Yu Date: Mon, 28 Oct 2019 20:21:05 +0800 Subject: [PATCH] Added /getMapping API. --- main.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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) }