In production mode, embed static content
This commit is contained in:
parent
d5fd91902f
commit
3dd22ecb09
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
bindata.go
|
||||
youp0m
|
||||
|
9
main.go
9
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")
|
||||
}
|
||||
})
|
||||
|
||||
|
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
|
||||
}
|
36
static.go
Normal file
36
static.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user