token-validator: add dev tag to not embedded static files into binary

This commit is contained in:
nemunaire 2020-02-27 15:07:27 +01:00
parent 2f162ac6df
commit a0d4b431bc
3 changed files with 106 additions and 3 deletions

View file

@ -6,12 +6,10 @@ import (
"net/http"
"net/url"
"path"
"path/filepath"
"strings"
)
var sharedSecret string
var StaticDir string
type ResponseWriterPrefix struct {
real http.ResponseWriter
@ -66,7 +64,7 @@ func main() {
// Sanitize options
var err error
log.Println("Checking paths...")
if StaticDir, err = filepath.Abs(StaticDir); err != nil {
if err = sanitizeStaticOptions(); err != nil {
log.Fatal(err)
}
if *baseURL != "/" {

View file

@ -0,0 +1,99 @@
// +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, "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)
})
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/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/dashboard")
serveStaticAsset(w, r)
})
Router().GET("/dashboard/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/dashboard")
serveStaticAsset(w, r)
})
Router().GET("/maatma/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "maatma.html"))
})
Router().GET("/maatma/auth", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "maatma.html"))
})
Router().GET("/maatma/domains", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "maatma.html"))
})
Router().GET("/maatma/tunnels", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "maatma.html"))
})
Router().GET("/maatma/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/maatma")
serveStaticAsset(w, r)
})
Router().GET("/maatma/fonts/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/maatma")
serveStaticAsset(w, r)
})
Router().GET("/maatma/img/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/maatma")
serveStaticAsset(w, r)
})
Router().GET("/maatma/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/maatma")
serveStaticAsset(w, r)
})
Router().GET("/maatma/views/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/maatma")
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,3 +1,5 @@
// +build !dev
package main
import (
@ -143,3 +145,7 @@ func init() {
}
})
}
func sanitizeStaticOptions() error {
return nil
}