From dfcde910b2dfad60c37f5a9525ef944396df8559 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Fri, 1 Oct 2021 17:52:44 +0200 Subject: [PATCH] Use go embed --- .drone.yml | 12 ------ assets-dev.go | 32 ++++++++++++++ assets.go | 28 ++++++++++++ go.mod | 2 +- static-dev.go | 71 ------------------------------ static.go | 117 +++++++++----------------------------------------- 6 files changed, 81 insertions(+), 181 deletions(-) create mode 100644 assets-dev.go create mode 100644 assets.go delete mode 100644 static-dev.go diff --git a/.drone.yml b/.drone.yml index 380e090..24d0b89 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,12 +8,6 @@ platform: arch: arm steps: -- name: generate frontend - image: golang:alpine - commands: - - apk --no-cache add go-bindata - - go generate -v - - name: vet image: golang:alpine commands: @@ -50,12 +44,6 @@ platform: arch: arm64 steps: -- name: generate frontend - image: golang:alpine - commands: - - apk --no-cache add go-bindata - - go generate -v - - name: vet image: golang:alpine commands: diff --git a/assets-dev.go b/assets-dev.go new file mode 100644 index 0000000..a23cd48 --- /dev/null +++ b/assets-dev.go @@ -0,0 +1,32 @@ +//go:build dev +// +build dev + +package main + +import ( + "flag" + "net/http" + "os" + "path/filepath" +) + +var ( + Assets http.FileSystem + StaticDir string = "htdocs/" +) + +func init() { + flag.StringVar(&StaticDir, "static", StaticDir, "Directory containing static files") +} + +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 + } + } + Assets = http.Dir(StaticDir) + return nil +} diff --git a/assets.go b/assets.go new file mode 100644 index 0000000..125f49e --- /dev/null +++ b/assets.go @@ -0,0 +1,28 @@ +//go:build !dev +// +build !dev + +package main + +import ( + "embed" + "io/fs" + "log" + "net/http" +) + +//go:embed htdocs +var _assets embed.FS + +var Assets http.FileSystem + +func init() { + sub, err := fs.Sub(_assets, "htdocs") + if err != nil { + log.Fatal("Unable to cd to htdocs/ directory:", err) + } + Assets = http.FS(sub) +} + +func sanitizeStaticOptions() error { + return nil +} diff --git a/go.mod b/go.mod index c920f12..31d9853 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module git.nemunai.re/atsebay.t -go 1.15 +go 1.16 require ( github.com/coreos/go-oidc v2.2.1+incompatible diff --git a/static-dev.go b/static-dev.go deleted file mode 100644 index 94fe79f..0000000 --- a/static-dev.go +++ /dev/null @@ -1,71 +0,0 @@ -// +build dev - -package main - -import ( - "flag" - "net/http" - "os" - "path" - "path/filepath" - - "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, "index.html")) - }) - Router().GET("/auth", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - http.ServeFile(w, r, path.Join(StaticDir, "index.html")) - }) - Router().GET("/grades", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - http.ServeFile(w, r, path.Join(StaticDir, "index.html")) - }) - Router().GET("/surveys", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - http.ServeFile(w, r, path.Join(StaticDir, "index.html")) - }) - Router().GET("/surveys/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - http.ServeFile(w, r, path.Join(StaticDir, "index.html")) - }) - Router().GET("/users", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - http.ServeFile(w, r, path.Join(StaticDir, "index.html")) - }) - Router().GET("/users/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - http.ServeFile(w, r, path.Join(StaticDir, "index.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) - }) - - 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/static.go b/static.go index c1910c4..0f24406 100644 --- a/static.go +++ b/static.go @@ -1,108 +1,31 @@ -// +build !dev - package main import ( - "fmt" "net/http" - "path" "github.com/julienschmidt/httprouter" ) -//go:generate go-bindata -ignore "\\.go|\\.less" -pkg "main" -o "bindata.go" htdocs/... -//go:generate go fmt bindata.go +func serveOrReverse(forced_url string) func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + if forced_url != "" { + r.URL.Path = forced_url + } + http.FileServer(Assets).ServeHTTP(w, r) + } +} func init() { - Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/index.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } - }) - Router().GET("/auth", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/index.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } - }) - Router().GET("/grades", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/index.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } - }) - Router().GET("/surveys", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/index.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } - }) - Router().GET("/surveys/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/index.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } - }) - Router().GET("/users", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/index.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } - }) - Router().GET("/users/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - if data, err := Asset("htdocs/index.html"); err != nil { - fmt.Fprintf(w, "{\"errmsg\":%q}", err) - } else { - w.Write(data) - } - }) - 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) - } - }) - 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) - } - }) - 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) - } - }) - 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) - } - }) - 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) - } - }) -} - -func sanitizeStaticOptions() error { - return nil + Router().GET("/", serveOrReverse("")) + Router().GET("/auth", serveOrReverse("/")) + Router().GET("/grades", serveOrReverse("/")) + Router().GET("/surveys", serveOrReverse("/")) + Router().GET("/surveys/*_", serveOrReverse("/")) + Router().GET("/users", serveOrReverse("/")) + Router().GET("/users/*_", serveOrReverse("/")) + Router().GET("/css/*_", serveOrReverse("")) + Router().GET("/fonts/*_", serveOrReverse("")) + Router().GET("/img/*_", serveOrReverse("")) + Router().GET("/js/*_", serveOrReverse("")) + Router().GET("/views/*_", serveOrReverse("")) }