Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Bornyasherk

$ curl ifconfig.co/asn
AS59795

$ curl ifconfig.co/asn-org
Hosting4Real
```

As JSON:
Expand Down
10 changes: 2 additions & 8 deletions cmd/echoip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"log"
"strings"

"os"

Expand All @@ -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 {
Expand Down
42 changes: 42 additions & 0 deletions cmd/echoip/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
6 changes: 3 additions & 3 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<body>
<div class="content">
<div class="pure-g gutters center">
<div class="pure-u-1 pure-u-md-2-3">
<div class="pure-u-1 {{ if .Sponsor }}pure-u-md-2-3{{ end }}">
<div class="l-box">
<h1>{{ .Host }} — What is my IP address?</h1>
<p><code class="ip">{{ .IP }}</code></p>
Expand All @@ -42,8 +42,8 @@ <h1>{{ .Host }} — What is my IP address?</h1>
</p>
</div>
</div>
{{ if .Sponsor }}
<div class="pure-u-1 pure-u-md-1-3">
{{ if .Sponsor }}
<div class="l-box leafcloud-placement">
<div class="pure-g">
<div class="pure-u pure-u-md-1">
Expand All @@ -63,8 +63,8 @@ <h1>{{ .Host }} — What is my IP address?</h1>
</div>
</div>
</div>
{{ end }}
</div>
{{ end }}
</div>

<div class="pure-g gutters center">
Expand Down
10 changes: 10 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down