5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-18 09:59:31 +08:00

[v3 assetserver] cleanup logging

- remove type assertions
- update contentTypeSniffer to capture the status code
- move logic in ServeHTTP to serveHTTP
- wrap serveHTTP with ServeHTTP adding logging & duration calculation
This commit is contained in:
Travis McLane 2023-08-24 12:44:13 -05:00
parent bd9f7deb98
commit f50c8f447e
2 changed files with 19 additions and 19 deletions

View File

@ -3,13 +3,13 @@ package assetserver
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/wailsapp/wails/v3/internal/assetserver/webview"
"log/slog" "log/slog"
"math/rand" "math/rand"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/http/httputil" "net/http/httputil"
"strings" "strings"
"time"
"golang.org/x/net/html" "golang.org/x/net/html"
) )
@ -139,25 +139,30 @@ func (d *AssetServer) AddPluginScript(pluginName string, script string) {
d.pluginScripts[pluginScriptName] = script d.pluginScripts[pluginScriptName] = script
} }
func logRequest(logger *slog.Logger, req *http.Request, code int) { func (d *AssetServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
windowName := req.Header.Get(webViewRequestHeaderWindowName) start := time.Now()
windowID := req.Header.Get(webViewRequestHeaderWindowId) wrapped := &contentTypeSniffer{rw: rw}
logger.Info("Asset Request:", "windowName", windowName, "windowID", windowID, "code", code, "method", req.Method, "url", req.URL.Path) d.serveHTTP(wrapped, req)
d.logger.Info(
"Asset Request:",
"windowName", req.Header.Get(webViewRequestHeaderWindowName),
"windowID", req.Header.Get(webViewRequestHeaderWindowId),
"code", wrapped.status,
"method", req.Method,
"path", req.URL.EscapedPath(),
"duration", time.Since(start),
)
} }
func (d *AssetServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (d *AssetServer) serveHTTP(rw http.ResponseWriter, req *http.Request) {
if d.wsHandler != nil { if d.wsHandler != nil {
d.wsHandler.ServeHTTP(rw, req) d.wsHandler.ServeHTTP(rw, req)
// Get response code from header
code := rw.(*contentTypeSniffer).rw.(*legacyRequestNoOpCloserResponseWriter).ResponseWriter.(*httptest.ResponseRecorder).Code
logRequest(d.logger, req, code)
return return
} else { } else {
if isWebSocket(req) { if isWebSocket(req) {
// WebSockets are not supported by the AssetServer // WebSockets are not supported by the AssetServer
rw.WriteHeader(http.StatusNotImplemented) rw.WriteHeader(http.StatusNotImplemented)
logRequest(d.logger, req, http.StatusNotImplemented)
return return
} }
} }
@ -192,7 +197,6 @@ func (d *AssetServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(recorder.Code) rw.WriteHeader(recorder.Code)
} }
logRequest(d.logger, req, recorder.Code)
return return
case runtimeJSPath: case runtimeJSPath:
@ -229,13 +233,9 @@ func (d *AssetServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
d.writeBlob(rw, path, []byte(script)) d.writeBlob(rw, path, []byte(script))
} else { } else {
d.handler.ServeHTTP(rw, req) d.handler.ServeHTTP(rw, req)
code := rw.(*contentTypeSniffer).rw.(webview.ResponseWriter).Code()
logRequest(d.logger, req, code)
return return
} }
} }
logRequest(d.logger, req, http.StatusOK)
} }
func (d *AssetServer) processIndexHTML(indexHTML []byte) ([]byte, error) { func (d *AssetServer) processIndexHTML(indexHTML []byte) ([]byte, error) {

View File

@ -5,12 +5,12 @@ import (
) )
type contentTypeSniffer struct { type contentTypeSniffer struct {
rw http.ResponseWriter rw http.ResponseWriter
status int
wroteHeader bool wroteHeader bool
} }
func (rw *contentTypeSniffer) Header() http.Header { func (rw contentTypeSniffer) Header() http.Header {
return rw.rw.Header() return rw.rw.Header()
} }
@ -23,7 +23,7 @@ func (rw *contentTypeSniffer) WriteHeader(code int) {
if rw.wroteHeader { if rw.wroteHeader {
return return
} }
rw.status = code
rw.rw.WriteHeader(code) rw.rw.WriteHeader(code)
rw.wroteHeader = true rw.wroteHeader = true
} }