From eaf02771d767e4745572d9b00e71e138ee030e60 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Thu, 4 Mar 2021 17:54:03 -0500 Subject: Switch go go 1.16 and use Embed --- cmd/web/main.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'cmd/web/main.go') diff --git a/cmd/web/main.go b/cmd/web/main.go index d66e366..091d22f 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -2,6 +2,7 @@ package main import ( "flag" + "io" "fmt" "log" "math/rand" @@ -11,6 +12,7 @@ import ( "strings" "sync" "time" + "embed" "github.com/gorilla/mux" "riedstra.dev/mitch/steam-export/steam" @@ -26,6 +28,9 @@ var ( Logger = log.New(os.Stderr, "", log.LstdFlags) Listen = ":8899" + //go:embed static/* + embeddedStatic embed.FS + Lib steamLib ) @@ -60,6 +65,33 @@ func setLibHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/", 302) } +func staticHandler(w http.ResponseWriter, r *http.Request){ + /* + vars := mux.Vars(r) + + fn, ok := vars["fn"] + if !ok { + http.Error(w, "Not found", http.StatusNotFound) + return + } + */ + + fn := r.URL.Path + + fh, err := embeddedStatic.Open(fn) + if err != nil { + Logger.Printf("While reading embedded file: %s", err) + http.Error(w, "Not found", http.StatusNotFound) + return + } + defer fh.Close() + + _, err = io.Copy(w, fh) + if err != nil { + Logger.Printf("While sending static file: %s", err) + } +} + func quitHandler(w http.ResponseWriter, r *http.Request) { if unauthorizedIfNotLocal(w, r) { return @@ -163,8 +195,8 @@ func main() { r.HandleFunc("/steam-export-web.exe", serveSelf) r.HandleFunc("/download/{game}", gameDownloader) r.HandleFunc("/status", statsHandler) - r.HandleFunc("/style.css", cssHandler) - r.HandleFunc("/main.js", jsHandler) + r.PathPrefix("/static").Handler( + http.FileServer(http.FS(embeddedStatic))) r.HandleFunc("/", index) s := http.Server{ -- cgit v1.2.3