mirror of https://github.com/mpolden/echoip
Disable handlers for disabled features
This commit is contained in:
parent
8112536125
commit
215cce290c
26
http/http.go
26
http/http.go
|
@ -52,8 +52,8 @@ type PortResponse struct {
|
||||||
Reachable bool `json:"reachable"`
|
Reachable bool `json:"reachable"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(db db.Database, lookupAddr LookupAddr, lookupPort LookupPort, logger *logrus.Logger) *Server {
|
func New(db db.Database, lookupAddr LookupAddr, lookupPort LookupPort, log *logrus.Logger) *Server {
|
||||||
return &Server{lookupAddr: lookupAddr, lookupPort: lookupPort, db: db, log: logger}
|
return &Server{lookupAddr: lookupAddr, lookupPort: lookupPort, db: db, log: log}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ipFromRequest(header string, r *http.Request) (net.IP, error) {
|
func ipFromRequest(header string, r *http.Request) (net.IP, error) {
|
||||||
|
@ -86,9 +86,13 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log.Debug(err)
|
s.log.Debug(err)
|
||||||
}
|
}
|
||||||
hostnames, err := s.lookupAddr(ip)
|
var hostnames []string
|
||||||
if err != nil {
|
if s.lookupAddr != nil {
|
||||||
s.log.Debug(err)
|
h, err := s.lookupAddr(ip)
|
||||||
|
if err != nil {
|
||||||
|
s.log.Debug(err)
|
||||||
|
}
|
||||||
|
hostnames = h
|
||||||
}
|
}
|
||||||
return Response{
|
return Response{
|
||||||
IP: ip,
|
IP: ip,
|
||||||
|
@ -269,15 +273,19 @@ func (s *Server) Handler() http.Handler {
|
||||||
r.Handle("/", appHandler(s.CLIHandler)).Methods("GET").MatcherFunc(cliMatcher)
|
r.Handle("/", appHandler(s.CLIHandler)).Methods("GET").MatcherFunc(cliMatcher)
|
||||||
r.Handle("/", appHandler(s.CLIHandler)).Methods("GET").Headers("Accept", textMediaType)
|
r.Handle("/", appHandler(s.CLIHandler)).Methods("GET").Headers("Accept", textMediaType)
|
||||||
r.Handle("/ip", appHandler(s.CLIHandler)).Methods("GET")
|
r.Handle("/ip", appHandler(s.CLIHandler)).Methods("GET")
|
||||||
r.Handle("/country", appHandler(s.CLICountryHandler)).Methods("GET")
|
if !s.db.IsEmpty() {
|
||||||
r.Handle("/country-iso", appHandler(s.CLICountryISOHandler)).Methods("GET")
|
r.Handle("/country", appHandler(s.CLICountryHandler)).Methods("GET")
|
||||||
r.Handle("/city", appHandler(s.CLICityHandler)).Methods("GET")
|
r.Handle("/country-iso", appHandler(s.CLICountryISOHandler)).Methods("GET")
|
||||||
|
r.Handle("/city", appHandler(s.CLICityHandler)).Methods("GET")
|
||||||
|
}
|
||||||
|
|
||||||
// Browser
|
// Browser
|
||||||
r.Handle("/", appHandler(s.DefaultHandler)).Methods("GET")
|
r.Handle("/", appHandler(s.DefaultHandler)).Methods("GET")
|
||||||
|
|
||||||
// Port testing
|
// Port testing
|
||||||
r.Handle("/port/{port:[0-9]+}", appHandler(s.PortHandler)).Methods("GET")
|
if s.lookupPort != nil {
|
||||||
|
r.Handle("/port/{port:[0-9]+}", appHandler(s.PortHandler)).Methods("GET")
|
||||||
|
}
|
||||||
|
|
||||||
// Not found handler which returns JSON when appropriate
|
// Not found handler which returns JSON when appropriate
|
||||||
r.NotFoundHandler = appHandler(s.NotFoundHandler)
|
r.NotFoundHandler = appHandler(s.NotFoundHandler)
|
||||||
|
|
|
@ -11,17 +11,20 @@ import (
|
||||||
"github.com/mpolden/ipd/iputil/db"
|
"github.com/mpolden/ipd/iputil/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
type database struct{}
|
|
||||||
|
|
||||||
func lookupAddr(net.IP) ([]string, error) { return []string{"localhost"}, nil }
|
func lookupAddr(net.IP) ([]string, error) { return []string{"localhost"}, nil }
|
||||||
func lookupPort(net.IP, uint64) error { return nil }
|
func lookupPort(net.IP, uint64) error { return nil }
|
||||||
func (d *database) Country(net.IP) (db.Country, error) {
|
|
||||||
|
type testDb struct{}
|
||||||
|
|
||||||
|
func (t *testDb) Country(net.IP) (db.Country, error) {
|
||||||
return db.Country{Name: "Elbonia", ISO: "EB"}, nil
|
return db.Country{Name: "Elbonia", ISO: "EB"}, nil
|
||||||
}
|
}
|
||||||
func (d *database) City(net.IP) (string, error) { return "Bornyasherk", nil }
|
|
||||||
|
|
||||||
func newTestAPI() *Server {
|
func (t *testDb) City(net.IP) (string, error) { return "Bornyasherk", nil }
|
||||||
return &Server{db: &database{}, lookupAddr: lookupAddr, lookupPort: lookupPort}
|
func (t *testDb) IsEmpty() bool { return false }
|
||||||
|
|
||||||
|
func testServer() *Server {
|
||||||
|
return &Server{db: &testDb{}, lookupAddr: lookupAddr, lookupPort: lookupPort}
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpGet(url string, acceptMediaType string, userAgent string) (string, int, error) {
|
func httpGet(url string, acceptMediaType string, userAgent string) (string, int, error) {
|
||||||
|
@ -47,7 +50,7 @@ func httpGet(url string, acceptMediaType string, userAgent string) (string, int,
|
||||||
|
|
||||||
func TestCLIHandlers(t *testing.T) {
|
func TestCLIHandlers(t *testing.T) {
|
||||||
log.SetOutput(ioutil.Discard)
|
log.SetOutput(ioutil.Discard)
|
||||||
s := httptest.NewServer(newTestAPI().Handler())
|
s := httptest.NewServer(testServer().Handler())
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
url string
|
url string
|
||||||
|
@ -79,9 +82,43 @@ func TestCLIHandlers(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDisabledHandlers(t *testing.T) {
|
||||||
|
log.SetOutput(ioutil.Discard)
|
||||||
|
server := testServer()
|
||||||
|
server.lookupPort = nil
|
||||||
|
server.lookupAddr = nil
|
||||||
|
server.db = db.Empty()
|
||||||
|
s := httptest.NewServer(server.Handler())
|
||||||
|
|
||||||
|
var tests = []struct {
|
||||||
|
url string
|
||||||
|
out string
|
||||||
|
status int
|
||||||
|
}{
|
||||||
|
{s.URL + "/port/1337", "404 page not found", 404},
|
||||||
|
{s.URL + "/country", "404 page not found", 404},
|
||||||
|
{s.URL + "/country-iso", "404 page not found", 404},
|
||||||
|
{s.URL + "/city", "404 page not found", 404},
|
||||||
|
{s.URL + "/json", `{"ip":"127.0.0.1","ip_decimal":2130706433}`, 200},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
out, status, err := httpGet(tt.url, "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if status != tt.status {
|
||||||
|
t.Errorf("Expected %d, got %d", tt.status, status)
|
||||||
|
}
|
||||||
|
if out != tt.out {
|
||||||
|
t.Errorf("Expected %q, got %q", tt.out, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestJSONHandlers(t *testing.T) {
|
func TestJSONHandlers(t *testing.T) {
|
||||||
log.SetOutput(ioutil.Discard)
|
log.SetOutput(ioutil.Discard)
|
||||||
s := httptest.NewServer(newTestAPI().Handler())
|
s := httptest.NewServer(testServer().Handler())
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
url string
|
url string
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
type Database interface {
|
type Database interface {
|
||||||
Country(net.IP) (Country, error)
|
Country(net.IP) (Country, error)
|
||||||
City(net.IP) (string, error)
|
City(net.IP) (string, error)
|
||||||
|
IsEmpty() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Country struct {
|
type Country struct {
|
||||||
|
@ -25,6 +26,7 @@ type empty struct{}
|
||||||
|
|
||||||
func (d *empty) Country(ip net.IP) (Country, error) { return Country{}, nil }
|
func (d *empty) Country(ip net.IP) (Country, error) { return Country{}, nil }
|
||||||
func (d *empty) City(ip net.IP) (string, error) { return "", nil }
|
func (d *empty) City(ip net.IP) (string, error) { return "", nil }
|
||||||
|
func (d *empty) IsEmpty() bool { return true }
|
||||||
|
|
||||||
func Empty() Database { return &empty{} }
|
func Empty() Database { return &empty{} }
|
||||||
|
|
||||||
|
@ -87,3 +89,7 @@ func (g *geoip) City(ip net.IP) (string, error) {
|
||||||
}
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *geoip) IsEmpty() bool {
|
||||||
|
return g.country != nil || g.city != nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue