diff --git a/v3/internal/assetserver/assetserver.go b/v3/internal/assetserver/assetserver.go index 415f33cf615..4acddcddf3e 100644 --- a/v3/internal/assetserver/assetserver.go +++ b/v3/internal/assetserver/assetserver.go @@ -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) } diff --git a/v3/internal/assetserver/common.go b/v3/internal/assetserver/common.go index 109c228be53..2fe219f248b 100644 --- a/v3/internal/assetserver/common.go +++ b/v3/internal/assetserver/common.go @@ -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 == "" { diff --git a/v3/internal/assetserver/options.go b/v3/internal/assetserver/options.go index 60c43e26907..987eae7b5ae 100644 --- a/v3/internal/assetserver/options.go +++ b/v3/internal/assetserver/options.go @@ -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 diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index 60fd64e5f9e..03b9d19d7bf 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -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 {