mirror of
https://github.com/mpolden/echoip.git
synced 2025-02-04 14:25:19 +01:00
Added coordinates from City database
This commit is contained in:
parent
b821b1efcc
commit
ac4a9de770
44
http/http.go
44
http/http.go
@ -31,12 +31,15 @@ type Server struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
IP net.IP `json:"ip"`
|
IP net.IP `json:"ip"`
|
||||||
IPDecimal *big.Int `json:"ip_decimal"`
|
IPDecimal *big.Int `json:"ip_decimal"`
|
||||||
Country string `json:"country,omitempty"`
|
Country string `json:"country,omitempty"`
|
||||||
CountryISO string `json:"country_iso,omitempty"`
|
CountryISO string `json:"country_iso,omitempty"`
|
||||||
City string `json:"city,omitempty"`
|
City string `json:"city,omitempty"`
|
||||||
Hostname string `json:"hostname,omitempty"`
|
Hostname string `json:"hostname,omitempty"`
|
||||||
|
IsInEuropeanUnion bool `json:"is_in_european_union,omitempty"`
|
||||||
|
Latitude float64 `json:"location_latitude,omitempty"`
|
||||||
|
Longitude float64 `json:"location_longitude,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PortResponse struct {
|
type PortResponse struct {
|
||||||
@ -95,12 +98,15 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
|
|||||||
hostname, _ = s.LookupAddr(ip)
|
hostname, _ = s.LookupAddr(ip)
|
||||||
}
|
}
|
||||||
return Response{
|
return Response{
|
||||||
IP: ip,
|
IP: ip,
|
||||||
IPDecimal: ipDecimal,
|
IPDecimal: ipDecimal,
|
||||||
Country: country.Name,
|
Country: country.Name,
|
||||||
CountryISO: country.ISO,
|
CountryISO: country.ISO,
|
||||||
City: city,
|
IsInEuropeanUnion: country.IsInEuropeanUnion,
|
||||||
Hostname: hostname,
|
City: city.Name,
|
||||||
|
Hostname: hostname,
|
||||||
|
Latitude: city.Latitude,
|
||||||
|
Longitude: city.Longitude,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,6 +164,15 @@ func (s *Server) CLICityHandler(w http.ResponseWriter, r *http.Request) *appErro
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) CLICoordinatesHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
|
response, err := s.newResponse(r)
|
||||||
|
if err != nil {
|
||||||
|
return internalServerError(err)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "%s, %s\n", formatCoordinate(response.Latitude), formatCoordinate(response.Longitude))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) JSONHandler(w http.ResponseWriter, r *http.Request) *appError {
|
func (s *Server) JSONHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
response, err := s.newResponse(r)
|
response, err := s.newResponse(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -281,6 +296,7 @@ func (s *Server) Handler() http.Handler {
|
|||||||
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)
|
||||||
|
r.Route("GET", "/coordinates", s.CLICoordinatesHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Browser
|
// Browser
|
||||||
@ -297,3 +313,7 @@ func (s *Server) Handler() http.Handler {
|
|||||||
func (s *Server) ListenAndServe(addr string) error {
|
func (s *Server) ListenAndServe(addr string) error {
|
||||||
return http.ListenAndServe(addr, s.Handler())
|
return http.ListenAndServe(addr, s.Handler())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatCoordinate(c float64) string {
|
||||||
|
return strconv.FormatFloat(c, 'f', 6, 64)
|
||||||
|
}
|
||||||
|
@ -20,7 +20,10 @@ func (t *testDb) Country(net.IP) (geo.Country, error) {
|
|||||||
return geo.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) (database.City, error) {
|
||||||
|
return database.City{Name: "Bornyasherk"}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (t *testDb) IsEmpty() bool { return false }
|
func (t *testDb) IsEmpty() bool { return false }
|
||||||
|
|
||||||
func testServer() *Server {
|
func testServer() *Server {
|
||||||
|
@ -2,19 +2,27 @@ package geo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
"math"
|
||||||
|
|
||||||
geoip2 "github.com/oschwald/geoip2-golang"
|
geoip2 "github.com/oschwald/geoip2-golang"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Reader interface {
|
type Reader interface {
|
||||||
Country(net.IP) (Country, error)
|
Country(net.IP) (Country, error)
|
||||||
City(net.IP) (string, error)
|
City(net.IP) (City, error)
|
||||||
IsEmpty() bool
|
IsEmpty() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Country struct {
|
type Country struct {
|
||||||
Name string
|
Name string
|
||||||
ISO string
|
ISO string
|
||||||
|
IsInEuropeanUnion bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type City struct {
|
||||||
|
Name string
|
||||||
|
Latitude float64
|
||||||
|
Longitude float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type geoip struct {
|
type geoip struct {
|
||||||
@ -62,21 +70,32 @@ func (g *geoip) Country(ip net.IP) (Country, error) {
|
|||||||
if record.RegisteredCountry.IsoCode != "" && country.ISO == "" {
|
if record.RegisteredCountry.IsoCode != "" && country.ISO == "" {
|
||||||
country.ISO = record.RegisteredCountry.IsoCode
|
country.ISO = record.RegisteredCountry.IsoCode
|
||||||
}
|
}
|
||||||
|
country.IsInEuropeanUnion = record.Country.IsInEuropeanUnion
|
||||||
|
if record.RegisteredCountry.IsoCode != "" && country.ISO == "" {
|
||||||
|
country.IsInEuropeanUnion = record.RegisteredCountry.IsInEuropeanUnion
|
||||||
|
}
|
||||||
return country, nil
|
return country, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *geoip) City(ip net.IP) (string, error) {
|
func (g *geoip) City(ip net.IP) (City, error) {
|
||||||
|
city := City{}
|
||||||
if g.city == nil {
|
if g.city == nil {
|
||||||
return "", nil
|
return city, nil
|
||||||
}
|
}
|
||||||
record, err := g.city.City(ip)
|
record, err := g.city.City(ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return city, err
|
||||||
}
|
}
|
||||||
if city, exists := record.City.Names["en"]; exists {
|
if c, exists := record.City.Names["en"]; exists {
|
||||||
return city, nil
|
city.Name = c
|
||||||
}
|
}
|
||||||
return "", nil
|
if !math.IsNaN(record.Location.Latitude) {
|
||||||
|
city.Latitude = record.Location.Latitude
|
||||||
|
}
|
||||||
|
if !math.IsNaN(record.Location.Longitude) {
|
||||||
|
city.Longitude = record.Location.Longitude
|
||||||
|
}
|
||||||
|
return city, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *geoip) IsEmpty() bool {
|
func (g *geoip) IsEmpty() bool {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user