|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/go-chi/render" |
| 8 | +) |
| 9 | + |
| 10 | +// ManifestIcon represents an icon in the PWA manifest |
| 11 | +type ManifestIcon struct { |
| 12 | + Src string `json:"src"` |
| 13 | + Type string `json:"type"` |
| 14 | + Sizes string `json:"sizes"` |
| 15 | +} |
| 16 | + |
| 17 | +// Manifest represents the structure of a PWA manifest.json file |
| 18 | +type Manifest struct { |
| 19 | + ShortName string `json:"short_name"` |
| 20 | + Name string `json:"name"` |
| 21 | + Icons []ManifestIcon `json:"icons"` |
| 22 | + CaptureLinks string `json:"capture_links"` |
| 23 | + StartURL string `json:"start_url"` |
| 24 | + Display string `json:"display"` |
| 25 | + DisplayOverride []string `json:"display_override"` |
| 26 | + Scope string `json:"scope"` |
| 27 | + ThemeColor string `json:"theme_color"` |
| 28 | + Description string `json:"description"` |
| 29 | +} |
| 30 | + |
| 31 | +// Generate PWA manifest.json with dynamic values |
| 32 | +func manifestHandler(config *ServerConfig) http.HandlerFunc { |
| 33 | + return func(w http.ResponseWriter, r *http.Request) { |
| 34 | + manifest := Manifest{ |
| 35 | + ShortName: "SilverBullet", |
| 36 | + Name: "SilverBullet", |
| 37 | + Icons: []ManifestIcon{ |
| 38 | + { |
| 39 | + Src: "/.client/logo-dock.png", |
| 40 | + Type: "image/png", |
| 41 | + Sizes: "512x512", |
| 42 | + }, |
| 43 | + }, |
| 44 | + CaptureLinks: "new-client", |
| 45 | + StartURL: "/#boot", |
| 46 | + Display: "standalone", |
| 47 | + DisplayOverride: []string{"window-controls-overlay"}, |
| 48 | + Scope: "/", |
| 49 | + ThemeColor: "#e1e1e1", |
| 50 | + Description: "Markdown as a platform", |
| 51 | + } |
| 52 | + |
| 53 | + // Override with environment variables if set |
| 54 | + if os.Getenv("SB_NAME") != "" { |
| 55 | + manifest.Name = os.Getenv("SB_NAME") |
| 56 | + manifest.ShortName = os.Getenv("SB_NAME") |
| 57 | + } |
| 58 | + if os.Getenv("SB_DESCRIPTION") != "" { |
| 59 | + manifest.Description = os.Getenv("SB_DESCRIPTION") |
| 60 | + } |
| 61 | + |
| 62 | + if config.HostURLPrefix != "" { |
| 63 | + manifest.Icons[0].Src = config.HostURLPrefix + manifest.Icons[0].Src |
| 64 | + manifest.StartURL = config.HostURLPrefix + manifest.StartURL |
| 65 | + manifest.Scope = config.HostURLPrefix + manifest.Scope |
| 66 | + } |
| 67 | + |
| 68 | + render.JSON(w, r, manifest) |
| 69 | + } |
| 70 | +} |
0 commit comments