Added /getMapping API.

This commit is contained in:
Yifu Yu 2019-10-28 20:21:05 +08:00
parent 8f908cadd4
commit 1018ff016f
1 changed files with 12 additions and 1 deletions

13
main.go
View File

@ -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)
}