From ccdc998836038ba2fb8bb72d417a0aa09648ad89 Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Thu, 27 Jun 2019 15:34:16 +0200 Subject: Serve assets as own files, use caching strategies --- gopherproxy.go | 78 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 24 deletions(-) (limited to 'gopherproxy.go') diff --git a/gopherproxy.go b/gopherproxy.go index 62b7446..1fcd29f 100644 --- a/gopherproxy.go +++ b/gopherproxy.go @@ -2,6 +2,7 @@ package gopherproxy import ( "bytes" + "crypto/md5" "fmt" "html" "html/template" @@ -28,7 +29,14 @@ type Item struct { Text string } -func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext string, jstext string, uri string, hostport string, d gopher.Directory) error { +type AssetHashList struct { + Style string + JS string + FontW string + FontW2 string +} + +func renderDirectory(w http.ResponseWriter, tpl *template.Template, assetHashList AssetHashList, uri string, hostport string, d gopher.Directory) error { var title string out := make([]Item, len(d.Items)) @@ -78,13 +86,12 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext st } return tpl.Execute(w, struct { - Title string - URI string - Style string - Script string - Lines []Item - RawText string - }{title, fmt.Sprintf("%s/%s", hostport, uri), styletext, jstext, out, ""}) + Title string + URI string + AssetHashList AssetHashList + Lines []Item + RawText string + }{title, fmt.Sprintf("%s/%s", hostport, uri), assetHashList, out, ""}) } // GopherHandler returns a Handler that proxies requests @@ -92,7 +99,7 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext st // to the request path and renders the content using the provided template. // The optional robots parameters points to a robotstxt.RobotsData struct // to test user agents against a configurable robotst.txt file. -func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, robotsdebug bool, styletext string, jstext string, uri string) http.HandlerFunc { +func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, assetHashList AssetHashList, robotsdebug bool, uri string) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { agent := req.UserAgent() path := strings.TrimPrefix(req.URL.Path, "/") @@ -141,13 +148,12 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, rob buf := new(bytes.Buffer) buf.ReadFrom(res.Body) tpl.Execute(w, struct { - Title string - URI string - Style string - Script string - RawText string - Lines []Item - }{uri, fmt.Sprintf("%s/%s", hostport, uri), styletext, jstext, buf.String(), nil}) + Title string + URI string + AssetHashList AssetHashList + RawText string + Lines []Item + }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetHashList, buf.String(), nil}) } else if parts[1] == "T" { _, _, err = vips.NewTransform(). Load(res.Body). @@ -160,7 +166,7 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, rob io.Copy(w, res.Body) } } else { - if err := renderDirectory(w, tpl, styletext, jstext, uri, hostport, res.Dir); err != nil { + if err := renderDirectory(w, tpl, assetHashList, uri, hostport, res.Dir); err != nil { io.WriteString(w, fmt.Sprintf("Error:
%s
", err)) return } @@ -190,10 +196,27 @@ func FaviconHandler(favicondata []byte) http.HandlerFunc { } w.Header().Set("Content-Type", "image/vnd.microsoft.icon") + w.Header().Set("Cache-Control", "max-age=2592000") w.Write(favicondata) } } +func StyleHandler(styledata []byte) http.HandlerFunc { + return func(w http.ResponseWriter, req *http.Request) { + w.Header().Set("Content-Type", "text/css") + w.Header().Set("Cache-Control", "max-age=2592000") + w.Write(styledata) + } +} + +func JavaScriptHandler(jsdata []byte) http.HandlerFunc { + return func(w http.ResponseWriter, req *http.Request) { + w.Header().Set("Content-Type", "text/javascript") + w.Header().Set("Cache-Control", "max-age=2592000") + w.Write(jsdata) + } +} + func FontHandler(woff2 bool, fontdata []byte) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { if fontdata == nil { @@ -206,6 +229,7 @@ func FontHandler(woff2 bool, fontdata []byte) http.HandlerFunc { } else { w.Header().Set("Content-Type", "font/woff") } + w.Header().Set("Cache-Control", "max-age=2592000") w.Write(fontdata) } @@ -242,21 +266,25 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, uri string) error if err != nil { fontdataw = []byte{} } + fontwhash := fmt.Sprintf("%x", md5.Sum(fontdataw)) fontdataw2, err := box.Find("iosevka-term-ss03-regular.woff2") if err != nil { fontdataw2 = []byte{} } + fontw2hash := fmt.Sprintf("%x", md5.Sum(fontdataw2)) - styletext, err := box.FindString("style.css") + styledata, err := box.Find("style.css") if err != nil { - styletext = "" + styledata = []byte{} } + stylehash := fmt.Sprintf("%x", md5.Sum(styledata)) - jstext, err := box.FindString("main.js") + jsdata, err := box.Find("main.js") if err != nil { - jstext = "" + jsdata = []byte{} } + jshash := fmt.Sprintf("%x", md5.Sum(jsdata)) favicondata, err := box.Find("favicon.ico") if err != nil { @@ -304,11 +332,13 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, uri string) error ConcurrencyLevel: 2, }) - http.HandleFunc("/", GopherHandler(tpl, robotsdata, robotsdebug, styletext, jstext, uri)) + http.HandleFunc("/", GopherHandler(tpl, robotsdata, AssetHashList{stylehash, jshash, fontwhash, fontw2hash}, robotsdebug, uri)) http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) http.HandleFunc("/favicon.ico", FaviconHandler(favicondata)) - http.HandleFunc("/iosevka-term-ss03-regular.woff", FontHandler(false, fontdataw)) - http.HandleFunc("/iosevka-term-ss03-regular.woff2", FontHandler(true, fontdataw2)) + http.HandleFunc("/style-"+stylehash+".css", StyleHandler(styledata)) + http.HandleFunc("/main-"+jshash+".js", JavaScriptHandler(jsdata)) + http.HandleFunc("/iosevka-term-ss03-regular-"+fontwhash+".woff", FontHandler(false, fontdataw)) + http.HandleFunc("/iosevka-term-ss03-regular-"+fontw2hash+".woff2", FontHandler(true, fontdataw2)) //http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/")))) return http.ListenAndServe(bind, nil) -- cgit v1.2.3-54-g00ecf