43 lines
809 B
Go
43 lines
809 B
Go
package db_updater
|
|
|
|
import (
|
|
"github.com/go-resty/resty/v2"
|
|
"github.com/rs/zerolog/log"
|
|
"net/http"
|
|
"path"
|
|
"thuanle.me/ip-info/configs"
|
|
)
|
|
|
|
var etags = map[string]string{}
|
|
|
|
func download(url string) bool {
|
|
log.Info().Str("url", url).Msg("Downloading DB")
|
|
filename := configs.GeoDbFolder + path.Base(url)
|
|
|
|
client := resty.New()
|
|
resp, err := client.R().
|
|
SetOutput(filename).
|
|
SetHeader("If-None-Match", etags[url]).
|
|
Get(url)
|
|
|
|
if err != nil {
|
|
log.Err(err).
|
|
Str("url", url).
|
|
Msg("Failed to fetch DB")
|
|
return false
|
|
}
|
|
|
|
if resp.StatusCode() == http.StatusOK {
|
|
etags[url] = resp.Header().Get("Etag")
|
|
} else {
|
|
//log.Printf("No update needed: %v", resp.Status())
|
|
return false
|
|
}
|
|
|
|
log.Info().
|
|
Str("Etag", etags[url]).
|
|
Str("filename", filename).
|
|
Msg("Downloaded DB")
|
|
return true
|
|
}
|