diff --git a/README.md b/README.md index 444eb534..930072b4 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,9 @@ Bornyasherk $ curl ifconfig.co/asn AS59795 + +$ curl ifconfig.co/asn-org +Hosting4Real ``` As JSON: diff --git a/cmd/echoip/main.go b/cmd/echoip/main.go index 737a4f40..f3d4c945 100644 --- a/cmd/echoip/main.go +++ b/cmd/echoip/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "log" + "strings" "os" @@ -14,14 +15,7 @@ import ( type multiValueFlag []string func (f *multiValueFlag) String() string { - vs := "" - for i, v := range *f { - vs += v - if i < len(*f)-1 { - vs += ", " - } - } - return vs + return strings.Join([]string(*f), ", ") } func (f *multiValueFlag) Set(v string) error { diff --git a/cmd/echoip/main_test.go b/cmd/echoip/main_test.go new file mode 100644 index 00000000..045a82a3 --- /dev/null +++ b/cmd/echoip/main_test.go @@ -0,0 +1,42 @@ +package main + +import "testing" + +func TestMultiValueFlagString(t *testing.T) { + var xmvf = []struct { + values multiValueFlag + expect string + }{ + { + values: multiValueFlag{ + "test", + "with multiples", + "flags", + }, + expect: `test, with multiples, flags`, + }, + { + values: multiValueFlag{ + "test", + }, + expect: `test`, + }, + { + values: multiValueFlag{ + "", + }, + expect: ``, + }, + { + values: nil, + expect: ``, + }, + } + + for _, mvf := range xmvf { + got := mvf.values.String() + if got != mvf.expect { + t.Errorf("\nFor: %#v\nExpected: %v\nGot: %v", mvf.values, mvf.expect, got) + } + } +} diff --git a/html/index.html b/html/index.html index 95570849..2ca69e69 100644 --- a/html/index.html +++ b/html/index.html @@ -32,7 +32,7 @@
-
+

{{ .Host }} — What is my IP address?

{{ .IP }}

@@ -42,8 +42,8 @@

{{ .Host }} — What is my IP address?

+ {{ if .Sponsor }}
- {{ if .Sponsor }}
@@ -63,8 +63,8 @@

{{ .Host }} — What is my IP address?

- {{ end }}
+ {{ end }}
diff --git a/http/http.go b/http/http.go index 626d75b6..d0a1838a 100644 --- a/http/http.go +++ b/http/http.go @@ -240,6 +240,15 @@ func (s *Server) CLIASNHandler(w http.ResponseWriter, r *http.Request) *appError return nil } +func (s *Server) CLIASNOrgHandler(w http.ResponseWriter, r *http.Request) *appError { + response, err := s.newResponse(r) + if err != nil { + return badRequest(err).WithMessage(err.Error()).AsJSON() + } + fmt.Fprintf(w, "%s\n", response.ASNOrg) + return nil +} + func (s *Server) JSONHandler(w http.ResponseWriter, r *http.Request) *appError { response, err := s.newResponse(r) if err != nil { @@ -431,6 +440,7 @@ func (s *Server) Handler() http.Handler { r.Route("GET", "/city", s.CLICityHandler) r.Route("GET", "/coordinates", s.CLICoordinatesHandler) r.Route("GET", "/asn", s.CLIASNHandler) + r.Route("GET", "/asn-org", s.CLIASNOrgHandler) } // Browser diff --git a/http/http_test.go b/http/http_test.go index 29dcf1bf..69c9e372 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -94,6 +94,7 @@ func TestCLIHandlers(t *testing.T) { {s.URL + "/city", "Bornyasherk\n", 200, "", ""}, {s.URL + "/foo", "404 page not found", 404, "", ""}, {s.URL + "/asn", "AS59795\n", 200, "", ""}, + {s.URL + "/asn-org", "Hosting4Real\n", 200, "", ""}, } for _, tt := range tests {