diff --git a/.gitignore b/.gitignore index 82ab2189..61375e19 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ postgres-data node_modules .mypy_cache data/ -dist \ No newline at end of file +dist +!tunnel/internal/client/dashboard/dist \ No newline at end of file diff --git a/tunnel/internal/client/dashboard/dashboard.go b/tunnel/internal/client/dashboard/dashboard.go index 4e0510d8..02a6557f 100644 --- a/tunnel/internal/client/dashboard/dashboard.go +++ b/tunnel/internal/client/dashboard/dashboard.go @@ -2,6 +2,7 @@ package dashboard import ( "context" + "embed" "errors" "fmt" "log/slog" @@ -15,7 +16,6 @@ import ( "github.com/amalshaji/portr/internal/client/dashboard/ui/dist" "github.com/amalshaji/portr/internal/client/db" "github.com/amalshaji/portr/internal/client/vite" - "github.com/amalshaji/portr/internal/constants" "github.com/amalshaji/portr/internal/utils" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/filesystem" @@ -31,8 +31,18 @@ type Dashboard struct { port int } +//go:embed templates +var templatesFS embed.FS + func New(db *db.Db, config *config.Config) *Dashboard { - engine := django.New("./internal/client/dashboard/templates", ".html") + var engine *django.Engine + + if config.UseVite { + engine = django.New("./internal/client/dashboard/templates", ".html") + } else { + engine = django.NewPathForwardingFileSystem(http.FS(templatesFS), "/templates", ".html") + } + engine.SetAutoEscape(false) app := fiber.New(fiber.Config{ @@ -43,20 +53,22 @@ func New(db *db.Db, config *config.Config) *Dashboard { app.Use(recover.New()) if config.UseVite { - app.Static("/", "./internal/server/admin/web/dist") app.Static("/static", "./internal/client/dashboard/static") } else { app.Use("/static", filesystem.New(filesystem.Config{ - Root: http.FS(dist.EmbededDirStatic), + Root: http.FS(dist.EmbeddedDirStatic), PathPrefix: "static", })) } rootTemplateView := func(c *fiber.Ctx) error { - return c.Render("index", fiber.Map{ - "UseVite": config.UseVite, - "ViteTags": vite.GenerateViteTags(constants.ClientUiViteDistDir), - }) + context := fiber.Map{ + "UseVite": config.UseVite, + } + if !config.UseVite { + context["ViteTags"] = vite.GenerateViteTags(dist.ManifestString) + } + return c.Render("index", context) } service := service.New(db, config) diff --git a/tunnel/internal/client/dashboard/templates/templates.go b/tunnel/internal/client/dashboard/templates/templates.go new file mode 100644 index 00000000..fdee2063 --- /dev/null +++ b/tunnel/internal/client/dashboard/templates/templates.go @@ -0,0 +1,6 @@ +package templates + +import "embed" + +//go:embed index.html +var IndexTemplate embed.FS diff --git a/tunnel/internal/client/vite/vite.go b/tunnel/internal/client/vite/vite.go index 06889a59..a686449e 100644 --- a/tunnel/internal/client/vite/vite.go +++ b/tunnel/internal/client/vite/vite.go @@ -3,7 +3,6 @@ package vite import ( "encoding/json" "log" - "os" ) type manifest struct { @@ -15,14 +14,9 @@ type manifest struct { } `json:"index.html"` } -func GenerateViteTags(manifestPath string) string { - manifestFileContents, err := os.ReadFile(manifestPath) - if err != nil { - log.Fatal(err) - } - +func GenerateViteTags(manifestString string) string { var manifest manifest - if err := json.Unmarshal(manifestFileContents, &manifest); err != nil { + if err := json.Unmarshal([]byte(manifestString), &manifest); err != nil { log.Fatal(err) }