-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Glenn Vriesman <[email protected]>
- Loading branch information
Showing
5 changed files
with
132 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package provider | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/mmcloughlin/geohash" | ||
) | ||
|
||
// server url | ||
const freeGeoIPServer = "https://freegeoip.app/json/" | ||
|
||
// Geo contains all the geolocation data | ||
type freeGeoIPPayload struct { | ||
// CountryCode of the prisoner | ||
CountryCode string `json:"country_code"` | ||
// Latitude of the prisoner | ||
Latitude float64 `json:"latitude"` | ||
// Longitude of the prisoner | ||
Longitude float64 `json:"longitude"` | ||
} | ||
|
||
// freeGeoIP is a provider | ||
type freeGeoIP struct{} | ||
|
||
// Check if freeGeoIP is a provider on compile-time | ||
var _ Provider = (*freeGeoIP)(nil) | ||
|
||
// Lookup takes an ip and returns the geohash if everthing went well. | ||
func (f freeGeoIP) Lookup(ip string) (Payload, error) { | ||
resp, err := http.Get(freeGeoIPServer + ip) | ||
if err != nil { | ||
return Payload{}, err | ||
} | ||
|
||
var ( | ||
data freeGeoIPPayload | ||
reader = new(bytes.Buffer) | ||
) | ||
_, err = reader.ReadFrom(resp.Body) | ||
if err != nil { | ||
return Payload{}, err | ||
} | ||
if err = json.Unmarshal(reader.Bytes(), &data); err != nil { | ||
return Payload{}, err | ||
} | ||
|
||
return Payload{data.CountryCode, geohash.Encode(data.Latitude, data.Longitude)}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package provider | ||
|
||
import "errors" | ||
|
||
var ( | ||
// ErrNoSuchProvider is thrown when there is no provider | ||
ErrNoSuchProvider = errors.New("no such provider") | ||
) | ||
|
||
// Payload is all the info we need about a prisoners location | ||
type Payload struct { | ||
// CountryCode represents the code, for example "NL" | ||
CountryCode string | ||
// GeoHash is latitude and longitude combined in a hash | ||
GeoHash string | ||
} | ||
|
||
// Provider is able to return a prisoners location data | ||
type Provider interface { | ||
// Fetch the prisoners location data | ||
Lookup(ip string) (Payload, error) | ||
} | ||
|
||
// New creates a new provider using the given provider name | ||
func New(provider string) (Provider, error) { | ||
switch provider { | ||
case "freeGeoIP": | ||
pr := new(freeGeoIP) | ||
return pr, nil | ||
} | ||
return nil, ErrNoSuchProvider | ||
} |