token-validator: use embed instead of bindata

This commit is contained in:
nemunaire 2021-10-31 18:43:43 +01:00
parent 840d58e8b0
commit 34cd31b916
3 changed files with 60 additions and 149 deletions

View File

@ -1,2 +1 @@
token-validator
bindata.go

View File

@ -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
}

View File

@ -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)
}