In production mode, embed static content

This commit is contained in:
nemunaire 2019-09-02 15:41:37 +02:00
parent d5fd91902f
commit 3dd22ecb09
4 changed files with 80 additions and 5 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
bindata.go
youp0m youp0m

View File

@ -8,6 +8,8 @@ import (
"path/filepath" "path/filepath"
) )
var mux = http.NewServeMux()
var PublishedImgDir string var PublishedImgDir string
var NextImgDir string var NextImgDir string
var ThumbsDir string var ThumbsDir string
@ -61,12 +63,9 @@ func main() {
} }
} }
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 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))) mux.Handle("/api/", http.StripPrefix("/api", ApiHandler(authFunc)))
@ -101,7 +100,7 @@ func main() {
w.Header().Set("WWW-Authenticate", "Basic realm=\"YouP0m\"") w.Header().Set("WWW-Authenticate", "Basic realm=\"YouP0m\"")
http.Error(w, "You are not allowed to perform this request.", http.StatusUnauthorized) http.Error(w, "You are not allowed to perform this request.", http.StatusUnauthorized)
} else { } else {
http.ServeFile(w, r, filepath.Join(staticDir, "admin.html")) serveStaticAsset(w, r, "admin.html")
} }
}) })

39
static-dev.go Normal file
View File

@ -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
}

36
static.go Normal file
View File

@ -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
}