Add support for city lookup

This commit is contained in:
Martin Polden
2016-04-17 11:28:47 +02:00
parent 8027c55fdf
commit 8f71576357
7 changed files with 77 additions and 11 deletions

View File

@ -32,6 +32,7 @@ type API struct {
type Response struct {
IP net.IP `json:"ip"`
Country string `json:"country,omitempty"`
City string `json:"city,omitempty"`
Hostname string `json:"hostname,omitempty"`
}
@ -73,6 +74,10 @@ func (a *API) newResponse(r *http.Request) (Response, error) {
if err != nil {
log.Print(err)
}
city, err := a.oracle.LookupCity(ip)
if err != nil {
log.Print(err)
}
hostnames, err := a.oracle.LookupAddr(ip.String())
if err != nil {
log.Print(err)
@ -80,6 +85,7 @@ func (a *API) newResponse(r *http.Request) (Response, error) {
return Response{
IP: ip,
Country: country,
City: city,
Hostname: strings.Join(hostnames, " "),
}, nil
}
@ -102,6 +108,15 @@ func (a *API) CLICountryHandler(w http.ResponseWriter, r *http.Request) *appErro
return nil
}
func (a *API) CLICityHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := a.newResponse(r)
if err != nil {
return internalServerError(err)
}
io.WriteString(w, response.City+"\n")
return nil
}
func (a *API) JSONHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := a.newResponse(r)
if err != nil {
@ -223,6 +238,7 @@ func (a *API) Handlers() http.Handler {
r.Handle("/", appHandler(a.CLIHandler)).Methods("GET").MatcherFunc(cliMatcher)
r.Handle("/ip", appHandler(a.CLIHandler)).Methods("GET").MatcherFunc(cliMatcher)
r.Handle("/country", appHandler(a.CLICountryHandler)).Methods("GET").MatcherFunc(cliMatcher)
r.Handle("/city", appHandler(a.CLICityHandler)).Methods("GET").MatcherFunc(cliMatcher)
// Browser
r.Handle("/", appHandler(a.DefaultHandler)).Methods("GET")