|
1 | 1 | package spaserve
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "io/fs" |
4 | 6 | "maps" // Requires Go 1.21+
|
5 | 7 | "net/http"
|
6 | 8 | )
|
7 | 9 |
|
| 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 | + |
8 | 33 | // NewSpaServer creates a new http.Handler configured to serve a Single Page Application.
|
9 | 34 | // 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 | + |
11 | 59 | // 1. Apply defaults and validate configuration
|
12 | 60 | err := config.configure()
|
13 | 61 | if err != nil {
|
@@ -42,6 +90,26 @@ func NewSpaServer(config SpaServerConfig) (http.Handler, error) {
|
42 | 90 | // Index page handler makes sure we fetch index pages properly.
|
43 | 91 | server = newIndexPageHandler(server)
|
44 | 92 |
|
| 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 | + |
45 | 113 | // Modifier handler: Intercepts requests for targeted files, modifies them (using cache).
|
46 | 114 | // Delegates non-targeted requests or serves modified content.
|
47 | 115 | server = newModifyingHandler(server, config.FS, targetsMap, cache, logger, errorHandler)
|
|
0 commit comments