mirror of https://github.com/mpolden/echoip
Rename package database -> geo
This commit is contained in:
parent
3497e6e28a
commit
059db6372c
|
@ -9,7 +9,7 @@ import (
|
||||||
|
|
||||||
"github.com/mpolden/ipd/http"
|
"github.com/mpolden/ipd/http"
|
||||||
"github.com/mpolden/ipd/iputil"
|
"github.com/mpolden/ipd/iputil"
|
||||||
"github.com/mpolden/ipd/iputil/database"
|
"github.com/mpolden/ipd/iputil/geo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -28,12 +28,12 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
log := log.New(os.Stderr, "ipd: ", 0)
|
log := log.New(os.Stderr, "ipd: ", 0)
|
||||||
db, err := database.New(opts.CountryDBPath, opts.CityDBPath)
|
r, err := geo.Open(opts.CountryDBPath, opts.CityDBPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
server := http.New(db)
|
server := http.New(r)
|
||||||
server.Template = opts.Template
|
server.Template = opts.Template
|
||||||
server.IPHeaders = opts.IPHeaders
|
server.IPHeaders = opts.IPHeaders
|
||||||
if opts.ReverseLookup {
|
if opts.ReverseLookup {
|
||||||
|
|
14
http/http.go
14
http/http.go
|
@ -7,7 +7,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/mpolden/ipd/iputil"
|
"github.com/mpolden/ipd/iputil"
|
||||||
"github.com/mpolden/ipd/iputil/database"
|
"github.com/mpolden/ipd/iputil/geo"
|
||||||
"github.com/mpolden/ipd/useragent"
|
"github.com/mpolden/ipd/useragent"
|
||||||
|
|
||||||
"net"
|
"net"
|
||||||
|
@ -25,7 +25,7 @@ type Server struct {
|
||||||
IPHeaders []string
|
IPHeaders []string
|
||||||
LookupAddr func(net.IP) (string, error)
|
LookupAddr func(net.IP) (string, error)
|
||||||
LookupPort func(net.IP, uint64) error
|
LookupPort func(net.IP, uint64) error
|
||||||
db database.Client
|
gr geo.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
|
@ -43,8 +43,8 @@ type PortResponse struct {
|
||||||
Reachable bool `json:"reachable"`
|
Reachable bool `json:"reachable"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(db database.Client) *Server {
|
func New(db geo.Reader) *Server {
|
||||||
return &Server{db: db}
|
return &Server{gr: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ipFromRequest(headers []string, r *http.Request) (net.IP, error) {
|
func ipFromRequest(headers []string, r *http.Request) (net.IP, error) {
|
||||||
|
@ -75,8 +75,8 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
|
||||||
return Response{}, err
|
return Response{}, err
|
||||||
}
|
}
|
||||||
ipDecimal := iputil.ToDecimal(ip)
|
ipDecimal := iputil.ToDecimal(ip)
|
||||||
country, _ := s.db.Country(ip)
|
country, _ := s.gr.Country(ip)
|
||||||
city, _ := s.db.City(ip)
|
city, _ := s.gr.City(ip)
|
||||||
var hostname string
|
var hostname string
|
||||||
if s.LookupAddr != nil {
|
if s.LookupAddr != nil {
|
||||||
hostname, _ = s.LookupAddr(ip)
|
hostname, _ = s.LookupAddr(ip)
|
||||||
|
@ -264,7 +264,7 @@ func (s *Server) Handler() http.Handler {
|
||||||
r.Route("GET", "/", s.CLIHandler).MatcherFunc(cliMatcher)
|
r.Route("GET", "/", s.CLIHandler).MatcherFunc(cliMatcher)
|
||||||
r.Route("GET", "/", s.CLIHandler).Header("Accept", textMediaType)
|
r.Route("GET", "/", s.CLIHandler).Header("Accept", textMediaType)
|
||||||
r.Route("GET", "/ip", s.CLIHandler)
|
r.Route("GET", "/ip", s.CLIHandler)
|
||||||
if !s.db.IsEmpty() {
|
if !s.gr.IsEmpty() {
|
||||||
r.Route("GET", "/country", s.CLICountryHandler)
|
r.Route("GET", "/country", s.CLICountryHandler)
|
||||||
r.Route("GET", "/country-iso", s.CLICountryISOHandler)
|
r.Route("GET", "/country-iso", s.CLICountryISOHandler)
|
||||||
r.Route("GET", "/city", s.CLICityHandler)
|
r.Route("GET", "/city", s.CLICityHandler)
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/mpolden/ipd/iputil/database"
|
"github.com/mpolden/ipd/iputil/geo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func lookupAddr(net.IP) (string, error) { return "localhost", nil }
|
func lookupAddr(net.IP) (string, error) { return "localhost", nil }
|
||||||
|
@ -16,15 +16,15 @@ func lookupPort(net.IP, uint64) error { return nil }
|
||||||
|
|
||||||
type testDb struct{}
|
type testDb struct{}
|
||||||
|
|
||||||
func (t *testDb) Country(net.IP) (database.Country, error) {
|
func (t *testDb) Country(net.IP) (geo.Country, error) {
|
||||||
return database.Country{Name: "Elbonia", ISO: "EB"}, nil
|
return geo.Country{Name: "Elbonia", ISO: "EB"}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *testDb) City(net.IP) (string, error) { return "Bornyasherk", nil }
|
func (t *testDb) City(net.IP) (string, error) { return "Bornyasherk", nil }
|
||||||
func (t *testDb) IsEmpty() bool { return false }
|
func (t *testDb) IsEmpty() bool { return false }
|
||||||
|
|
||||||
func testServer() *Server {
|
func testServer() *Server {
|
||||||
return &Server{db: &testDb{}, LookupAddr: lookupAddr, LookupPort: lookupPort}
|
return &Server{gr: &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) {
|
||||||
|
@ -87,7 +87,7 @@ func TestDisabledHandlers(t *testing.T) {
|
||||||
server := testServer()
|
server := testServer()
|
||||||
server.LookupPort = nil
|
server.LookupPort = nil
|
||||||
server.LookupAddr = nil
|
server.LookupAddr = nil
|
||||||
server.db, _ = database.New("", "")
|
server.db, _ = geo.New("", "")
|
||||||
s := httptest.NewServer(server.Handler())
|
s := httptest.NewServer(server.Handler())
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package database
|
package geo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
@ -6,7 +6,7 @@ import (
|
||||||
geoip2 "github.com/oschwald/geoip2-golang"
|
geoip2 "github.com/oschwald/geoip2-golang"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client interface {
|
type Reader interface {
|
||||||
Country(net.IP) (Country, error)
|
Country(net.IP) (Country, error)
|
||||||
City(net.IP) (string, error)
|
City(net.IP) (string, error)
|
||||||
IsEmpty() bool
|
IsEmpty() bool
|
||||||
|
@ -22,7 +22,7 @@ type geoip struct {
|
||||||
city *geoip2.Reader
|
city *geoip2.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(countryDB, cityDB string) (Client, error) {
|
func Open(countryDB, cityDB string) (Reader, error) {
|
||||||
var country, city *geoip2.Reader
|
var country, city *geoip2.Reader
|
||||||
if countryDB != "" {
|
if countryDB != "" {
|
||||||
r, err := geoip2.Open(countryDB)
|
r, err := geoip2.Open(countryDB)
|
Loading…
Reference in New Issue