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 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 package main
import ( import (
"fmt" "embed"
"flag"
"io/fs"
"log"
"net/http" "net/http"
"path"
"strings" "strings"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
//go:generate go-bindata -ignore "\\.go|\\.less" -pkg "main" -o "bindata.go" htdocs/... var (
//go:generate go fmt bindata.go //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() { 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) { Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/maatma.html"); err != nil { serveFile(w, r, "/maatma.html")
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
}) })
Router().GET("/auth", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { Router().GET("/auth", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/maatma.html"); err != nil { serveFile(w, r, "/maatma.html")
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
}) })
Router().GET("/domains", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { Router().GET("/domains", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/maatma.html"); err != nil { serveFile(w, r, "/maatma.html")
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
}) })
Router().GET("/tunnels", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { Router().GET("/tunnels", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/maatma.html"); err != nil { serveFile(w, r, "/maatma.html")
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
}) })
Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "text/css") serveFile(w, r, r.URL.Path)
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) { 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 { serveFile(w, r, r.URL.Path)
http.NotFound(w, r)
} else {
w.Write(data)
}
}) })
Router().GET("/img/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { 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 { serveFile(w, r, r.URL.Path)
http.NotFound(w, r)
} else {
w.Write(data)
}
}) })
Router().GET("/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { Router().GET("/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "text/javascript") serveFile(w, r, r.URL.Path)
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) { Router().GET("/views/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "text/html") serveFile(w, r, r.URL.Path)
if data, err := Asset(path.Join("htdocs", r.URL.Path)); err != nil {
http.NotFound(w, r)
} else {
w.Write(data)
}
}) })
Router().GET("/dashboard/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { Router().GET("/dashboard/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/dashboard.html"); err != nil { serveFile(w, r, "/dashboard.html")
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
}) })
Router().GET("/dashboard/:where", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { Router().GET("/dashboard/:where", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/dashboard.html"); err != nil { serveFile(w, r, "/dashboard.html")
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
}) })
Router().GET("/dashboard/:where/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { Router().GET("/dashboard/:where/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if ps.ByName("where") == "css" { if ps.ByName("where") == "css" {
w.Header().Set("Content-Type", "text/css") w.Header().Set("Content-Type", "text/css")
if data, err := Asset(path.Join("htdocs", strings.TrimPrefix(r.URL.Path, "/dashboard"))); err != nil { serveFile(w, r, strings.TrimPrefix(r.URL.Path, "/dashboard"))
http.NotFound(w, r)
} else {
w.Write(data)
}
} else if ps.ByName("where") == "js" { } else if ps.ByName("where") == "js" {
w.Header().Set("Content-Type", "text/javascript") w.Header().Set("Content-Type", "text/javascript")
if data, err := Asset(path.Join("htdocs", strings.TrimPrefix(r.URL.Path, "/dashboard"))); err != nil { serveFile(w, r, strings.TrimPrefix(r.URL.Path, "/dashboard"))
http.NotFound(w, r)
} else {
w.Write(data)
}
} else { } else {
http.NotFound(w, r) http.NotFound(w, r)
} }