mirror of
https://github.com/mpolden/echoip.git
synced 2025-01-27 02:15:01 +01:00
Add country lookup as separate handler
This commit is contained in:
parent
95b5b76ef7
commit
55d72fd053
17
api/api.go
17
api/api.go
@ -142,15 +142,20 @@ func (a *API) newResponse(r *http.Request) (Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) CLIHandler(w http.ResponseWriter, r *http.Request) *appError {
|
func (a *API) CLIHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
|
ip, err := a.ipFromRequest(r)
|
||||||
|
if err != nil {
|
||||||
|
return internalServerError(err)
|
||||||
|
}
|
||||||
|
io.WriteString(w, ip.String()+"\n")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *API) CLICountryHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
response, err := a.newResponse(r)
|
response, err := a.newResponse(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return internalServerError(err)
|
return internalServerError(err)
|
||||||
}
|
}
|
||||||
if r.URL.Path == "/country" {
|
io.WriteString(w, response.Country+"\n")
|
||||||
io.WriteString(w, response.Country+"\n")
|
|
||||||
} else {
|
|
||||||
io.WriteString(w, response.IP.String()+"\n")
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,7 +280,7 @@ func (a *API) Handlers() http.Handler {
|
|||||||
// CLI
|
// CLI
|
||||||
r.Handle("/", appHandler(a.CLIHandler)).Methods("GET").MatcherFunc(cliMatcher)
|
r.Handle("/", appHandler(a.CLIHandler)).Methods("GET").MatcherFunc(cliMatcher)
|
||||||
r.Handle("/ip", appHandler(a.CLIHandler)).Methods("GET").MatcherFunc(cliMatcher)
|
r.Handle("/ip", appHandler(a.CLIHandler)).Methods("GET").MatcherFunc(cliMatcher)
|
||||||
r.Handle("/country", appHandler(a.CLIHandler)).Methods("GET").MatcherFunc(cliMatcher)
|
r.Handle("/country", appHandler(a.CLICountryHandler)).Methods("GET").MatcherFunc(cliMatcher)
|
||||||
|
|
||||||
// Browser
|
// Browser
|
||||||
r.Handle("/", appHandler(a.DefaultHandler)).Methods("GET")
|
r.Handle("/", appHandler(a.DefaultHandler)).Methods("GET")
|
||||||
|
@ -50,7 +50,7 @@ func httpGet(url string, json bool, userAgent string) (string, int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetIP(t *testing.T) {
|
func TestGetIP(t *testing.T) {
|
||||||
//log.SetOutput(ioutil.Discard)
|
log.SetOutput(ioutil.Discard)
|
||||||
toJSON := func(r Response) string {
|
toJSON := func(r Response) string {
|
||||||
b, err := json.Marshal(r)
|
b, err := json.Marshal(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -72,6 +72,7 @@ func TestGetIP(t *testing.T) {
|
|||||||
{s.URL, false, "127.0.0.1\n", "Go 1.1 package http", 200},
|
{s.URL, false, "127.0.0.1\n", "Go 1.1 package http", 200},
|
||||||
{s.URL, false, "127.0.0.1\n", "Go-http-client/1.1", 200},
|
{s.URL, false, "127.0.0.1\n", "Go-http-client/1.1", 200},
|
||||||
{s.URL, false, "127.0.0.1\n", "Go-http-client/2.0", 200},
|
{s.URL, false, "127.0.0.1\n", "Go-http-client/2.0", 200},
|
||||||
|
{s.URL + "/country", false, "Elbonia\n", "curl/7.26.0", 200},
|
||||||
{s.URL, true, toJSON(Response{IP: net.ParseIP("127.0.0.1"), Country: "Elbonia", Hostname: "localhost"}), "", 200},
|
{s.URL, true, toJSON(Response{IP: net.ParseIP("127.0.0.1"), Country: "Elbonia", Hostname: "localhost"}), "", 200},
|
||||||
{s.URL + "/foo", false, "404 page not found", "curl/7.26.0", 404},
|
{s.URL + "/foo", false, "404 page not found", "curl/7.26.0", 404},
|
||||||
{s.URL + "/foo", true, "{\"error\":\"404 page not found\"}", "curl/7.26.0", 404},
|
{s.URL + "/foo", true, "{\"error\":\"404 page not found\"}", "curl/7.26.0", 404},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user