mirror of
https://github.com/mpolden/echoip.git
synced 2024-11-10 07:27:22 +01:00
Refactor
This commit is contained in:
parent
c8db4935ca
commit
2c5f4e08b5
41
api/api.go
41
api/api.go
@ -35,7 +35,7 @@ type Response struct {
|
|||||||
Hostname string `json:"hostname,omitempty"`
|
Hostname string `json:"hostname,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestPortResponse struct {
|
type PortResponse struct {
|
||||||
IP net.IP `json:"ip"`
|
IP net.IP `json:"ip"`
|
||||||
Port uint64 `json:"port"`
|
Port uint64 `json:"port"`
|
||||||
Reachable bool `json:"reachable"`
|
Reachable bool `json:"reachable"`
|
||||||
@ -86,6 +86,27 @@ func (a *API) newResponse(r *http.Request) (Response, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *API) newPortResponse(r *http.Request) (PortResponse, error) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
port, err := strconv.ParseUint(vars["port"], 10, 16)
|
||||||
|
if err != nil {
|
||||||
|
return PortResponse{Port: port}, err
|
||||||
|
}
|
||||||
|
if port < 1 || port > 65355 {
|
||||||
|
return PortResponse{Port: port}, fmt.Errorf("invalid port: %d", port)
|
||||||
|
}
|
||||||
|
ip, err := ipFromRequest(a.IPHeader, r)
|
||||||
|
if err != nil {
|
||||||
|
return PortResponse{Port: port}, err
|
||||||
|
}
|
||||||
|
err = a.oracle.LookupPort(ip, port)
|
||||||
|
return PortResponse{
|
||||||
|
IP: ip,
|
||||||
|
Port: port,
|
||||||
|
Reachable: err == nil,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (a *API) CLIHandler(w http.ResponseWriter, r *http.Request) *appError {
|
func (a *API) CLIHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
ip, err := ipFromRequest(a.IPHeader, r)
|
ip, err := ipFromRequest(a.IPHeader, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -128,23 +149,9 @@ func (a *API) JSONHandler(w http.ResponseWriter, r *http.Request) *appError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) PortHandler(w http.ResponseWriter, r *http.Request) *appError {
|
func (a *API) PortHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
vars := mux.Vars(r)
|
response, err := a.newPortResponse(r)
|
||||||
port, err := strconv.ParseUint(vars["port"], 10, 16)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return badRequest(err).WithMessage("Invalid port: " + vars["port"]).AsJSON()
|
return badRequest(err).WithMessage(fmt.Sprintf("Invalid port: %d", response.Port)).AsJSON()
|
||||||
}
|
|
||||||
if port < 1 || port > 65355 {
|
|
||||||
return badRequest(nil).WithMessage("Invalid port: " + vars["port"]).AsJSON()
|
|
||||||
}
|
|
||||||
ip, err := ipFromRequest(a.IPHeader, r)
|
|
||||||
if err != nil {
|
|
||||||
return internalServerError(err).AsJSON()
|
|
||||||
}
|
|
||||||
err = a.oracle.LookupPort(ip, port)
|
|
||||||
response := TestPortResponse{
|
|
||||||
IP: ip,
|
|
||||||
Port: port,
|
|
||||||
Reachable: err == nil,
|
|
||||||
}
|
}
|
||||||
b, err := json.Marshal(response)
|
b, err := json.Marshal(response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -85,6 +85,9 @@ func TestJSONHandlers(t *testing.T) {
|
|||||||
status int
|
status int
|
||||||
}{
|
}{
|
||||||
{s.URL, `{"ip":"127.0.0.1","country":"Elbonia","city":"Bornyasherk","hostname":"localhost"}`, 200},
|
{s.URL, `{"ip":"127.0.0.1","country":"Elbonia","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},
|
||||||
{s.URL + "/port/31337", `{"ip":"127.0.0.1","port":31337,"reachable":true}`, 200},
|
{s.URL + "/port/31337", `{"ip":"127.0.0.1","port":31337,"reachable":true}`, 200},
|
||||||
{s.URL + "/foo", `{"error":"404 page not found"}`, 404},
|
{s.URL + "/foo", `{"error":"404 page not found"}`, 404},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user