diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index 1ff4ad5a..92f6b12e 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -45,7 +45,12 @@ func New(appOptions Options) *App { result.Events = NewCustomEventProcessor(result.dispatchEventToWindows) - opts := assetserveroptions.Options{Assets: appOptions.Assets.FS, Handler: appOptions.Assets.Handler, Middleware: appOptions.Assets.Middleware} + opts := assetserveroptions.Options{ + Assets: appOptions.Assets.FS, + Handler: appOptions.Assets.Handler, + Middleware: assetserveroptions.Middleware(appOptions.Assets.Middleware), + } + // TODO ServingFrom disk? srv, err := assetserver.NewAssetServer("", opts, false, nil, wailsruntime.RuntimeAssetsBundle) if err != nil { diff --git a/v3/pkg/application/options_application.go b/v3/pkg/application/options_application.go index 69ede8ab..f1989010 100644 --- a/v3/pkg/application/options_application.go +++ b/v3/pkg/application/options_application.go @@ -20,11 +20,46 @@ type Options struct { Assets AssetOptions } +// AssetOptions defines the configuration of the AssetServer. type AssetOptions struct { - // FS to use for loading assets from + // FS defines the static assets to be used. A GET request is first tried to be served from this FS. If the FS returns + // `os.ErrNotExist` for that file, the request handling will fallback to the Handler and tries to serve the GET + // request from it. + // + // If set to nil, all GET requests will be forwarded to Handler. FS fs.FS - // Handler is a custom handler to use for serving assets. If this is set, the `URL` and `FS` fields are ignored. + + // Handler will be called for every GET request that can't be served from FS, due to `os.ErrNotExist`. Furthermore all + // non GET requests will always be served from this Handler. + // + // If not defined, the result is the following in cases where the Handler would have been called: + // GET request: `http.StatusNotFound` + // Other request: `http.StatusMethodNotAllowed` Handler http.Handler - // Middleware is a custom middleware to use for serving assets. If this is set, the `URL` and `FS` fields are ignored. - Middleware func(http.Handler) http.Handler + + // Middleware is a HTTP Middleware which allows to hook into the AssetServer request chain. It allows to skip the default + // request handler dynamically, e.g. implement specialized Routing etc. + // The Middleware is called to build a new `http.Handler` used by the AssetSever and it also receives the default + // handler used by the AssetServer as an argument. + // + // If not defined, the default AssetServer request chain is executed. + // + // Multiple Middlewares can be chained together with: + // ChainMiddleware(middleware ...Middleware) Middleware + Middleware Middleware +} + +// Middleware defines a HTTP middleware that can be applied to the AssetServer. +// The handler passed as next is the next handler in the chain. One can decide to call the next handler +// or implement a specialized handling. +type Middleware func(next http.Handler) http.Handler + +// ChainMiddleware allows chaining multiple middlewares to one middleware. +func ChainMiddleware(middleware ...Middleware) Middleware { + return func(h http.Handler) http.Handler { + for i := len(middleware) - 1; i >= 0; i-- { + h = middleware[i](h) + } + return h + } }