mirror of
https://github.com/chubin/wttr.in
synced 2025-01-25 18:24:59 +00:00
Rename Location to Address in geo/ip
This commit is contained in:
parent
d91c6da43e
commit
c9fc3aa74a
3 changed files with 24 additions and 24 deletions
|
@ -25,20 +25,20 @@ func (c *Cache) ConvertCache() error {
|
|||
return err
|
||||
}
|
||||
|
||||
err = createTable(db, "Location")
|
||||
err = createTable(db, "Address")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Println("listing cache entries...")
|
||||
files, err := filepath.Glob(filepath.Join(c.config.Geo.LocationCache, "*"))
|
||||
files, err := filepath.Glob(filepath.Join(c.config.Geo.IPCache, "*"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("going to convert %d entries\n", len(files))
|
||||
|
||||
block := []Location{}
|
||||
block := []Address{}
|
||||
for i, file := range files {
|
||||
ip := filepath.Base(file)
|
||||
loc, err := c.Read(ip)
|
||||
|
@ -58,7 +58,7 @@ func (c *Cache) ConvertCache() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
block = []Location{}
|
||||
block = []Address{}
|
||||
log.Println("converted", i+1, "entries")
|
||||
}
|
||||
|
||||
|
|
|
@ -18,8 +18,8 @@ import (
|
|||
"github.com/samonzeweb/godb/adapters/sqlite"
|
||||
)
|
||||
|
||||
// Location information.
|
||||
type Location struct {
|
||||
// Address information.
|
||||
type Address struct {
|
||||
IP string `db:"ip,key"`
|
||||
CountryCode string `db:"countryCode"`
|
||||
Country string `db:"country"`
|
||||
|
@ -29,7 +29,7 @@ type Location struct {
|
|||
Longitude float64 `db:"longitude"`
|
||||
}
|
||||
|
||||
func (l *Location) String() string {
|
||||
func (l *Address) String() string {
|
||||
if l.Latitude == -1000 {
|
||||
return fmt.Sprintf(
|
||||
"%s;%s;%s;%s",
|
||||
|
@ -76,7 +76,7 @@ func NewCache(config *config.Config) (*Cache, error) {
|
|||
// DE;Germany;Free and Hanseatic City of Hamburg;Hamburg;53.5736;9.9782
|
||||
//
|
||||
|
||||
func (c *Cache) Read(addr string) (*Location, error) {
|
||||
func (c *Cache) Read(addr string) (*Address, error) {
|
||||
if c.config.Geo.IPCacheType == types.CacheTypeDB {
|
||||
return c.readFromCacheDB(addr)
|
||||
}
|
||||
|
@ -84,17 +84,17 @@ func (c *Cache) Read(addr string) (*Location, error) {
|
|||
return c.readFromCacheFile(addr)
|
||||
}
|
||||
|
||||
func (c *Cache) readFromCacheFile(addr string) (*Location, error) {
|
||||
func (c *Cache) readFromCacheFile(addr string) (*Address, error) {
|
||||
bytes, err := os.ReadFile(c.cacheFile(addr))
|
||||
if err != nil {
|
||||
return nil, types.ErrNotFound
|
||||
}
|
||||
|
||||
return NewLocationFromString(addr, string(bytes))
|
||||
return NewAddressFromString(addr, string(bytes))
|
||||
}
|
||||
|
||||
func (c *Cache) readFromCacheDB(addr string) (*Location, error) {
|
||||
result := Location{}
|
||||
func (c *Cache) readFromCacheDB(addr string) (*Address, error) {
|
||||
result := Address{}
|
||||
err := c.db.Select(&result).
|
||||
Where("IP = ?", addr).
|
||||
Do()
|
||||
|
@ -105,7 +105,7 @@ func (c *Cache) readFromCacheDB(addr string) (*Location, error) {
|
|||
return &result, nil
|
||||
}
|
||||
|
||||
func (c *Cache) Put(addr string, loc *Location) error {
|
||||
func (c *Cache) Put(addr string, loc *Address) error {
|
||||
if c.config.Geo.IPCacheType == types.CacheTypeDB {
|
||||
return c.putToCacheDB(loc)
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ func (c *Cache) Put(addr string, loc *Location) error {
|
|||
return c.putToCacheFile(addr, loc)
|
||||
}
|
||||
|
||||
func (c *Cache) putToCacheDB(loc *Location) error {
|
||||
func (c *Cache) putToCacheDB(loc *Address) error {
|
||||
err := c.db.Insert(loc).Do()
|
||||
// it should work like this:
|
||||
//
|
||||
|
@ -140,9 +140,9 @@ func (c *Cache) cacheFile(addr string) string {
|
|||
return path.Join(c.config.Geo.IPCache, addr)
|
||||
}
|
||||
|
||||
// NewLocationFromString parses the location cache entry s,
|
||||
// NewAddressFromString parses the location cache entry s,
|
||||
// and return location, or error, if the cache entry is invalid.
|
||||
func NewLocationFromString(addr, s string) (*Location, error) {
|
||||
func NewAddressFromString(addr, s string) (*Address, error) {
|
||||
var (
|
||||
lat float64 = -1000
|
||||
long float64 = -1000
|
||||
|
@ -166,7 +166,7 @@ func NewLocationFromString(addr, s string) (*Location, error) {
|
|||
}
|
||||
}
|
||||
|
||||
return &Location{
|
||||
return &Address{
|
||||
IP: addr,
|
||||
CountryCode: parts[0],
|
||||
Country: parts[1],
|
||||
|
@ -207,7 +207,7 @@ func (c *Cache) Response(r *http.Request) *routing.Cadre {
|
|||
return respERR
|
||||
}
|
||||
|
||||
location, err := NewLocationFromString(ip, value)
|
||||
location, err := NewAddressFromString(ip, value)
|
||||
if err != nil {
|
||||
return respERR
|
||||
}
|
||||
|
|
|
@ -15,13 +15,13 @@ func TestParseCacheEntry(t *testing.T) {
|
|||
tests := []struct {
|
||||
addr string
|
||||
input string
|
||||
expected Location
|
||||
expected Address
|
||||
err error
|
||||
}{
|
||||
{
|
||||
"1.2.3.4",
|
||||
"DE;Germany;Free and Hanseatic City of Hamburg;Hamburg;53.5736;9.9782",
|
||||
Location{
|
||||
Address{
|
||||
IP: "1.2.3.4",
|
||||
CountryCode: "DE",
|
||||
Country: "Germany",
|
||||
|
@ -36,7 +36,7 @@ func TestParseCacheEntry(t *testing.T) {
|
|||
{
|
||||
"1.2.3.4",
|
||||
"ES;Spain;Madrid, Comunidad de;Madrid;40.4165;-3.70256;28223;Orange Espagne SA;orange.es",
|
||||
Location{
|
||||
Address{
|
||||
IP: "1.2.3.4",
|
||||
CountryCode: "ES",
|
||||
Country: "Spain",
|
||||
|
@ -51,7 +51,7 @@ func TestParseCacheEntry(t *testing.T) {
|
|||
{
|
||||
"1.2.3.4",
|
||||
"US;United States of America;California;Mountain View",
|
||||
Location{
|
||||
Address{
|
||||
IP: "1.2.3.4",
|
||||
CountryCode: "US",
|
||||
Country: "United States of America",
|
||||
|
@ -67,13 +67,13 @@ func TestParseCacheEntry(t *testing.T) {
|
|||
{
|
||||
"1.2.3.4",
|
||||
"DE;Germany;Free and Hanseatic City of Hamburg;Hamburg;53.5736;XXX",
|
||||
Location{},
|
||||
Address{},
|
||||
types.ErrInvalidCacheEntry,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
result, err := NewLocationFromString(tt.addr, tt.input)
|
||||
result, err := NewAddressFromString(tt.addr, tt.input)
|
||||
if tt.err == nil {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, *result, tt.expected)
|
||||
|
|
Loading…
Reference in a new issue