From 34cd31b91656698ae56d732cdd81807bc8639545 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 31 Oct 2021 18:43:43 +0100 Subject: [PATCH] token-validator: use embed instead of bindata --- token-validator/.gitignore | 1 - token-validator/static-dev.go | 74 ------------------- token-validator/static.go | 134 +++++++++++++++------------------- 3 files changed, 60 insertions(+), 149 deletions(-) delete mode 100644 token-validator/static-dev.go diff --git a/token-validator/.gitignore b/token-validator/.gitignore index f645e2a..b0b676c 100644 --- a/token-validator/.gitignore +++ b/token-validator/.gitignore @@ -1,2 +1 @@ token-validator -bindata.go diff --git a/token-validator/static-dev.go b/token-validator/static-dev.go deleted file mode 100644 index d461ea3..0000000 --- a/token-validator/static-dev.go +++ /dev/null @@ -1,74 +0,0 @@ -// +build dev - -package main - -import ( - "flag" - "net/http" - "os" - "path" - "path/filepath" - "strings" - - "github.com/julienschmidt/httprouter" -) - -var StaticDir string = "htdocs/" - -func init() { - Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - http.ServeFile(w, r, path.Join(StaticDir, "maatma.html")) - }) - Router().GET("/auth", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - http.ServeFile(w, r, path.Join(StaticDir, "maatma.html")) - }) - Router().GET("/domains", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - http.ServeFile(w, r, path.Join(StaticDir, "maatma.html")) - }) - Router().GET("/tunnels", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - http.ServeFile(w, r, path.Join(StaticDir, "maatma.html")) - }) - Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - serveStaticAsset(w, r) - }) - Router().GET("/fonts/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - serveStaticAsset(w, r) - }) - Router().GET("/img/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - serveStaticAsset(w, r) - }) - Router().GET("/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - serveStaticAsset(w, r) - }) - Router().GET("/views/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - serveStaticAsset(w, r) - }) - - Router().GET("/dashboard/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - http.ServeFile(w, r, path.Join(StaticDir, "dashboard.html")) - }) - Router().GET("/dashboard/:where", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - http.ServeFile(w, r, path.Join(StaticDir, "dashboard.html")) - }) - Router().GET("/dashboard/:where/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - r.URL.Path = strings.TrimPrefix(r.URL.Path, "/dashboard") - serveStaticAsset(w, r) - }) - - flag.StringVar(&StaticDir, "static", StaticDir, "Directory containing static files") -} - -func serveStaticAsset(w http.ResponseWriter, r *http.Request) { - http.StripPrefix("/", http.FileServer(http.Dir(StaticDir))).ServeHTTP(w, r) -} - -func sanitizeStaticOptions() error { - StaticDir, _ := filepath.Abs(StaticDir) - if _, err := os.Stat(StaticDir); os.IsNotExist(err) { - StaticDir, _ = filepath.Abs(filepath.Join(filepath.Dir(os.Args[0]), "htdocs")) - if _, err := os.Stat(StaticDir); os.IsNotExist(err) { - return err - } - } - return nil -} diff --git a/token-validator/static.go b/token-validator/static.go index 1ca9931..a73dcbf 100644 --- a/token-validator/static.go +++ b/token-validator/static.go @@ -1,116 +1,102 @@ -// +build !dev - package main import ( - "fmt" + "embed" + "flag" + "io/fs" + "log" "net/http" - "path" "strings" "github.com/julienschmidt/httprouter" ) -//go:generate go-bindata -ignore "\\.go|\\.less" -pkg "main" -o "bindata.go" htdocs/... -//go:generate go fmt bindata.go +var ( + //go:embed htdocs + assets embed.FS + staticFS staticDevDir +) + +type staticDevDir struct { + fs *http.FileSystem + Path *string +} + +func (i *staticDevDir) String() string { + if i.Path == nil { + return "embeded" + } + return *i.Path +} + +func (i *staticDevDir) Set(value string) error { + return nil +} + +func (i *staticDevDir) GetFS() *http.FileSystem { + if i.fs == nil { + if i.Path != nil { + v := http.FileSystem(http.Dir(*i.Path)) + i.fs = &v + } else if sub, err := fs.Sub(assets, "htdocs"); err != nil { + log.Fatal("Unable to cd to htdocs/ directory in embeded FS:", err) + } else { + v := http.FS(sub) + i.fs = &v + } + } + return i.fs +} + +func serveFile(w http.ResponseWriter, r *http.Request, url string) { + r.URL.Path = url + http.FileServer(*staticFS.GetFS()).ServeHTTP(w, r) +} func init() { + flag.Var(&staticFS, "static", "Directory containing static files (default if not provided: use embedded files)") + Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/maatma.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } + serveFile(w, r, "/maatma.html") }) Router().GET("/auth", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/maatma.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } + serveFile(w, r, "/maatma.html") }) Router().GET("/domains", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/maatma.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } + serveFile(w, r, "/maatma.html") }) Router().GET("/tunnels", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/maatma.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } + serveFile(w, r, "/maatma.html") }) Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - w.Header().Set("Content-Type", "text/css") - if data, err := Asset(path.Join("htdocs", r.URL.Path)); err != nil { - http.NotFound(w, r) - } else { - w.Write(data) - } + serveFile(w, r, r.URL.Path) }) Router().GET("/fonts/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset(path.Join("htdocs", r.URL.Path)); err != nil { - http.NotFound(w, r) - } else { - w.Write(data) - } + serveFile(w, r, r.URL.Path) }) Router().GET("/img/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset(path.Join("htdocs", r.URL.Path)); err != nil { - http.NotFound(w, r) - } else { - w.Write(data) - } + serveFile(w, r, r.URL.Path) }) Router().GET("/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - w.Header().Set("Content-Type", "text/javascript") - if data, err := Asset(path.Join("htdocs", r.URL.Path)); err != nil { - http.NotFound(w, r) - } else { - w.Write(data) - } + serveFile(w, r, r.URL.Path) }) Router().GET("/views/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - w.Header().Set("Content-Type", "text/html") - if data, err := Asset(path.Join("htdocs", r.URL.Path)); err != nil { - http.NotFound(w, r) - } else { - w.Write(data) - } + serveFile(w, r, r.URL.Path) }) Router().GET("/dashboard/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/dashboard.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } + serveFile(w, r, "/dashboard.html") }) Router().GET("/dashboard/:where", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/dashboard.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } + serveFile(w, r, "/dashboard.html") }) Router().GET("/dashboard/:where/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { if ps.ByName("where") == "css" { w.Header().Set("Content-Type", "text/css") - if data, err := Asset(path.Join("htdocs", strings.TrimPrefix(r.URL.Path, "/dashboard"))); err != nil { - http.NotFound(w, r) - } else { - w.Write(data) - } + serveFile(w, r, strings.TrimPrefix(r.URL.Path, "/dashboard")) } else if ps.ByName("where") == "js" { w.Header().Set("Content-Type", "text/javascript") - if data, err := Asset(path.Join("htdocs", strings.TrimPrefix(r.URL.Path, "/dashboard"))); err != nil { - http.NotFound(w, r) - } else { - w.Write(data) - } + serveFile(w, r, strings.TrimPrefix(r.URL.Path, "/dashboard")) } else { http.NotFound(w, r) }