Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
190 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// Copyright 2013-2014 Alexandre Fiori | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
) | ||
|
||
type Cache struct { | ||
Country map[string]string | ||
Region map[RegionKey]string | ||
City map[int]Location | ||
} | ||
|
||
type RegionKey struct { | ||
CountryCode, | ||
RegionCode *string | ||
} | ||
|
||
type Location struct { | ||
CountryCode, | ||
RegionCode, | ||
CityName, | ||
ZipCode string | ||
Latitude, | ||
Longitude float32 | ||
MetroCode, | ||
AreaCode string | ||
} | ||
|
||
func NewCache(db *sql.DB) *Cache { | ||
cache := &Cache{ | ||
Country: make(map[string]string), | ||
Region: make(map[RegionKey]string), | ||
City: make(map[int]Location), | ||
} | ||
|
||
var ( | ||
row *sql.Rows | ||
err error | ||
) | ||
|
||
// Load list of countries. | ||
if row, err = db.Query(` | ||
SELECT | ||
country_code, | ||
country_name | ||
FROM | ||
country_blocks | ||
`); err != nil { | ||
log.Fatal("Failed to load countries from db:", err) | ||
} | ||
|
||
var country_code, region_code, name string | ||
for row.Next() { | ||
if err = row.Scan( | ||
&country_code, | ||
&name, | ||
); err != nil { | ||
log.Fatal("Failed to load country from db:", err) | ||
} | ||
|
||
cache.Country[country_code] = name | ||
} | ||
|
||
row.Close() | ||
|
||
// Load list of regions. | ||
if row, err = db.Query(` | ||
SELECT | ||
country_code, | ||
region_code, | ||
region_name | ||
FROM | ||
region_names | ||
`); err != nil { | ||
log.Fatal("Failed to load regions from db:", err) | ||
} | ||
|
||
for row.Next() { | ||
if err = row.Scan( | ||
&country_code, | ||
®ion_code, | ||
&name, | ||
); err != nil { | ||
log.Fatal("Failed to load region from db:", err) | ||
} | ||
|
||
cache.Region[RegionKey{&country_code, ®ion_code}] = name | ||
} | ||
|
||
row.Close() | ||
|
||
// Load list of city locations. | ||
if row, err = db.Query("SELECT * FROM city_location"); err != nil { | ||
log.Fatal("Failed to load cities from db:", err) | ||
} | ||
|
||
var ( | ||
locId int | ||
loc Location | ||
) | ||
|
||
for row.Next() { | ||
if err = row.Scan( | ||
&locId, | ||
&loc.CountryCode, | ||
&loc.RegionCode, | ||
&loc.CityName, | ||
&loc.ZipCode, | ||
&loc.Latitude, | ||
&loc.Longitude, | ||
&loc.MetroCode, | ||
&loc.AreaCode, | ||
); err != nil { | ||
log.Fatal("Failed to load city from db:", err) | ||
} | ||
|
||
cache.City[locId] = loc | ||
} | ||
|
||
row.Close() | ||
|
||
return cache | ||
} | ||
|
||
func (cache *Cache) Update(geoip *GeoIP, locId int) { | ||
city, ok := cache.City[locId] | ||
if !ok { | ||
return | ||
} | ||
|
||
geoip.CountryName = cache.Country[city.CountryCode] | ||
geoip.RegionName = cache.Region[RegionKey{ | ||
&city.CountryCode, | ||
&city.RegionCode, | ||
}] | ||
geoip.CityName = &city.CityName | ||
geoip.ZipCode = &city.ZipCode | ||
geoip.Latitude = &city.Latitude | ||
geoip.Longitude = &city.Longitude | ||
geoip.MetroCode = &city.MetroCode | ||
geoip.AreaCode = &city.AreaCode | ||
} |
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