s3browser-backend/internal/httpserver/staticFiles.go

34 lines
610 B
Go
Raw Permalink Normal View History

2021-09-03 21:23:06 +00:00
//go:build prod
// +build prod
2021-09-24 13:39:23 +00:00
package httpserver
2021-09-03 21:23:06 +00:00
import (
"embed"
2021-11-22 01:26:08 +00:00
"github.com/gorilla/mux"
2021-09-03 21:23:06 +00:00
"io/fs"
"net/http"
"os"
)
// content holds our static web server content.
//go:embed static
var staticFiles embed.FS
type spaFileSystem struct {
root http.FileSystem
}
func (spa *spaFileSystem) Open(name string) (http.File, error) {
f, err := spa.root.Open(name)
if os.IsNotExist(err) {
return spa.root.Open("index.html")
}
return f, err
}
2021-11-22 01:26:08 +00:00
func initStatic(r *mux.Router) {
2021-09-03 21:23:06 +00:00
staticFS, _ := fs.Sub(staticFiles, "static")
2021-11-22 01:26:08 +00:00
r.PathPrefix("/").Handler(http.FileServer(&spaFileSystem{http.FS(staticFS)}))
2021-09-03 21:23:06 +00:00
}