first commit
This commit is contained in:
42
internal/services/db_updater/downloader.go
Normal file
42
internal/services/db_updater/downloader.go
Normal file
@@ -0,0 +1,42 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user