diff options
author | Adam Mathes <adam@trenchant.org> | 2018-06-12 16:59:04 -0700 |
---|---|---|
committer | Adam Mathes <adam@trenchant.org> | 2018-06-12 16:59:04 -0700 |
commit | f06192a98a2f8e8dae3148cda59b6b57968160ae (patch) | |
tree | 86ced9e042563f9a218819b4099fb05b3b5de119 /web/web.go | |
parent | c2e10f09732ff96a7b185aa4e0a8896750d5c32d (diff) | |
download | neko-f06192a98a2f8e8dae3148cda59b6b57968160ae.tar.gz neko-f06192a98a2f8e8dae3148cda59b6b57968160ae.tar.bz2 neko-f06192a98a2f8e8dae3148cda59b6b57968160ae.zip |
remove runtime static file dependencies, use rice boxes
Diffstat (limited to 'web/web.go')
-rw-r--r-- | web/web.go | 31 |
1 files changed, 17 insertions, 14 deletions
@@ -8,17 +8,17 @@ import ( "encoding/base64" "encoding/json" "fmt" + "github.com/GeertJohan/go.rice" "golang.org/x/crypto/bcrypt" "io/ioutil" "log" "net/http" - "path" "strconv" "time" ) func indexHandler(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, staticFilePath("ui.html")) + serveBoxedFile(w, r, "ui.html") } func streamHandler(w http.ResponseWriter, r *http.Request) { @@ -218,7 +218,7 @@ var SecondsInAYear = 60 * 60 * 24 * 365 func loginHandler(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": - http.ServeFile(w, r, staticFilePath("login.html")) + serveBoxedFile(w, r, "login.html") case "POST": password := r.FormValue("password") if password == config.Config.DigestPassword { @@ -262,15 +262,24 @@ func AuthWrap(wrapped http.HandlerFunc) http.HandlerFunc { } } +func serveBoxedFile(w http.ResponseWriter, r *http.Request, filename string) { + box := rice.MustFindBox("../static") + ui, err := box.Open(filename) + if err != nil { + panic(err) + } + fi, _ := ui.Stat() + http.ServeContent(w, r, filename, fi.ModTime(), ui) +} + func Serve() { - fs := http.FileServer(http.Dir(config.Config.StaticDir)) - http.Handle("/static/", http.StripPrefix("/static/", fs)) - http.HandleFunc("/stream/", AuthWrap(streamHandler)) + box := rice.MustFindBox("../static") + staticFileServer := http.StripPrefix("/static/", http.FileServer(box.HTTPBox())) + http.Handle("/static/", staticFileServer) - // http.HandleFunc("/item/", AuthWrap(itemHandler)) + http.HandleFunc("/stream/", AuthWrap(streamHandler)) http.Handle("/item/", http.StripPrefix("/item/", AuthWrap(itemHandler))) - http.HandleFunc("/feed/", AuthWrap(feedHandler)) http.HandleFunc("/tag/", AuthWrap(categoryHandler)) http.Handle("/image/", http.StripPrefix("/image/", AuthWrap(imageProxyHandler))) @@ -282,9 +291,3 @@ func Serve() { http.ListenAndServe(config.Config.WebServer, nil) } - -// given a path, prepend config.Config.StaticDir -// TODO: compile these into the binary to remove dependency on the files -func staticFilePath(p string) string { - return path.Join(config.Config.StaticDir, p) -} |