summaryrefslogtreecommitdiff
path: root/cmd/generate/httpcache.go
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-04-15 12:06:51 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-04-15 12:06:51 -0600
commit8b336e06f15263310eb82345fb2778414ed76879 (patch)
tree9dd1b2fbc1f79a8bd7de151cc9d38912695dd069 /cmd/generate/httpcache.go
parent2d25eb7f7bb2d091c0c1d243f5a9bd1c7ff98ea1 (diff)
persist the httpcache to the filesystem
Diffstat (limited to 'cmd/generate/httpcache.go')
-rw-r--r--cmd/generate/httpcache.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/cmd/generate/httpcache.go b/cmd/generate/httpcache.go
index 87d2fb5..cff0c7c 100644
--- a/cmd/generate/httpcache.go
+++ b/cmd/generate/httpcache.go
@@ -5,6 +5,9 @@ import (
"fmt"
"io"
"net/http"
+ "net/url"
+ "os"
+ "path/filepath"
)
var httpCache = map[string]string{}
@@ -13,6 +16,17 @@ func httpGet(u string) (string, error) {
if cache, ok := httpCache[u]; ok {
return cache, nil
}
+ if err := os.Mkdir(".http-cache", 0777); err != nil && !os.IsExist(err) {
+ return "", err
+ }
+ cacheFile := filepath.Join(".http-cache", url.QueryEscape(u))
+ if bs, err := os.ReadFile(cacheFile); err == nil {
+ httpCache[u] = string(bs)
+ return httpCache[u], nil
+ } else if !os.IsNotExist(err) {
+ return "", err
+ }
+
fmt.Printf("GET %q...", u)
resp, err := http.Get(u)
if err != nil {
@@ -29,6 +43,9 @@ func httpGet(u string) (string, error) {
return "", err
}
fmt.Printf(" ok\n")
+ if err := os.WriteFile(cacheFile, bs, 0666); err != nil {
+ return "", err
+ }
httpCache[u] = string(bs)
return httpCache[u], nil
}