updated dip

* added async memcache actions
* update cors methods
This commit is contained in:
Paul 2024-05-11 18:21:21 +02:00
parent fa74f7ad4c
commit 715eedf4bf
4 changed files with 18 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"net" "net"
"time"
"git.paulbsd.com/paulbsd/dip/src/geoip" "git.paulbsd.com/paulbsd/dip/src/geoip"
"github.com/bradfitz/gomemcache/memcache" "github.com/bradfitz/gomemcache/memcache"
@ -35,6 +36,7 @@ func (ip *IP) GetIPInfo(c echo.Context) (cached bool, err error) {
var mc *memcache.Client var mc *memcache.Client
if MemcacheConn != "" { if MemcacheConn != "" {
mc = memcache.New(MemcacheConn) mc = memcache.New(MemcacheConn)
mc.Timeout, err = time.ParseDuration("1s")
mcenabled = true mcenabled = true
defer mc.Close() defer mc.Close()
} }
@ -48,7 +50,7 @@ func (ip *IP) GetIPInfo(c echo.Context) (cached bool, err error) {
if mcenabled { if mcenabled {
item, err := mc.Get(ip.IP) item, err := mc.Get(ip.IP)
if err != nil { if err != nil {
log.Println(ip.IP, err) log.Println("error from cache", ip.IP, err)
} else { } else {
cached = true cached = true
cachedip := IP{} cachedip := IP{}
@ -81,11 +83,13 @@ func (ip *IP) GetIPInfo(c echo.Context) (cached bool, err error) {
} }
if mcenabled { if mcenabled {
dt, err := json.Marshal(*ip) go func() {
err = mc.Set(&memcache.Item{Key: ip.IP, Value: dt, Expiration: cacheMaxTime}) dt, err := json.Marshal(*ip)
if err != nil { err = mc.Set(&memcache.Item{Key: ip.IP, Value: dt, Expiration: cacheMaxTime})
log.Println(err) if err != nil {
} log.Println(err)
}
}()
} }
/*err = ip.GetWhois() /*err = ip.GetWhois()

View File

@ -13,8 +13,11 @@ import (
geoip2 "github.com/oschwald/geoip2-golang" geoip2 "github.com/oschwald/geoip2-golang"
) )
var RootURL = "https://git.paulbsd.com/paulbsd/GeoLite.mmdb/releases/download/%s/%s" const RootURL = "https://git.paulbsd.com/paulbsd/GeoLite.mmdb/releases/download/%s/%s"
var APIUrl = "https://git.paulbsd.com/api/v1/repos/paulbsd/GeoLite.mmdb/releases" const APIUrl = "https://git.paulbsd.com/api/v1/repos/paulbsd/GeoLite.mmdb/releases"
const CityFilename = "GeoLite2-City.mmdb"
const ASNFilename = "GeoLite2-ASN.mmdb"
func GetLastVersion() string { func GetLastVersion() string {
var apiresults []struct { var apiresults []struct {
@ -45,8 +48,8 @@ func GetLastVersion() string {
func InitGeoIP() (citydb *geoip2.Reader, asndb *geoip2.Reader, err error) { func InitGeoIP() (citydb *geoip2.Reader, asndb *geoip2.Reader, err error) {
var version = GetLastVersion() var version = GetLastVersion()
var dbs map[string]string = map[string]string{ var dbs map[string]string = map[string]string{
"city": fmt.Sprintf(RootURL, version, "GeoLite2-City.mmdb"), "city": fmt.Sprintf(RootURL, version, CityFilename),
"asn": fmt.Sprintf(RootURL, version, "GeoLite2-ASN.mmdb")} "asn": fmt.Sprintf(RootURL, version, ASNFilename)}
log.Printf("Fetching GeoLite.mmdb version %s\n", version) log.Printf("Fetching GeoLite.mmdb version %s\n", version)
citydb, err = FetchDB(dbs["city"], version) citydb, err = FetchDB(dbs["city"], version)
if err != nil { if err != nil {

View File

@ -22,7 +22,7 @@ func (ws *WS) InitServer(templatefiles *embed.FS, staticfiles *embed.FS) (err er
})) }))
ws.e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ ws.e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"}, AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete}, AllowMethods: []string{http.MethodGet},
})) }))
builtTemplates, _ := templates.BuildTemplates(templatefiles) builtTemplates, _ := templates.BuildTemplates(templatefiles)