mirror of
https://github.com/dcarrillo/whatismyip.git
synced 2025-07-01 19:49:27 +00:00
first commit
This commit is contained in:
34
service/geo.go
Normal file
34
service/geo.go
Normal file
@ -0,0 +1,34 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"github.com/dcarrillo/whatismyip/models"
|
||||
)
|
||||
|
||||
type Geo struct {
|
||||
IP net.IP
|
||||
}
|
||||
|
||||
func (g *Geo) LookUpCity() *models.GeoRecord {
|
||||
record := &models.GeoRecord{}
|
||||
err := record.LookUp(g.IP)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return record
|
||||
}
|
||||
|
||||
func (g *Geo) LookUpASN() *models.ASNRecord {
|
||||
record := &models.ASNRecord{}
|
||||
err := record.LookUp(g.IP)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return record
|
||||
}
|
36
service/geo_test.go
Normal file
36
service/geo_test.go
Normal file
@ -0,0 +1,36 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/dcarrillo/whatismyip/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
models.Setup("../test/GeoIP2-City-Test.mmdb", "../test/GeoLite2-ASN-Test.mmdb")
|
||||
defer models.CloseDBs()
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func TestCityLookup(t *testing.T) {
|
||||
ip := Geo{IP: net.ParseIP("error")}
|
||||
c := ip.LookUpCity()
|
||||
assert.Nil(t, c)
|
||||
|
||||
ip = Geo{IP: net.ParseIP("1.1.1.1")}
|
||||
c = ip.LookUpCity()
|
||||
assert.NotNil(t, c)
|
||||
}
|
||||
|
||||
func TestASNLookup(t *testing.T) {
|
||||
ip := Geo{IP: net.ParseIP("error")}
|
||||
a := ip.LookUpASN()
|
||||
assert.Nil(t, a)
|
||||
|
||||
ip = Geo{IP: net.ParseIP("1.1.1.1")}
|
||||
a = ip.LookUpASN()
|
||||
assert.NotNil(t, a)
|
||||
}
|
Reference in New Issue
Block a user