first commit

This commit is contained in:
2021-11-10 20:06:12 +01:00
commit 2fcfa91545
29 changed files with 2724 additions and 0 deletions

34
service/geo.go Normal file
View 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
View 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)
}