feat: add custom branding support for PWA

This commit is contained in:
vishalkadam47 2024-10-26 03:46:19 +05:30
parent d90d39933a
commit 037080c4ce
2 changed files with 36 additions and 2 deletions

View File

@ -9,9 +9,9 @@
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="Glance">
<meta name="apple-mobile-web-app-title" content="{{ if ne "" .App.Config.Branding.PwaText }}{{ .App.Config.Branding.PwaText }}{{ else }}Glance{{ end }}">
<meta name="theme-color" content="{{ if ne nil .App.Config.Theme.BackgroundColor }}{{ .App.Config.Theme.BackgroundColor }}{{ else }}hsl(240, 8%, 9%){{ end }}">
<link rel="apple-touch-icon" sizes="512x512" href="{{ .App.AssetPath "app-icon.png" }}">
<link rel="apple-touch-icon" sizes="512x512" href="{{ if ne "" .App.Config.Branding.FaviconURL }}{{ .App.Config.Branding.FaviconURL }}{{ else }}{{ .App.AssetPath "app-icon.png" }}{{ end }}">
<link rel="manifest" href="{{ .App.AssetPath "manifest.json" }}">
<link rel="icon" type="image/png" href="{{ .App.Config.Branding.FaviconURL }}" />
<link rel="stylesheet" href="{{ .App.AssetPath "main.css" }}">

View File

@ -3,6 +3,7 @@ package glance
import (
"bytes"
"context"
"encoding/json"
"fmt"
"html/template"
"log/slog"
@ -55,6 +56,7 @@ type Branding struct {
LogoText string `yaml:"logo-text"`
LogoURL string `yaml:"logo-url"`
FaviconURL string `yaml:"favicon-url"`
PwaText string `yaml:"pwa-text"`
}
type Column struct {
@ -279,6 +281,8 @@ func (a *Application) Serve() error {
w.WriteHeader(http.StatusOK)
})
mux.HandleFunc(fmt.Sprintf("GET /static/%s/manifest.json", a.Config.Server.AssetsHash), a.HandleManifestRequest)
mux.Handle(
fmt.Sprintf("GET /static/%s/{path...}", a.Config.Server.AssetsHash),
http.StripPrefix("/static/"+a.Config.Server.AssetsHash, FileServerWithCache(http.FS(assets.PublicFS), 24*time.Hour)),
@ -306,3 +310,33 @@ func (a *Application) Serve() error {
return server.ListenAndServe()
}
func (a *Application) HandleManifestRequest(w http.ResponseWriter, r *http.Request) {
manifest := map[string]interface{}{
"name": func() string {
if a.Config.Branding.PwaText != "" {
return a.Config.Branding.PwaText
}
return "Glance"
}(),
"display": "standalone",
"background_color": "#151519",
"scope": "/",
"start_url": "/",
"icons": []map[string]string{
{
"src": func() string {
if a.Config.Branding.FaviconURL != "" {
return a.Config.Branding.FaviconURL
}
return "app-icon.png"
}(),
"type": "image/png",
"sizes": "512x512",
},
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(manifest)
}