5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-07 03:00:52 +08:00
wails/v3/pkg/application/options_application.go
2023-08-14 20:49:09 +10:00

85 lines
3.0 KiB
Go

package application
import (
"io/fs"
"log/slog"
"net/http"
)
type Options struct {
Name string
Description string
Icon []byte
Mac MacOptions
Windows WindowsApplicationOptions
Bind []any
Logger *slog.Logger
Assets AssetOptions
Plugins map[string]Plugin
Flags map[string]any
// PanicHandler is a way to register a custom panic handler
PanicHandler func(any)
// ProductionOverrides allows you to have different options in production builds
// We would love if we could merge the options, but we can't because of the way
// Go handles zero values.
ProductionOverrides *Options
}
func (o Options) getOptions(debugMode bool) Options {
if o.ProductionOverrides == nil || debugMode {
o.ProductionOverrides = nil
return o
}
return *o.ProductionOverrides
}
// AssetOptions defines the configuration of the AssetServer.
type AssetOptions struct {
// 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 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 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
// External URL can be set to a development server URL so that all requests are forwarded to it. This is useful
// when using a development server like `vite` or `snowpack` which serves the assets on a different port.
ExternalURL string
}
// 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
}
}