This repository has been archived on 2024-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
adlin/token-validator/static-dev.go

75 lines
2.4 KiB
Go

// +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
}