diff --git a/.gitignore b/.gitignore index 71975ac..e943f04 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +bindata.go youp0m diff --git a/main.go b/main.go index d727a97..2e7821e 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "path/filepath" ) +var mux = http.NewServeMux() + var PublishedImgDir string var NextImgDir string var ThumbsDir string @@ -61,12 +63,9 @@ func main() { } } - mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, filepath.Join(staticDir, "index.html")) + serveStaticAsset(w, r, "index.html") }) - mux.Handle("/css/", http.FileServer(http.Dir(staticDir))) - mux.Handle("/js/", http.FileServer(http.Dir(staticDir))) mux.Handle("/api/", http.StripPrefix("/api", ApiHandler(authFunc))) @@ -101,7 +100,7 @@ func main() { w.Header().Set("WWW-Authenticate", "Basic realm=\"YouP0m\"") http.Error(w, "You are not allowed to perform this request.", http.StatusUnauthorized) } else { - http.ServeFile(w, r, filepath.Join(staticDir, "admin.html")) + serveStaticAsset(w, r, "admin.html") } }) diff --git a/static-dev.go b/static-dev.go new file mode 100644 index 0000000..36215fa --- /dev/null +++ b/static-dev.go @@ -0,0 +1,39 @@ +// +build dev + +package main + +import ( + "flag" + "net/http" + "os" + "path/filepath" +) + +var StaticDir string = "static/" + +func init() { + mux.Handle("/css/", http.FileServer(http.Dir(StaticDir))) + mux.Handle("/js/", http.FileServer(http.Dir(StaticDir))) + + flag.StringVar(&StaticDir, "static", StaticDir, "Directory containing static files") +} + +func serveStaticAsset(w http.ResponseWriter, r *http.Request, url string) { + if url == "index.html" { + r.URL.Path = "/" + } else { + r.URL.Path = "/" + url + } + http.FileServer(http.Dir(StaticDir)).ServeHTTP(w, r) +} + +func sanitizeStaticOptions() error { + StaticDir, _ := filepath.Abs("static") + if _, err := os.Stat(StaticDir); os.IsNotExist(err) { + StaticDir, _ = filepath.Abs(filepath.Join(filepath.Dir(os.Args[0]), "static")) + if _, err := os.Stat(StaticDir); os.IsNotExist(err) { + return err + } + } + return nil +} diff --git a/static.go b/static.go new file mode 100644 index 0000000..b4340fe --- /dev/null +++ b/static.go @@ -0,0 +1,36 @@ +// +build !dev + +package main + +import ( + "net/http" + "path" +) + +//go:generate go-bindata -ignore "\\.go|\\.less" -pkg "main" -o "bindata.go" static/... +//go:generate go fmt bindata.go + +const StaticDir string = "./static/" + +func init() { + mux.HandleFunc("/css/", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/css") + serveStaticAsset(w, r, r.URL.Path) + }) + mux.HandleFunc("/js/", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/javascript") + serveStaticAsset(w, r, r.URL.Path) + }) +} + +func serveStaticAsset(w http.ResponseWriter, r *http.Request, url string) { + if data, err := Asset(path.Join(StaticDir, url)); err != nil { + http.NotFound(w, r) + } else { + w.Write(data) + } +} + +func sanitizeStaticOptions() error { + return nil +}