Files
dev-service/web/embed.go
2025-06-21 23:32:51 -04:00

54 lines
980 B
Go

package web
import (
"embed"
"fmt"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"io/fs"
"net/http"
"strings"
)
//go:embed dist/*
var staticFS embed.FS
func AddRoutes(router gin.IRouter) {
embeddedBuildFolder := newStaticFileSystem()
router.Use(static.Serve("/", embeddedBuildFolder))
}
type staticFileSystem struct {
http.FileSystem
}
var _ static.ServeFileSystem = (*staticFileSystem)(nil)
func newStaticFileSystem() *staticFileSystem {
sub, err := fs.Sub(staticFS, "dist")
if err != nil {
panic(err)
}
return &staticFileSystem{
FileSystem: http.FS(sub),
}
}
func (s *staticFileSystem) Exists(prefix string, path string) bool {
buildpath := fmt.Sprintf("dist%s", path)
// support for folders
if strings.HasSuffix(path, "/") {
_, err := staticFS.ReadDir(strings.TrimSuffix(buildpath, "/"))
return err == nil
}
// support for files
f, err := staticFS.Open(buildpath)
if f != nil {
_ = f.Close()
}
return err == nil
}