feature (dev): server static file from fs in dev mode

This commit is contained in:
Mickael Kerjean 2022-11-23 15:19:18 +11:00
parent 7d5719445c
commit ac7bf9f173

View file

@ -5,6 +5,7 @@ import (
"fmt"
. "github.com/mickael-kerjean/filestash/server/common"
"io"
"io/fs"
"net/http"
URL "net/url"
"os"
@ -15,6 +16,7 @@ import (
)
var (
WWWDir fs.FS
//go:embed static/www
WWWEmbed embed.FS
@ -22,6 +24,10 @@ var (
HtmlPage404 []byte
)
func init() {
WWWDir = os.DirFS(GetAbsolutePath("../"))
}
func StaticHandler(_path string) func(*App, http.ResponseWriter, *http.Request) {
return func(ctx *App, res http.ResponseWriter, req *http.Request) {
var chroot string = GetAbsolutePath(_path)
@ -245,6 +251,9 @@ func ServeFile(res http.ResponseWriter, req *http.Request, filePath string) {
}
curPath := filePath + cfg.FileExt
file, err := WWWEmbed.Open("static/www" + curPath)
if os.Getenv("NODE_ENV") != "" {
file, err = WWWDir.Open("server/ctrl/static/www" + curPath)
}
if err != nil {
continue
}