[v3, assetServer] Use middlewares for runtime, caps and falgs instead of having the implementation in the assetserver

This commit is contained in:
stffabi 2024-02-15 07:27:31 +01:00
commit 723dd976b4
4 changed files with 33 additions and 45 deletions

View file

@ -13,10 +13,6 @@ import (
)
const (
runtimePath = "/wails/runtime"
capabilitiesPath = "/wails/capabilities"
flagsPath = "/wails/flags"
webViewRequestHeaderWindowId = "x-wails-window-id"
webViewRequestHeaderWindowName = "x-wails-window-name"
)
@ -107,21 +103,7 @@ func (a *AssetServer) serveHTTP(rw http.ResponseWriter, req *http.Request, userH
default:
rw.WriteHeader(recorder.Code)
}
return
case capabilitiesPath:
var data = a.options.GetCapabilities()
a.writeBlob(rw, path, data)
case flagsPath:
var data = a.options.GetFlags()
a.writeBlob(rw, path, data)
case runtimePath:
a.options.RuntimeHandler.ServeHTTP(rw, req)
return
default:
// Check if this is a plugin script
@ -129,13 +111,12 @@ func (a *AssetServer) serveHTTP(rw http.ResponseWriter, req *http.Request, userH
a.writeBlob(rw, path, []byte(script))
} else {
userHandler.ServeHTTP(rw, req)
return
}
}
}
func (a *AssetServer) writeBlob(rw http.ResponseWriter, filename string, blob []byte) {
err := serveFile(rw, filename, blob)
err := ServeFile(rw, filename, blob)
if err != nil {
a.serveError(rw, err, "Unable to write content %s", filename)
}

View file

@ -26,7 +26,7 @@ var (
assetServerLogger = struct{}{}
)
func serveFile(rw http.ResponseWriter, filename string, blob []byte) error {
func ServeFile(rw http.ResponseWriter, filename string, blob []byte) error {
header := rw.Header()
header.Set(HeaderContentLength, fmt.Sprintf("%d", len(blob)))
if mimeType := header.Get(HeaderContentType); mimeType == "" {

View file

@ -26,15 +26,6 @@ type Options struct {
// Logger is the logger used by the AssetServer. If not defined, no logging will be done.
Logger *slog.Logger
// RuntimeHandler is the handler used for the runtime calls.
RuntimeHandler http.Handler
// GetCapabilities returns the capabilities of the runtime
GetCapabilities func() []byte
// GetFlags returns the application flags
GetFlags func() []byte
}
// Validate the options

View file

@ -76,22 +76,38 @@ func New(appOptions Options) *App {
result.Events = NewWailsEventProcessor(result.dispatchEventToWindows)
messageProc := NewMessageProcessor(result.Logger)
opts := &assetserver.Options{
Handler: appOptions.Assets.Handler,
Middleware: assetserver.Middleware(appOptions.Assets.Middleware),
Logger: result.Logger,
RuntimeHandler: NewMessageProcessor(result.Logger),
GetCapabilities: func() []byte {
return globalApplication.capabilities.AsBytes()
},
GetFlags: func() []byte {
updatedOptions := result.impl.GetFlags(appOptions)
flags, err := json.Marshal(updatedOptions)
if err != nil {
log.Fatal("Invalid flags provided to application: ", err.Error())
}
return flags
},
Handler: appOptions.Assets.Handler,
Middleware: assetserver.ChainMiddleware(
func(next http.Handler) http.Handler {
if m := appOptions.Assets.Middleware; m != nil {
return m(next)
}
return next
},
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
path := req.URL.Path
switch path {
case "/wails/runtime":
messageProc.ServeHTTP(rw, req)
case "/wails/capabilities":
assetserver.ServeFile(rw, path, globalApplication.capabilities.AsBytes())
case "/wails/flags":
updatedOptions := result.impl.GetFlags(appOptions)
flags, err := json.Marshal(updatedOptions)
if err != nil {
log.Fatal("Invalid flags provided to application: ", err.Error())
}
assetserver.ServeFile(rw, path, flags)
default:
next.ServeHTTP(rw, req)
}
})
},
),
Logger: result.Logger,
}
if appOptions.Assets.DisableLogging {