From c55e859bd93016f7246ea62984f7eaa02a325736 Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Wed, 5 Jun 2019 13:52:59 +0200 Subject: Updated font handling --- gopherproxy.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'gopherproxy.go') diff --git a/gopherproxy.go b/gopherproxy.go index dc849d0..82215c5 100644 --- a/gopherproxy.go +++ b/gopherproxy.go @@ -14,6 +14,8 @@ import ( "github.com/temoto/robotstxt" "github.com/prologic/go-gopher" + + "github.com/gobuffalo/packr" ) type Item struct { @@ -164,6 +166,23 @@ func RobotsTxtHandler(robotstxtdata []byte) http.HandlerFunc { } } +func FontHandler(woff2 bool, fontdata []byte) http.HandlerFunc { + return func(w http.ResponseWriter, req *http.Request) { + if fontdata == nil { + http.Error(w, "Not Found", http.StatusNotFound) + return + } + + if woff2 { + w.Header().Set("Content-Type", "font/woff2") + } else { + w.Header().Set("Content-Type", "font/woff") + } + + w.Write(fontdata) + } +} + // ListenAndServe creates a listening HTTP server bound to // the interface specified by bind and sets up a Gopher to HTTP // proxy proxying requests as requested and by default will prozy @@ -194,6 +213,10 @@ func ListenAndServe(bind, robotsfile, uri string) error { styletext = string(styledata) } + box := packr.NewBox("./assets") + fontdataw, err := box.Find("iosevka-term-ss03-regular.woff") + fontdataw2, err := box.Find("iosevka-term-ss03-regular.woff2") + tpldata, err := ioutil.ReadFile(".template") if err == nil { tpltext = string(tpldata) @@ -212,7 +235,9 @@ func ListenAndServe(bind, robotsfile, uri string) error { http.HandleFunc("/", GopherHandler(tpl, robotsdata, styletext, uri)) http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) - // http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/")))) + http.HandleFunc("/iosevka-term-ss03-regular.woff", FontHandler(false, fontdataw)) + http.HandleFunc("/iosevka-term-ss03-regular.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