Repository navigation
|
Prepared Q&A example: this question is being posted together with its solution, so it does not need a maintainer to investigate an unresolved application bug. In Wails v2, suppose the asset filesystem contains a file at With both How should |
Replies: 1 comment
|
Use
For example, with api := http.NewServeMux()
api.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte("{\"ok\":true}"))
})
assetOptions := &assetserver.Options{
Assets: assets,
Middleware: func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api/") {
api.ServeHTTP(w, r)
return
}
next.ServeHTTP(w, r)
})
},
}Assign The prefix check reserves all Checked with the v2.15.0 options type and a Go References: AssetServer options, Options source. This is the companion solution to the prepared example above. |
Use
assetserver.Options.Middlewareto dispatch the API prefix before calling Wails' default handler.Handleris a fallback for GET requests that are absent fromAssets; a matching static file therefore wins. Middleware receives the default handler asnextand can decide whether to call it.For example, with
assetsas your existing filesystem: