From 02026a9f3a6ba960004fa5a4d6b383b1aec96568 Mon Sep 17 00:00:00 2001 From: Tobias Gesellchen Date: Sun, 15 Feb 2026 00:14:13 +0100 Subject: [PATCH] Add configurable shortcuts and log them on startup --- cmd/soundtouch-service/main.go | 28 +++++++++++++-------- pkg/service/datastore/datastore.go | 17 +++++++------ pkg/service/handlers/server.go | 17 +++++++++++++ pkg/service/handlers/shortcut_middleware.go | 23 +++++++++++++++++ 4 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 pkg/service/handlers/shortcut_middleware.go diff --git a/cmd/soundtouch-service/main.go b/cmd/soundtouch-service/main.go index 2fdc54c..0d3401e 100644 --- a/cmd/soundtouch-service/main.go +++ b/cmd/soundtouch-service/main.go @@ -165,16 +165,19 @@ func main() { if !settingsExist { log.Printf("Creating default settings.json in %s", config.dataDir) - _ = ds.SaveSettings(datastore.Settings{ - ServerURL: config.serverURL, - ProxyURL: config.targetURL, - HTTPServerURL: config.httpsServerURL, - RedactLogs: config.redact, - LogBodies: config.logBody, - RecordInteractions: config.record, - DiscoveryInterval: config.discoveryInterval.String(), - DiscoveryDisabled: false, - }) + persisted.ServerURL = config.serverURL + persisted.ProxyURL = config.targetURL + persisted.HTTPServerURL = config.httpsServerURL + persisted.RedactLogs = config.redact + persisted.LogBodies = config.logBody + persisted.RecordInteractions = config.record + persisted.DiscoveryInterval = config.discoveryInterval.String() + persisted.DiscoveryDisabled = false + persisted.Shortcuts = map[string]int{ + "/.well-known/appspecific/com.chrome.devtools.json": http.StatusNotFound, + "/sw.js": http.StatusNotFound, + } + _ = ds.SaveSettings(persisted) } // Recalculate domains if settings changed @@ -191,6 +194,10 @@ func main() { server.SetHTTPServerURL(config.httpsServerURL) server.SetVersionInfo(version, commit, date) server.SetDiscoverySettings(config.discoveryInterval, persisted.DiscoveryDisabled) + server.SetShortcuts(persisted.Shortcuts) + for path, status := range persisted.Shortcuts { + log.Printf("Warning: configured shortcut: %s -> %d", path, status) + } recorder := proxy.NewRecorder(config.dataDir) recorder.Redact = config.redact @@ -440,6 +447,7 @@ func setupRouter(server *handlers.Server, pyProxy *httputil.ReverseProxy) *chi.M r := chi.NewRouter() r.Use(middleware.Logger) r.Use(middleware.Recoverer) + r.Use(server.ShortcutMiddleware) r.Use(server.RecordMiddleware) r.Get("/", server.HandleRoot) diff --git a/pkg/service/datastore/datastore.go b/pkg/service/datastore/datastore.go index 7bb62aa..bd1efc7 100644 --- a/pkg/service/datastore/datastore.go +++ b/pkg/service/datastore/datastore.go @@ -709,14 +709,15 @@ func (ds *DataStore) GetETagForAccount(account, device string) int64 { // Settings represents the global service settings. type Settings struct { - ServerURL string `json:"server_url"` - ProxyURL string `json:"proxy_url"` - HTTPServerURL string `json:"https_server_url,omitempty"` - RedactLogs bool `json:"redact_logs"` - LogBodies bool `json:"log_bodies"` - RecordInteractions bool `json:"record_interactions"` - DiscoveryInterval string `json:"discovery_interval,omitempty"` - DiscoveryDisabled bool `json:"discovery_disabled"` + ServerURL string `json:"server_url"` + ProxyURL string `json:"proxy_url"` + HTTPServerURL string `json:"https_server_url,omitempty"` + RedactLogs bool `json:"redact_logs"` + LogBodies bool `json:"log_bodies"` + RecordInteractions bool `json:"record_interactions"` + DiscoveryInterval string `json:"discovery_interval,omitempty"` + DiscoveryDisabled bool `json:"discovery_disabled"` + Shortcuts map[string]int `json:"shortcuts,omitempty"` } // GetSettings retrieves the global service settings. diff --git a/pkg/service/handlers/server.go b/pkg/service/handlers/server.go index 9df66b4..a987327 100644 --- a/pkg/service/handlers/server.go +++ b/pkg/service/handlers/server.go @@ -27,6 +27,7 @@ type Server struct { recordEnabled bool discoveryInterval time.Duration discoveryDisabled bool + shortcuts map[string]int recorder *proxy.Recorder Version string Commit string @@ -66,6 +67,22 @@ func (s *Server) SetDiscoverySettings(interval time.Duration, disabled bool) { s.discoveryDisabled = disabled } +// SetShortcuts sets the request shortcuts for the server. +func (s *Server) SetShortcuts(shortcuts map[string]int) { + s.mu.Lock() + defer s.mu.Unlock() + + s.shortcuts = shortcuts +} + +// GetShortcuts returns the current request shortcuts. +func (s *Server) GetShortcuts() map[string]int { + s.mu.RLock() + defer s.mu.RUnlock() + + return s.shortcuts +} + // GetDiscoverySettings returns the current discovery settings. func (s *Server) GetDiscoverySettings() (time.Duration, bool) { s.mu.RLock() diff --git a/pkg/service/handlers/shortcut_middleware.go b/pkg/service/handlers/shortcut_middleware.go new file mode 100644 index 0000000..a9e6cd3 --- /dev/null +++ b/pkg/service/handlers/shortcut_middleware.go @@ -0,0 +1,23 @@ +package handlers + +import ( + "net/http" +) + +// ShortcutMiddleware returns a middleware that shortcuts requests to specific paths. +func (s *Server) ShortcutMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + s.mu.RLock() + shortcuts := s.shortcuts + s.mu.RUnlock() + + if shortcuts != nil { + if status, ok := shortcuts[r.URL.Path]; ok { + w.WriteHeader(status) + return + } + } + + next.ServeHTTP(w, r) + }) +}