Skip to content

Commit ad9f19a

Browse files
committed
Manifest generation
1 parent e4d8f09 commit ad9f19a

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

pkg/server/auth.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import (
1313
const authPagePath = ".client/auth.html"
1414

1515
var excludedPaths = []string{
16-
"/manifest.json",
17-
"/favicon.png",
18-
"/logo.png",
16+
"/.client/",
1917
"/.auth",
2018
"/.logout",
2119
"/.ping",
@@ -126,7 +124,7 @@ func authMiddleware(config *ServerConfig, jwtIssuer *Authenticator) func(http.Ha
126124
path := removeURLPrefix(r.URL.Path, config.HostURLPrefix)
127125
host := extractHost(r)
128126

129-
if isExcludedPath(path, excludedPaths) {
127+
if isExcludedPath(path) {
130128
next.ServeHTTP(w, r)
131129
return
132130
}

pkg/server/auth_utils.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"net/http"
66
"regexp"
7-
"slices"
87
"strings"
98
"time"
109
)
@@ -133,8 +132,14 @@ func removeURLPrefix(path, prefix string) string {
133132
}
134133

135134
// isExcludedPath checks if a path should be excluded from authentication
136-
func isExcludedPath(path string, excludedPaths []string) bool {
137-
return slices.Contains(excludedPaths, path)
135+
func isExcludedPath(path string) bool {
136+
// Check if path starts with any item in excludedPaths
137+
for _, excludedPath := range excludedPaths {
138+
if strings.HasPrefix(path, excludedPath) {
139+
return true
140+
}
141+
}
142+
return false
138143
}
139144

140145
// parseFormValue safely extracts a form value

pkg/server/manifest.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

pkg/server/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ func RunServer(config *ServerConfig) error {
8080
// Proxy endpoint
8181
r.HandleFunc("/.proxy/*", proxyHandler(config))
8282

83+
// Manifest endpoint
84+
r.HandleFunc("/.client/manifest.json", manifestHandler(config))
85+
8386
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
8487
path := chi.URLParam(r, "*")
8588

0 commit comments

Comments
 (0)