Skip to content

Commit a21e73a

Browse files
authored
Merge branch 'mpolden:master' into master
2 parents a7e88fb + d84665c commit a21e73a

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Bornyasherk
4040
4141
$ curl ifconfig.co/asn
4242
AS59795
43+
44+
$ curl ifconfig.co/asn-org
45+
Hosting4Real
4346
```
4447

4548
As JSON:
@@ -107,6 +110,11 @@ Hub](https://hub.docker.com/r/mpolden/echoip), which can be downloaded with:
107110

108111
`docker pull mpolden/echoip`
109112

113+
## [GeoIP](https://www.maxmind.com/en/geoip2-databases)/[GeoLite](https://dev.maxmind.com/geoip/geolite2-free-geolocation-data?) Database (MaxMind)
114+
To utilise MaxMind [GeoIP](https://www.maxmind.com/en/geoip2-databases)/[GeoLite](https://dev.maxmind.com/geoip/geolite2-free-geolocation-data?) database to enhance the information provided to end users, you can download the relevant **binary** databases (`.mmdb` format) directly from MaxMind using the above links.
115+
116+
**Please Note**: This has only been tested using the free, GeoLite database.
117+
110118
### Usage
111119

112120
```

cmd/echoip/main.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"flag"
55
"log"
6+
"strings"
67

78
"os"
89

@@ -14,14 +15,7 @@ import (
1415
type multiValueFlag []string
1516

1617
func (f *multiValueFlag) String() string {
17-
vs := ""
18-
for i, v := range *f {
19-
vs += v
20-
if i < len(*f)-1 {
21-
vs += ", "
22-
}
23-
}
24-
return vs
18+
return strings.Join([]string(*f), ", ")
2519
}
2620

2721
func (f *multiValueFlag) Set(v string) error {

cmd/echoip/main_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestMultiValueFlagString(t *testing.T) {
6+
var xmvf = []struct {
7+
values multiValueFlag
8+
expect string
9+
}{
10+
{
11+
values: multiValueFlag{
12+
"test",
13+
"with multiples",
14+
"flags",
15+
},
16+
expect: `test, with multiples, flags`,
17+
},
18+
{
19+
values: multiValueFlag{
20+
"test",
21+
},
22+
expect: `test`,
23+
},
24+
{
25+
values: multiValueFlag{
26+
"",
27+
},
28+
expect: ``,
29+
},
30+
{
31+
values: nil,
32+
expect: ``,
33+
},
34+
}
35+
36+
for _, mvf := range xmvf {
37+
got := mvf.values.String()
38+
if got != mvf.expect {
39+
t.Errorf("\nFor: %#v\nExpected: %v\nGot: %v", mvf.values, mvf.expect, got)
40+
}
41+
}
42+
}

http/http.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ func (s *Server) CLIASNHandler(w http.ResponseWriter, r *http.Request) *appError
240240
return nil
241241
}
242242

243+
func (s *Server) CLIASNOrgHandler(w http.ResponseWriter, r *http.Request) *appError {
244+
response, err := s.newResponse(r)
245+
if err != nil {
246+
return badRequest(err).WithMessage(err.Error()).AsJSON()
247+
}
248+
fmt.Fprintf(w, "%s\n", response.ASNOrg)
249+
return nil
250+
}
251+
243252
func (s *Server) JSONHandler(w http.ResponseWriter, r *http.Request) *appError {
244253
response, err := s.newResponse(r)
245254
if err != nil {
@@ -431,6 +440,7 @@ func (s *Server) Handler() http.Handler {
431440
r.Route("GET", "/city", s.CLICityHandler)
432441
r.Route("GET", "/coordinates", s.CLICoordinatesHandler)
433442
r.Route("GET", "/asn", s.CLIASNHandler)
443+
r.Route("GET", "/asn-org", s.CLIASNOrgHandler)
434444
}
435445

436446
// Browser

http/http_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func TestCLIHandlers(t *testing.T) {
9494
{s.URL + "/city", "Bornyasherk\n", 200, "", ""},
9595
{s.URL + "/foo", "404 page not found", 404, "", ""},
9696
{s.URL + "/asn", "AS59795\n", 200, "", ""},
97+
{s.URL + "/asn-org", "Hosting4Real\n", 200, "", ""},
9798
}
9899

99100
for _, tt := range tests {

0 commit comments

Comments
 (0)