5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-07 06:31:11 +08:00
wails/v3/internal/assetserver/assetserver_dev.go
Fabio Massaioli d4096868e3
[v3] Fix and optimise assetserver (#4049)
* Fix and optimize content type sniffer

- Minimize copying and buffering
- Ensure it sniffs the full 512-bytes prefix

* Fix assorted warnings

* Cleanup error formatting

- Remove unnecessary formatting calls
- Fix invalid format strings
- Standardise logging calls

* Fix and optimize index fallback method

- Pass through non-404 responses correctly
- Do not buffer original response

* Test content sniffing and index fallback

* Update changelog

* Remove obsolete check

* Add safety checks in sniffer
2025-02-09 00:02:54 +11:00

51 lines
1.0 KiB
Go

//go:build !production
package assetserver
import (
"embed"
"io"
iofs "io/fs"
)
//go:embed defaults
var defaultHTML embed.FS
func defaultIndexHTML(language string) []byte {
result := []byte("index.html not found")
// Create an fs.Sub in the defaults directory
defaults, err := iofs.Sub(defaultHTML, "defaults")
if err != nil {
return result
}
// Get the 2 character language code
lang := "en"
if len(language) >= 2 {
lang = language[:2]
}
// Now we can read the index.html file in the format
// index.<lang>.html.
indexFile, err := defaults.Open("index." + lang + ".html")
if err != nil {
return result
}
indexBytes, err := io.ReadAll(indexFile)
if err != nil {
return result
}
return indexBytes
}
func (a *AssetServer) LogDetails() {
var info = []any{
"middleware", a.options.Middleware != nil,
"handler", a.options.Handler != nil,
}
if devServerURL := GetDevServerURL(); devServerURL != "" {
info = append(info, "devServerURL", devServerURL)
}
a.options.Logger.Info("AssetServer Info:", info...)
}