summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKali Kaneko <kali@leap.se>2018-12-13 08:55:57 -0500
committerKali Kaneko (leap communications) <kali@leap.se>2018-12-13 15:49:19 +0100
commit0493f76ad62f3288d51455d091b9470c912459f1 (patch)
tree29f223a0d29a6265536ff9159eb4d5dc8a21c8df
parent1223039a813fa836b3a7616225fc2d488b396249 (diff)
refactor for readability
-rw-r--r--main.go63
1 files changed, 35 insertions, 28 deletions
diff --git a/main.go b/main.go
index 1767a7c..b606e28 100644
--- a/main.go
+++ b/main.go
@@ -45,26 +45,18 @@ type geodb struct {
earth *ellipsoid.Ellipsoid
}
-func geolocateCity(city string) coordinates {
- // because some cities apparently are not good enough for the top 10k
- missingCities := make(map[string]coordinates)
- missingCities["hongkong"] = coordinates{22.319201099, 114.1696121}
-
- re := regexp.MustCompile("-| ")
- for i := 0; i < len(cities.Cities); i++ {
- c := cities.Cities[i]
- canonical := strings.ToLower(city)
- canonical = re.ReplaceAllString(canonical, "")
- if strings.ToLower(c.City) == canonical {
- return coordinates{c.Latitude, c.Longitude}
- }
- v, ok := missingCities[canonical]
- if ok == true {
- return v
- }
+func (g *geodb) getPointForLocation(lat float64, lon float64) *EuclideanPoint {
+ x, y, z := g.earth.ToECEF(lat, lon, 0)
+ p := NewEuclideanPoint(x, y, z)
+ return p
+}
- }
- return coordinates{0, 0}
+func (g *geodb) getClosestGateway(lat float64, lon float64) gateway {
+ t := g.getPointForLocation(lat, lon)
+ nn := g.GatewayTree.KNN(t, 1)[0]
+ p := [3]float64{nn.GetValue(0), nn.GetValue(1), nn.GetValue(2)}
+ closestGateway := g.GatewayMap[p]
+ return closestGateway
}
func (g *geodb) geolocateGateways(b *bonafide) {
@@ -77,9 +69,8 @@ func (g *geodb) geolocateGateways(b *bonafide) {
gw.Coordinates = coord
b.eip.Gateways[i] = gw
- x, y, z := g.earth.ToECEF(coord.Latitude, coord.Longitude, 0)
+ p := g.getPointForLocation(coord.Latitude, coord.Longitude)
- p := NewEuclideanPoint(x, y, z)
gatewayPoints = append(gatewayPoints, *p)
var i [3]float64
copy(i[:], p.Vec)
@@ -98,6 +89,28 @@ func (g *geodb) getRecordForIP(ipstr string) *geoip2.City {
return record
}
+func geolocateCity(city string) coordinates {
+ // because some cities apparently are not good enough for the top 10k
+ missingCities := make(map[string]coordinates)
+ missingCities["hongkong"] = coordinates{22.319201099, 114.1696121}
+
+ re := regexp.MustCompile("-| ")
+ for i := 0; i < len(cities.Cities); i++ {
+ c := cities.Cities[i]
+ canonical := strings.ToLower(city)
+ canonical = re.ReplaceAllString(canonical, "")
+ if strings.ToLower(c.City) == canonical {
+ return coordinates{c.Latitude, c.Longitude}
+ }
+ v, ok := missingCities[canonical]
+ if ok == true {
+ return v
+ }
+
+ }
+ return coordinates{0, 0}
+}
+
type jsonHandler struct {
geoipdb *geodb
}
@@ -105,13 +118,7 @@ type jsonHandler struct {
func (jh *jsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ipstr := getRemoteIP(req)
record := jh.geoipdb.getRecordForIP(ipstr)
-
- x, y, z := jh.geoipdb.earth.ToECEF(record.Location.Latitude, record.Location.Longitude, 0)
-
- t := NewEuclideanPoint(x, y, z)
- nn := jh.geoipdb.GatewayTree.KNN(t, 1)[0]
- p := [3]float64{nn.GetValue(0), nn.GetValue(1), nn.GetValue(2)}
- closestGateway := jh.geoipdb.GatewayMap[p]
+ closestGateway := jh.geoipdb.getClosestGateway(record.Location.Latitude, record.Location.Longitude)
data := map[string]string{
"ip": ipstr,