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"
"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 {
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()

View File

@ -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 {

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{
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete},
AllowMethods: []string{http.MethodGet},
}))
builtTemplates, _ := templates.BuildTemplates(templatefiles)