Repository navigation
Expand file tree
/
Copy pathembed.go
More file actions
53 lines (43 loc) · 1.41 KB
/
Copy pathembed.go
File metadata and controls
53 lines (43 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// @soleil-clems: Embed - SPA frontend embedding & serving
package main
import (
"embed"
"io/fs"
"net/http"
"strings"
"github.com/soleilouisol/socAdmin/core/api"
)
//go:embed all:frontend/dist
var frontendFS embed.FS
// FrontendHandler serves the embedded React SPA.
// It injects the API prefix into index.html so the frontend knows the secret URL.
// The inline script uses a CSP nonce generated per request by SecurityHeaders.
func FrontendHandler(apiPrefix string) http.Handler {
dist, _ := fs.Sub(frontendFS, "frontend/dist")
fileServer := http.FileServer(http.FS(dist))
indexBytes, _ := fs.ReadFile(dist, "index.html")
indexTemplate := string(indexBytes)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
// Try to serve static files directly (JS, CSS, images, etc.)
if path != "/" {
cleanPath := strings.TrimPrefix(path, "/")
if f, err := dist.Open(cleanPath); err == nil {
f.Close()
fileServer.ServeHTTP(w, r)
return
}
}
// Get CSP nonce from context (set by SecurityHeaders middleware)
nonce, _ := r.Context().Value(api.CSPNonceKey).(string)
// SPA fallback: inject API prefix with nonce
html := strings.Replace(
indexTemplate,
"</head>",
`<script nonce="`+nonce+`">window.__SOCADMIN_API_PREFIX__="`+apiPrefix+`";</script></head>`,
1,
)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(html))
})
}