Requested modifications to streamline code

This commit is contained in:
Johann Richard 2018-02-04 23:05:18 +01:00
parent 8537b74a58
commit a4816e9baa
5 changed files with 13 additions and 14 deletions

View File

@ -32,7 +32,7 @@ Country and city lookup:
$ http ifconfig.co/country
Elbonia
$ http ifconfig.co/iso
$ http ifconfig.co/country-iso
EB
$ http ifconfig.co/city

View File

@ -34,7 +34,7 @@ type Response struct {
IP net.IP `json:"ip"`
IPDecimal *big.Int `json:"ip_decimal"`
Country string `json:"country,omitempty"`
CountryISO string `json:"isoCountry,omitempty"`
CountryISO string `json:"country_iso,omitempty"`
City string `json:"city,omitempty"`
Hostname string `json:"hostname,omitempty"`
}
@ -85,7 +85,7 @@ func (a *API) newResponse(r *http.Request) (Response, error) {
if err != nil {
a.log.Debug(err)
}
iso, err := a.oracle.LookupCountryISO(ip)
countryISO, err := a.oracle.LookupCountryISO(ip)
if err != nil {
a.log.Debug(err)
}
@ -101,7 +101,7 @@ func (a *API) newResponse(r *http.Request) (Response, error) {
IP: ip,
IPDecimal: ipDecimal,
Country: country,
CountryISO: iso,
CountryISO: countryISO,
City: city,
Hostname: strings.Join(hostnames, " "),
}, nil
@ -265,7 +265,7 @@ func (a *API) Router() http.Handler {
r.Handle("/", appHandler(a.CLIHandler)).Methods("GET").Headers("Accept", textMediaType)
r.Handle("/ip", appHandler(a.CLIHandler)).Methods("GET")
r.Handle("/country", appHandler(a.CLICountryHandler)).Methods("GET")
r.Handle("/iso", appHandler(a.CLICountryISOHandler)).Methods("GET")
r.Handle("/country-iso", appHandler(a.CLICountryISOHandler)).Methods("GET")
r.Handle("/city", appHandler(a.CLICityHandler)).Methods("GET")
// Browser

View File

@ -62,7 +62,7 @@ func TestCLIHandlers(t *testing.T) {
{s.URL, "127.0.0.1\n", 200, "foo/bar", textMediaType},
{s.URL + "/ip", "127.0.0.1\n", 200, "", ""},
{s.URL + "/country", "Elbonia\n", 200, "", ""},
{s.URL + "/iso", "EB\n", 200, "", ""},
{s.URL + "/country-iso", "EB\n", 200, "", ""},
{s.URL + "/city", "Bornyasherk\n", 200, "", ""},
{s.URL + "/foo", "404 page not found", 404, "", ""},
}
@ -90,7 +90,7 @@ func TestJSONHandlers(t *testing.T) {
out string
status int
}{
{s.URL, `{"ip":"127.0.0.1","ip_decimal":2130706433,"country":"Elbonia","isoCountry":"EB","city":"Bornyasherk","hostname":"localhost"}`, 200},
{s.URL, `{"ip":"127.0.0.1","ip_decimal":2130706433,"country":"Elbonia","country_iso":"EB","city":"Bornyasherk","hostname":"localhost"}`, 200},
{s.URL + "/port/foo", `{"error":"404 page not found"}`, 404},
{s.URL + "/port/0", `{"error":"Invalid port: 0"}`, 400},
{s.URL + "/port/65356", `{"error":"Invalid port: 65356"}`, 400},

View File

@ -142,7 +142,6 @@ func lookupCountryISO(db *geoip2.Reader, ip net.IP) (string, error) {
if err != nil {
return "", err
}
if record.Country.IsoCode != "" {
return record.Country.IsoCode, nil
}

View File

@ -63,9 +63,9 @@ $ bat -print=b {{ .Host }}/ip
<pre>
$ http {{ .Host }}/country
{{ .Country }}</pre>
<h2>Country ISO lookup:</h2>
<h2>Short country code (ISO 3166-1 alpha-2) lookup:</h2>
<pre>
$ http {{ .Host }}/iso
$ http {{ .Host }}/country-iso
{{ .CountryISO }}</pre>
{{ end }}
{{ if .IsLookupCityEnabled }}
@ -82,7 +82,7 @@ $ http {{ .Host }}/json
{ {{ if .IsLookupCityEnabled }}
"city": "{{ .City }}",
"country": "{{ .Country }}",
"isoCountry": "{{ .CountryISO }}",{{ end }}{{ if .IsLookupAddrEnabled }}
"country_iso": "{{ .CountryISO }}",{{ end }}{{ if .IsLookupAddrEnabled }}
"hostname": "{{ .Hostname }}",{{ end }}
"ip": "{{ .IP }}",
"ip_decimal": {{ .IPDecimal }}
@ -105,12 +105,12 @@ $ http {{ .Host }}/port/8080
}</pre>
{{ end }}
</div>
{{ if .IsLookupCityEnabled }}
{{ if .IsLookupCountryEnabled }}{{ if ne .Country "Unknown"}}{{ if .IsLookupCityEnabled }}{{ if ne .City "Unknown"}}
<div class="pure-u-1 pure-u-md-1-1">
<h2>MAP</h2>
<h2>Map</h2>
<p><img src="https://maps.googleapis.com/maps/api/staticmap?size=600x200&amp;scale=2&amp;markers=color%3Aorange%7Clabel%3AS%7c{{ .City }},{{ .Country }}" width="600" height="200"/>
</p>
{{ end }}
{{ end }}{{ end }}{{ end }}{{ end }}
</div>
<div class="pure-u-1 pure-u-md-1-1">
<h2>FAQ</h2>