diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2021-03-04 17:54:03 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2021-03-04 17:54:03 -0500 |
| commit | eaf02771d767e4745572d9b00e71e138ee030e60 (patch) | |
| tree | fd3ac8cbe984344e6cc76d80e26ff963d8139d87 /cmd/web/main.go | |
| parent | 17d0e3a4ef3ec0abf609a3bfe86e0429bd789583 (diff) | |
| download | steam-export-eaf02771d767e4745572d9b00e71e138ee030e60.tar.gz steam-export-eaf02771d767e4745572d9b00e71e138ee030e60.tar.xz | |
Switch go go 1.16 and use Embed
Diffstat (limited to 'cmd/web/main.go')
| -rw-r--r-- | cmd/web/main.go | 36 |
1 files changed, 34 insertions, 2 deletions
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{ |
