From 3613aac4c1ca21e90059e1dd900bb1c6fa45eb4d Mon Sep 17 00:00:00 2001 From: James Magahern Date: Mon, 19 Jun 2023 20:34:06 -0700 Subject: [PATCH] Log API endpoints --- web/server.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/web/server.go b/web/server.go index 5d4c429..052bd32 100644 --- a/web/server.go +++ b/web/server.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "net/http" + "strings" "code.severnaya.net/kordophone-mock/v2/server" ) @@ -27,6 +28,10 @@ func (e *AuthError) Error() string { return e.message } +func (m *MockHTTPServer) logRequest(r *http.Request, extras ...string) { + log.Printf("%s %s %s", r.Method, r.URL.Path, strings.Join(extras, " ")) +} + func (m *MockHTTPServer) checkAuthentication(r *http.Request) error { if !m.authEnabled { return nil @@ -146,7 +151,13 @@ func (m *MockHTTPServer) handleAuthenticate(w http.ResponseWriter, r *http.Reque w.Write(jsonData) } +func (m *MockHTTPServer) handleNotFound(w http.ResponseWriter, r *http.Request) { + log.Printf("** Unimplemented API endpoint: %s %s", r.Method, r.URL.Path) + http.NotFound(w, r) +} + func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { + m.logRequest(r, r.URL.Query().Encode()) m.mux.ServeHTTP(w, r) } @@ -162,6 +173,7 @@ func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer { this.mux.Handle("/status", http.HandlerFunc(this.handleStatus)) this.mux.Handle("/authenticate", http.HandlerFunc(this.handleAuthenticate)) this.mux.Handle("/messages", http.HandlerFunc(this.handleMessages)) + this.mux.Handle("/", http.HandlerFunc(this.handleNotFound)) return &this }