Skip to content

Commit

Permalink
embed assets
Browse files Browse the repository at this point in the history
  • Loading branch information
amalshaji committed Mar 20, 2024
1 parent 9a4645c commit 15f5357
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ postgres-data
node_modules
.mypy_cache
data/
dist
dist
!tunnel/internal/client/dashboard/dist
28 changes: 20 additions & 8 deletions tunnel/internal/client/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dashboard

import (
"context"
"embed"
"errors"
"fmt"
"log/slog"
Expand All @@ -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"
Expand All @@ -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{
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions tunnel/internal/client/dashboard/templates/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package templates

import "embed"

//go:embed index.html
var IndexTemplate embed.FS
10 changes: 2 additions & 8 deletions tunnel/internal/client/vite/vite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vite
import (
"encoding/json"
"log"
"os"
)

type manifest struct {
Expand All @@ -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)
}

Expand Down

0 comments on commit 15f5357

Please sign in to comment.