From d0b3c432bdc80796f8854651b2822662a3ccef70 Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Mon, 11 Nov 2019 18:58:24 +0100 Subject: Replaced UglifyJS with Terser, include whole asset path in struct, display errors in template instead of basic HTML --- gopherproxy.go | 63 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 20 deletions(-) (limited to 'gopherproxy.go') diff --git a/gopherproxy.go b/gopherproxy.go index 6490b28..0b3279c 100644 --- a/gopherproxy.go +++ b/gopherproxy.go @@ -29,14 +29,14 @@ type Item struct { Text string } -type AssetHashList struct { +type AssetList 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 { +func renderDirectory(w http.ResponseWriter, tpl *template.Template, assetList AssetList, uri string, hostport string, d gopher.Directory) error { var title string out := make([]Item, len(d.Items)) @@ -88,10 +88,11 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, assetHashLis return tpl.Execute(w, struct { Title string URI string - AssetHashList AssetHashList + Assets AssetList Lines []Item RawText string - }{title, fmt.Sprintf("%s/%s", hostport, uri), assetHashList, out, ""}) + Error bool + }{title, fmt.Sprintf("%s/%s", hostport, uri), assetList, out, "", false}) } // GopherHandler returns a Handler that proxies requests @@ -99,7 +100,7 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, assetHashLis // 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, assetHashList AssetHashList, robotsdebug bool, uri string) http.HandlerFunc { +func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, assetList AssetList, robotsdebug bool, uri string) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { agent := req.UserAgent() path := strings.TrimPrefix(req.URL.Path, "/") @@ -124,7 +125,14 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass uri, err := url.QueryUnescape(strings.Join(parts[1:], "/")) if err != nil { - io.WriteString(w, fmt.Sprintf("Error:
%s
", err)) + tpl.Execute(w, struct { + Title string + URI string + Assets AssetList + RawText string + Lines []Item + Error bool + }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error: %s", err), nil, true}) return } @@ -138,7 +146,14 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass ) if err != nil { - io.WriteString(w, fmt.Sprintf("Error:
%s
", err)) + tpl.Execute(w, struct { + Title string + URI string + Assets AssetList + RawText string + Lines []Item + Error bool + }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error: %s", err), nil, true}) return } @@ -152,10 +167,11 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass tpl.Execute(w, struct { Title string URI string - AssetHashList AssetHashList + Assets AssetList RawText string Lines []Item - }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetHashList, buf.String(), nil}) + Error bool + }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetList, buf.String(), nil, false}) } else if parts[1] == "T" { _, _, err = vips.NewTransform(). Load(res.Body). @@ -168,8 +184,15 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass io.Copy(w, res.Body) } } else { - if err := renderDirectory(w, tpl, assetHashList, uri, hostport, res.Dir); err != nil { - io.WriteString(w, fmt.Sprintf("Error:
%s
", err)) + if err := renderDirectory(w, tpl, assetList, uri, hostport, res.Dir); err != nil { + tpl.Execute(w, struct { + Title string + URI string + Assets AssetList + RawText string + Lines []Item + Error bool + }{uri, fmt.Sprintf("%s/%s", hostport, uri), assetList, fmt.Sprintf("Error: %s", err), nil, true}) return } } @@ -268,25 +291,25 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, vipsconcurrency i if err != nil { fontdataw = []byte{} } - fontwhash := fmt.Sprintf("%x", md5.Sum(fontdataw)) + fontwAsset := fmt.Sprintf("/iosevka-term-ss03-regular-%x.woff", md5.Sum(fontdataw)) fontdataw2, err := box.Find("iosevka-term-ss03-regular.woff2") if err != nil { fontdataw2 = []byte{} } - fontw2hash := fmt.Sprintf("%x", md5.Sum(fontdataw2)) + fontw2Asset := fmt.Sprintf("/iosevka-term-ss03-regular-%x.woff2", md5.Sum(fontdataw2)) styledata, err := box.Find("style.css") if err != nil { styledata = []byte{} } - stylehash := fmt.Sprintf("%x", md5.Sum(styledata)) + styleAsset := fmt.Sprintf("/style-%x.css", md5.Sum(styledata)) jsdata, err := box.Find("main.js") if err != nil { jsdata = []byte{} } - jshash := fmt.Sprintf("%x", md5.Sum(jsdata)) + jsAsset := fmt.Sprintf("/main-%x.js", md5.Sum(jsdata)) favicondata, err := box.Find("favicon.ico") if err != nil { @@ -334,13 +357,13 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, vipsconcurrency i ConcurrencyLevel: vipsconcurrency, }) - http.HandleFunc("/", GopherHandler(tpl, robotsdata, AssetHashList{stylehash, jshash, fontwhash, fontw2hash}, robotsdebug, uri)) + http.HandleFunc("/", GopherHandler(tpl, robotsdata, AssetList{styleAsset, jsAsset, fontwAsset, fontw2Asset}, robotsdebug, uri)) http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) http.HandleFunc("/favicon.ico", FaviconHandler(favicondata)) - 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.HandleFunc(styleAsset, StyleHandler(styledata)) + http.HandleFunc(jsAsset, JavaScriptHandler(jsdata)) + http.HandleFunc(fontwAsset, FontHandler(false, fontdataw)) + http.HandleFunc(fontw2Asset, 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