Skip to content

Commit 1608dbd

Browse files
Additional backwards compatibility for options.
1 parent 843bb91 commit 1608dbd

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

spaserver.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,61 @@
11
package spaserve
22

33
import (
4+
"fmt"
5+
"io/fs"
46
"maps" // Requires Go 1.21+
57
"net/http"
68
)
79

10+
type spaServerOption func(SpaServerConfig) SpaServerConfig
11+
12+
func WithHtmlPageWhitelist(whitelist []string) spaServerOption {
13+
return func(c SpaServerConfig) SpaServerConfig {
14+
c.HtmlPageWhitelist = whitelist
15+
return c
16+
}
17+
}
18+
19+
func WithSpaFallbackPath(spaFallbackPath string) spaServerOption {
20+
return func(c SpaServerConfig) SpaServerConfig {
21+
c.SpaFallbackPath = spaFallbackPath
22+
return c
23+
}
24+
}
25+
26+
func WithTargets(targets []TargetConfig) spaServerOption {
27+
return func(c SpaServerConfig) SpaServerConfig {
28+
c.Targets = targets
29+
return c
30+
}
31+
}
32+
833
// NewSpaServer creates a new http.Handler configured to serve a Single Page Application.
934
// It applies file modifications, handles SPA routing fallbacks, and serves static files.
10-
func NewSpaServer(config SpaServerConfig) (http.Handler, error) {
35+
func NewSpaServer(filesys fs.FS, fn ...interface{}) (http.Handler, error) {
36+
config := SpaServerConfig{
37+
FS: filesys,
38+
SpaFallbackPath: "/",
39+
}
40+
staticFileServerHandlerOpts := defaultStaticFilesHandlerOpts
41+
for _, f := range fn {
42+
spaServerOption, isSPAServerOption := f.(spaServerOption)
43+
if isSPAServerOption {
44+
config = spaServerOption(config)
45+
}
46+
staticFileServerOption, isStaticFileServerOption := f.(staticFilesHandlerFunc)
47+
if isStaticFileServerOption {
48+
staticFileServerHandlerOpts = staticFileServerOption(staticFileServerHandlerOpts)
49+
}
50+
if !isSPAServerOption && !isStaticFileServerOption {
51+
panic(fmt.Errorf("Unknown option: %q", f))
52+
}
53+
}
54+
// Updates from backwards-compatible flags
55+
config.BasePath = staticFileServerHandlerOpts.basePath
56+
config.Logger = staticFileServerHandlerOpts.logger
57+
config.MuxErrorHandler = newMuxErrorHandler(staticFileServerHandlerOpts.muxErrHandler)
58+
1159
// 1. Apply defaults and validate configuration
1260
err := config.configure()
1361
if err != nil {
@@ -42,6 +90,26 @@ func NewSpaServer(config SpaServerConfig) (http.Handler, error) {
4290
// Index page handler makes sure we fetch index pages properly.
4391
server = newIndexPageHandler(server)
4492

93+
// Backwards-compatible modifier handler: if using opts.ns and opts.webEnv, add the modifier
94+
// This replicates the staticFileServerHandler behavior
95+
if staticFileServerHandlerOpts.ns != "" && staticFileServerHandlerOpts.webEnv != nil {
96+
envModifier, err := CreateHtmlScriptTagEnvModifier(
97+
staticFileServerHandlerOpts.webEnv,
98+
staticFileServerHandlerOpts.ns,
99+
)
100+
if err != nil {
101+
return nil, err
102+
}
103+
backCompatTargetsMap := map[string]TargetConfig{
104+
"index.html": {
105+
TargetFile: "index.html",
106+
Modifier: envModifier,
107+
CacheResult: true,
108+
},
109+
}
110+
server = newModifyingHandler(server, config.FS, backCompatTargetsMap, cache, logger, errorHandler)
111+
}
112+
45113
// Modifier handler: Intercepts requests for targeted files, modifies them (using cache).
46114
// Delegates non-targeted requests or serves modified content.
47115
server = newModifyingHandler(server, config.FS, targetsMap, cache, logger, errorHandler)

0 commit comments

Comments
 (0)