In production mode, embed static content
This commit is contained in:
parent
d5fd91902f
commit
3dd22ecb09
4 changed files with 80 additions and 5 deletions
39
static-dev.go
Normal file
39
static-dev.go
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue