From 715eedf4bf6f2c7fa89e578c46acc9bd7de543da Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 11 May 2024 18:21:21 +0200 Subject: [PATCH] updated dip * added async memcache actions * update cors methods --- scripts/{docker.sh => kube_deploy.sh} | 0 src/dip/main.go | 16 ++++++++++------ src/geoip/main.go | 11 +++++++---- src/ws/main.go | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) rename scripts/{docker.sh => kube_deploy.sh} (100%) diff --git a/scripts/docker.sh b/scripts/kube_deploy.sh similarity index 100% rename from scripts/docker.sh rename to scripts/kube_deploy.sh diff --git a/src/dip/main.go b/src/dip/main.go index f00ab2b..a175ce0 100644 --- a/src/dip/main.go +++ b/src/dip/main.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "net" + "time" "git.paulbsd.com/paulbsd/dip/src/geoip" "github.com/bradfitz/gomemcache/memcache" @@ -35,6 +36,7 @@ func (ip *IP) GetIPInfo(c echo.Context) (cached bool, err error) { var mc *memcache.Client if MemcacheConn != "" { mc = memcache.New(MemcacheConn) + mc.Timeout, err = time.ParseDuration("1s") mcenabled = true defer mc.Close() } @@ -48,7 +50,7 @@ func (ip *IP) GetIPInfo(c echo.Context) (cached bool, err error) { if mcenabled { item, err := mc.Get(ip.IP) if err != nil { - log.Println(ip.IP, err) + log.Println("error from cache", ip.IP, err) } else { cached = true cachedip := IP{} @@ -81,11 +83,13 @@ func (ip *IP) GetIPInfo(c echo.Context) (cached bool, err error) { } if mcenabled { - dt, err := json.Marshal(*ip) - err = mc.Set(&memcache.Item{Key: ip.IP, Value: dt, Expiration: cacheMaxTime}) - if err != nil { - log.Println(err) - } + go func() { + dt, err := json.Marshal(*ip) + err = mc.Set(&memcache.Item{Key: ip.IP, Value: dt, Expiration: cacheMaxTime}) + if err != nil { + log.Println(err) + } + }() } /*err = ip.GetWhois() diff --git a/src/geoip/main.go b/src/geoip/main.go index 12bbbda..37313b6 100644 --- a/src/geoip/main.go +++ b/src/geoip/main.go @@ -13,8 +13,11 @@ import ( geoip2 "github.com/oschwald/geoip2-golang" ) -var 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 RootURL = "https://git.paulbsd.com/paulbsd/GeoLite.mmdb/releases/download/%s/%s" +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 { var apiresults []struct { @@ -45,8 +48,8 @@ func GetLastVersion() string { func InitGeoIP() (citydb *geoip2.Reader, asndb *geoip2.Reader, err error) { var version = GetLastVersion() var dbs map[string]string = map[string]string{ - "city": fmt.Sprintf(RootURL, version, "GeoLite2-City.mmdb"), - "asn": fmt.Sprintf(RootURL, version, "GeoLite2-ASN.mmdb")} + "city": fmt.Sprintf(RootURL, version, CityFilename), + "asn": fmt.Sprintf(RootURL, version, ASNFilename)} log.Printf("Fetching GeoLite.mmdb version %s\n", version) citydb, err = FetchDB(dbs["city"], version) if err != nil { diff --git a/src/ws/main.go b/src/ws/main.go index b230f17..c006741 100644 --- a/src/ws/main.go +++ b/src/ws/main.go @@ -22,7 +22,7 @@ func (ws *WS) InitServer(templatefiles *embed.FS, staticfiles *embed.FS) (err er })) ws.e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ AllowOrigins: []string{"*"}, - AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete}, + AllowMethods: []string{http.MethodGet}, })) builtTemplates, _ := templates.BuildTemplates(templatefiles)