etag write to file

handle query before db loaded
This commit is contained in:
thuanle
2024-07-14 00:48:58 +07:00
parent 12603bbf6c
commit eaeb32b421
4 changed files with 39 additions and 7 deletions

View File

@@ -4,12 +4,11 @@ import (
"github.com/go-resty/resty/v2"
"github.com/rs/zerolog/log"
"net/http"
"os"
"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)
@@ -17,7 +16,7 @@ func download(url string) bool {
client := resty.New()
resp, err := client.R().
SetOutput(filename).
SetHeader("If-None-Match", etags[url]).
SetHeader("If-None-Match", readETag(filename)).
Get(url)
if err != nil {
@@ -28,15 +27,31 @@ func download(url string) bool {
}
if resp.StatusCode() == http.StatusOK {
etags[url] = resp.Header().Get("Etag")
writeETag(filename, 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
}
// readETag reads the ETag from the file
func readETag(baseFile string) string {
data, err := os.ReadFile(baseFile + ".etag")
if err != nil {
return ""
}
return string(data)
}
// writeETag writes the ETag to the file
func writeETag(baseFile, etag string) {
err := os.WriteFile(baseFile+".etag", []byte(etag), 0644)
if err != nil {
log.Err(err).Msg("Failed to write ETag")
}
}