// +build dev package main import ( "flag" "net/http" "os" "path" "path/filepath" "strconv" "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) { if _, err := strconv.ParseInt(string(ps.ByName("where")), 10, 64); err != nil { http.NotFound(w, r) } else { 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 }