Private
Public Access
1
0

Log API endpoints

This commit is contained in:
2023-06-19 20:34:06 -07:00
parent 64c5169542
commit 3613aac4c1

View File

@@ -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
}