Skip to content

Modern Docker Management, Designed for Everyone

refactor: serve application fonts from the backend embedded fs (#1106)

Kyle Mendellef922590273a
diff --git a/backend/internal/bootstrap/router_bootstrap.go b/backend/internal/bootstrap/router_bootstrap.go
index 57eb6141d..45fede9f6 100644
--- a/backend/internal/bootstrap/router_bootstrap.go
+++ b/backend/internal/bootstrap/router_bootstrap.go
@@ -37,7 +37,9 @@ func setupRouter(cfg *config.Config, appServices *Services) *gin.Engine {
"GET /api/environments/*/ws/system/stats",
"GET /_app/*",
"GET /img",
- "GET /fonts",
+ "GET /api/fonts/sans",
+ "GET /api/fonts/mono",
+ "GET /api/fonts/serif",
"GET /api/health",
"HEAD /api/health",
}
@@ -94,6 +96,7 @@ func setupRouter(cfg *config.Config, appServices *Services) *gin.Engine {
Oidc: appServices.Oidc,
ApiKey: appServices.ApiKey,
AppImages: appServices.AppImages,
+ Font: appServices.Font,
Project: appServices.Project,
Event: appServices.Event,
Version: appServices.Version,
diff --git a/backend/internal/bootstrap/services_bootstrap.go b/backend/internal/bootstrap/services_bootstrap.go
index a965d6cf8..df9fbb7d4 100644
--- a/backend/internal/bootstrap/services_bootstrap.go
+++ b/backend/internal/bootstrap/services_bootstrap.go
@@ -37,6 +37,7 @@ type Services struct {
Notification *services.NotificationService
Apprise *services.AppriseService
ApiKey *services.ApiKeyService
+ Font *services.FontService
}
func initializeServices(ctx context.Context, db *database.DB, cfg *config.Config, httpClient *http.Client) (svcs *Services, dockerSrvice *services.DockerClientService, err error) {
@@ -50,6 +51,7 @@ func initializeServices(ctx context.Context, db *database.DB, cfg *config.Config
svcs.SettingsSearch = services.NewSettingsSearchService()
svcs.CustomizeSearch = services.NewCustomizeSearchService()
svcs.AppImages = services.NewApplicationImagesService(resources.FS, svcs.Settings)
+ svcs.Font = services.NewFontService(resources.FS)
dockerClient := services.NewDockerClientService(db, cfg)
svcs.Docker = dockerClient
svcs.User = services.NewUserService(db)
diff --git a/backend/internal/huma/handlers/fonts.go b/backend/internal/huma/handlers/fonts.go
new file mode 100644
index 000000000..5a97092a7
--- /dev/null
+++ b/backend/internal/huma/handlers/fonts.go
@@ -0,0 +1,112 @@
+package handlers
+
+import (
+ "context"
+ "net/http"
+
+ "github.com/danielgtaylor/huma/v2"
+ "github.com/getarcaneapp/arcane/backend/internal/services"
+)
+
+// FontsHandler provides Huma-based font endpoints.
+type FontsHandler struct {
+ fontService *services.FontService
+}
+
+// --- Huma Input/Output Wrappers ---
+
+type GetFontOutput struct {
+ ContentType string `header:"Content-Type"`
+ CacheControl string `header:"Cache-Control"`
+ Body []byte
+}
+
+// RegisterFonts registers font routes using Huma.
+func RegisterFonts(api huma.API, fontService *services.FontService) {
+ h := &FontsHandler{
+ fontService: fontService,
+ }
+
+ huma.Register(api, huma.Operation{
+ OperationID: "get-sans-font",
+ Method: http.MethodGet,
+ Path: "/fonts/sans",
+ Summary: "Get sans-serif font",
+ Description: "Get the application sans-serif font (Geist)",
+ Tags: []string{"Fonts"},
+ Security: []map[string][]string{}, // Public endpoint
+ }, h.GetSansFont)
+
+ huma.Register(api, huma.Operation{
+ OperationID: "get-mono-font",
+ Method: http.MethodGet,
+ Path: "/fonts/mono",
+ Summary: "Get monospace font",
+ Description: "Get the application monospace font (Geist Mono)",
+ Tags: []string{"Fonts"},
+ Security: []map[string][]string{}, // Public endpoint
+ }, h.GetMonoFont)
+
+ huma.Register(api, huma.Operation{
+ OperationID: "get-serif-font",
+ Method: http.MethodGet,
+ Path: "/fonts/serif",
+ Summary: "Get serif font",
+ Description: "Get the application serif font (Calistoga)",
+ Tags: []string{"Fonts"},
+ Security: []map[string][]string{}, // Public endpoint
+ }, h.GetSerifFont)
+}
+
+// GetSansFont returns the sans-serif font.
+func (h *FontsHandler) GetSansFont(ctx context.Context, input *struct{}) (*GetFontOutput, error) {
+ if h.fontService == nil {
+ return nil, huma.Error500InternalServerError("service not available")
+ }
+
+ data, mimeType, err := h.fontService.GetSansFont()
+ if err != nil {
+ return nil, huma.Error404NotFound("font not found")
+ }
+
+ return h.createFontResponse(data, mimeType), nil
+}
+
+// GetMonoFont returns the monospace font.
+func (h *FontsHandler) GetMonoFont(ctx context.Context, input *struct{}) (*GetFontOutput, error) {
+ if h.fontService == nil {
+ return nil, huma.Error500InternalServerError("service not available")
+ }
+
+ data, mimeType, err := h.fontService.GetMonoFont()
+ if err != nil {
+ return nil, huma.Error404NotFound("font not found")
+ }
+
+ return h.createFontResponse(data, mimeType), nil
+}
+
+// GetSerifFont returns the serif font.
+func (h *FontsHandler) GetSerifFont(ctx context.Context, input *struct{}) (*GetFontOutput, error) {
+ if h.fontService == nil {
+ return nil, huma.Error500InternalServerError("service not available")
+ }
+
+ data, mimeType, err := h.fontService.GetSerifFont()
+ if err != nil {
+ return nil, huma.Error404NotFound("font not found")
+ }
+
+ return h.createFontResponse(data, mimeType), nil
+}
+
+func (h *FontsHandler) createFontResponse(data []byte, mimeType string) *GetFontOutput {
+ // Cache for 1 year, immutable
+ cacheControl := "public, max-age=31536000, immutable"
+
+ return &GetFontOutput{
+ ContentType: mimeType,
+ CacheControl: cacheControl,
+ Body: data,
+ }
+}
diff --git a/backend/internal/huma/huma.go b/backend/internal/huma/huma.go
index a29f38485..fdf4a835a 100644
--- a/backend/internal/huma/huma.go
+++ b/backend/internal/huma/huma.go
@@ -119,6 +119,7 @@ type Services struct {
Oidc *services.OidcService
ApiKey *services.ApiKeyService
AppImages *services.ApplicationImagesService
+ Font *services.FontService
Project *services.ProjectService
Event *services.EventService
Version *services.VersionService
@@ -273,6 +274,7 @@ func registerHandlers(api huma.API, svc *Services) {
var oidcSvc *services.OidcService
var apiKeySvc *services.ApiKeyService
var appImagesSvc *services.ApplicationImagesService
+ var fontSvc *services.FontService
var projectSvc *services.ProjectService
var eventSvc *services.EventService
var versionSvc *services.VersionService
@@ -301,6 +303,7 @@ func registerHandlers(api huma.API, svc *Services) {
oidcSvc = svc.Oidc
apiKeySvc = svc.ApiKey
appImagesSvc = svc.AppImages
+ fontSvc = svc.Font
projectSvc = svc.Project
eventSvc = svc.Event
versionSvc = svc.Version
@@ -327,6 +330,7 @@ func registerHandlers(api huma.API, svc *Services) {
handlers.RegisterAuth(api, userSvc, authSvc, oidcSvc)
handlers.RegisterApiKeys(api, apiKeySvc)
handlers.RegisterAppImages(api, appImagesSvc)
+ handlers.RegisterFonts(api, fontSvc)
handlers.RegisterProjects(api, projectSvc)
handlers.RegisterUsers(api, userSvc)
handlers.RegisterVersion(api, versionSvc)
diff --git a/backend/internal/services/font_service.go b/backend/internal/services/font_service.go
new file mode 100644
index 000000000..27f44e2cd
--- /dev/null
+++ b/backend/internal/services/font_service.go
@@ -0,0 +1,69 @@
+package services
+
+import (
+ "embed"
+ "errors"
+ "fmt"
+ "io/fs"
+ "mime"
+ "path/filepath"
+ "strings"
+)
+
+type FontService struct {
+ fs embed.FS
+}
+
+func NewFontService(embeddedFS embed.FS) *FontService {
+ return &FontService{
+ fs: embeddedFS,
+ }
+}
+
+func (s *FontService) GetSansFont() ([]byte, string, error) {
+ return s.GetFont("Geist/geist.woff2")
+}
+
+func (s *FontService) GetMonoFont() ([]byte, string, error) {
+ return s.GetFont("Geist/geist-mono.woff2")
+}
+
+func (s *FontService) GetSerifFont() ([]byte, string, error) {
+ return s.GetFont("Calistoga/Calistoga-Regular.woff2")
+}
+
+func (s *FontService) GetFont(fontPath string) ([]byte, string, error) {
+ // Prevent directory traversal
+ if strings.Contains(fontPath, "..") {
+ return nil, "", fmt.Errorf("invalid font path")
+ }
+
+ // The fonts are located in "fonts" directory in the embedded FS
+ fullPath := filepath.Join("fonts", fontPath)
+
+ data, err := s.fs.ReadFile(fullPath)
+ if err != nil {
+ if errors.Is(err, fs.ErrNotExist) {
+ return nil, "", fmt.Errorf("font not found")
+ }
+ return nil, "", err
+ }
+
+ ext := filepath.Ext(fontPath)
+ mimeType := mime.TypeByExtension(ext)
+ if mimeType == "" {
+ // Fallback for common font types if mime package doesn't detect them
+ switch strings.ToLower(ext) {
+ case ".woff2":
+ mimeType = "font/woff2"
+ case ".ttf":
+ mimeType = "font/ttf"
+ case ".otf":
+ mimeType = "font/otf"
+ default:
+ mimeType = "application/octet-stream"
+ }
+ }
+
+ return data, mimeType, nil
+}
diff --git a/backend/resources/embed.go b/backend/resources/embed.go
index 48d373839..d2d103ac6 100644
--- a/backend/resources/embed.go
+++ b/backend/resources/embed.go
@@ -4,5 +4,5 @@ import "embed"
// Embedded file systems for the project
-//go:embed migrations images email-templates
+//go:embed migrations images email-templates fonts
var FS embed.FS
diff --git a/backend/resources/fonts/Calistoga/Calistoga-Regular.woff2 b/backend/resources/fonts/Calistoga/Calistoga-Regular.woff2
new file mode 100644
index 000000000..a3b8b16b2
Binary files /dev/null and b/backend/resources/fonts/Calistoga/Calistoga-Regular.woff2 differ
diff --git a/frontend/static/fonts/Geist/geist-mono.woff2 b/backend/resources/fonts/Geist/geist-mono.woff2
similarity index 100%
rename from frontend/static/fonts/Geist/geist-mono.woff2
rename to backend/resources/fonts/Geist/geist-mono.woff2
diff --git a/frontend/static/fonts/Geist/geist.woff2 b/backend/resources/fonts/Geist/geist.woff2
similarity index 100%
rename from frontend/static/fonts/Geist/geist.woff2
rename to backend/resources/fonts/Geist/geist.woff2
diff --git a/frontend/src/app.css b/frontend/src/app.css
index c794ce439..adab6130a 100644
--- a/frontend/src/app.css
+++ b/frontend/src/app.css
@@ -3,18 +3,22 @@
@font-face {
font-family: 'Geist';
- src: url('/fonts/Geist/geist.woff2') format('woff2');
- font-style: normal;
+ src: url('/api/fonts/sans') format('woff2');
font-display: swap;
}
@font-face {
font-family: 'Geist Mono';
- src: url('/fonts/Geist/geist-mono.woff2') format('woff2');
- font-style: normal;
+ src: url('/api/fonts/mono') format('woff2');
font-display: swap;
}
+@font-face {
+ font-family: Calistoga;
+ font-display: swap;
+ src: url('/api/fonts/serif') format('woff2');
+}
+
@layer base {
*,
::after,
@@ -173,7 +177,8 @@
--color-sidebar-ring: var(--sidebar-ring);
--font-sans: 'Geist', system-ui, sans-serif;
- --font-mono: 'Geist Mono';
+ --font-mono: 'Geist Mono', monospace;
+ --font-serif: Calistoga;
}
@layer base {
@@ -182,7 +187,7 @@
}
body {
- @apply bg-background text-foreground font-sans antialiased;
+ @apply bg-background text-foreground antialiased;
position: relative;
}
button {