[v3] AssetServer options update comments

This commit is contained in:
stffabi
2023-03-16 06:49:46 +01:00
parent 0e614b0194
commit 74f349862e
2 changed files with 45 additions and 5 deletions
+6 -1
View File
@@ -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 {
+39 -4
View File
@@ -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
}
}