Skip to content

Commit

Permalink
[v3, assetServer] Use middlewares for runtime, caps and falgs instead…
Browse files Browse the repository at this point in the history
… of having the implementation in the assetserver
  • Loading branch information
stffabi committed Feb 16, 2024
1 parent 65251cd commit 723dd97
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 45 deletions.
21 changes: 1 addition & 20 deletions v3/internal/assetserver/assetserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -107,35 +103,20 @@ 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
if script, ok := a.pluginScripts[path]; ok {
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)
}
Expand Down
2 changes: 1 addition & 1 deletion v3/internal/assetserver/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
9 changes: 0 additions & 9 deletions v3/internal/assetserver/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 31 additions & 15 deletions v3/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 723dd97

Please sign in to comment.