From 280f1a038b5d28a4497db9dc825eea440b45dfc7 Mon Sep 17 00:00:00 2001 From: blek Date: Sun, 5 Nov 2023 15:05:30 +1000 Subject: [PATCH] cached proxied resources --- resource/main.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/resource/main.go b/resource/main.go index c0a4002..513c349 100644 --- a/resource/main.go +++ b/resource/main.go @@ -45,6 +45,25 @@ func (self Config) Validate() int { return 0 } +func (self Resource) GetProxied() ([]byte, error) { + + cached, exists := ProxyResourceCache[self.Url]; + if exists { + return cached, nil + } + + resp, err := http.Get(self.Url) + if err != nil { return make([]byte, 0, 0), err } + + buf, err := ioutil.ReadAll(resp.Body) + if err != nil { return make([]byte, 0, 0), err } + + ProxyResourceCache[self.Url] = buf + + return buf, nil +} + +var ProxyResourceCache map[string][]byte = make(map[string][]byte) func main() { var conf Config @@ -82,19 +101,15 @@ func main() { if ! strings.HasPrefix(res.Url, "file://") { if res.Proxied { - resp, err := http.Get(res.Url) + data, err := res.GetProxied() if err != nil { log.Fatalln(err) + // we failed, send a redirect instead + // (next executed line would be 115) } else { c.Response().Header.SetContentType(res.Mime) - // c.Response().Header.SetContentLength(resp.ContentLength) - buf, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatalln(err) - return c.Status(500).SendString("Internal error") - } - - return c.Send(buf) + c.Response().Header.SetContentLength(len(data)) + return c.Send(data) } }