Remove ipFromRequest field

This commit is contained in:
Martin Polden 2016-04-17 16:03:00 +02:00
parent 3134de8260
commit 73fd32f37e
2 changed files with 8 additions and 17 deletions

View File

@ -23,10 +23,9 @@ var USER_AGENT_RE = regexp.MustCompile(
) )
type API struct { type API struct {
Template string Template string
IPHeader string IPHeader string
oracle Oracle oracle Oracle
ipFromRequest func(string, *http.Request) (net.IP, error)
} }
type Response struct { type Response struct {
@ -43,10 +42,7 @@ type TestPortResponse struct {
} }
func New(oracle Oracle) *API { func New(oracle Oracle) *API {
return &API{ return &API{oracle: oracle}
oracle: oracle,
ipFromRequest: ipFromRequest,
}
} }
func ipFromRequest(header string, r *http.Request) (net.IP, error) { func ipFromRequest(header string, r *http.Request) (net.IP, error) {
@ -66,7 +62,7 @@ func ipFromRequest(header string, r *http.Request) (net.IP, error) {
} }
func (a *API) newResponse(r *http.Request) (Response, error) { func (a *API) newResponse(r *http.Request) (Response, error) {
ip, err := a.ipFromRequest(a.IPHeader, r) ip, err := ipFromRequest(a.IPHeader, r)
if err != nil { if err != nil {
return Response{}, err return Response{}, err
} }
@ -91,7 +87,7 @@ 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(a.IPHeader, r) ip, err := ipFromRequest(a.IPHeader, r)
if err != nil { if err != nil {
return internalServerError(err) return internalServerError(err)
} }
@ -140,7 +136,7 @@ func (a *API) PortHandler(w http.ResponseWriter, r *http.Request) *appError {
if port < 1 || port > 65355 { if port < 1 || port > 65355 {
return badRequest(nil).WithMessage("Invalid port: " + vars["port"]).AsJSON() return badRequest(nil).WithMessage("Invalid port: " + vars["port"]).AsJSON()
} }
ip, err := a.ipFromRequest(a.IPHeader, r) ip, err := ipFromRequest(a.IPHeader, r)
if err != nil { if err != nil {
return internalServerError(err).AsJSON() return internalServerError(err).AsJSON()
} }

View File

@ -21,12 +21,7 @@ func (r *mockOracle) IsLookupCityEnabled() bool { return true }
func (r *mockOracle) IsLookupPortEnabled() bool { return true } func (r *mockOracle) IsLookupPortEnabled() bool { return true }
func newTestAPI() *API { func newTestAPI() *API {
return &API{ return &API{oracle: &mockOracle{}}
oracle: &mockOracle{},
ipFromRequest: func(string, *http.Request) (net.IP, error) {
return net.ParseIP("127.0.0.1"), nil
},
}
} }
func httpGet(url string, json bool, userAgent string) (string, int, error) { func httpGet(url string, json bool, userAgent string) (string, int, error) {