Private
Public Access
1
0

Switch to zerolog

This commit is contained in:
James Magahern
2023-06-22 11:03:00 -07:00
parent 3613aac4c1
commit 84dbb7f006
5 changed files with 54 additions and 19 deletions

View File

@@ -3,10 +3,10 @@ package web
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"github.com/rs/zerolog/log"
"code.severnaya.net/kordophone-mock/v2/server"
)
@@ -29,7 +29,7 @@ func (e *AuthError) Error() string {
}
func (m *MockHTTPServer) logRequest(r *http.Request, extras ...string) {
log.Printf("%s %s %s", r.Method, r.URL.Path, strings.Join(extras, " "))
log.Debug().Msgf("%s %s %s", r.Method, r.URL.Path, strings.Join(extras, " "))
}
func (m *MockHTTPServer) checkAuthentication(r *http.Request) error {
@@ -63,7 +63,7 @@ func (m *MockHTTPServer) handleVersion(w http.ResponseWriter, r *http.Request) {
func (m *MockHTTPServer) handleStatus(w http.ResponseWriter, r *http.Request) {
if err := m.checkAuthentication(r); err != nil {
log.Printf("Status: Error checking authentication: %s", err)
log.Error().Err(err).Msg("Status: Error checking authentication")
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
@@ -77,7 +77,7 @@ func (m *MockHTTPServer) handleConversations(w http.ResponseWriter, r *http.Requ
// Encode convos as JSON
jsonData, err := json.Marshal(convos)
if err != nil {
log.Printf("Error marshalling conversations: %s", err)
log.Error().Err(err).Msg("Error marshalling conversations")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -92,14 +92,14 @@ func (m *MockHTTPServer) handleMessages(w http.ResponseWriter, r *http.Request)
guid := r.URL.Query().Get("guid")
if len(guid) == 0 {
log.Println("handleMessage: Got empty guid parameter")
log.Error().Msg("handleMessage: Got empty guid parameter")
http.Error(w, "no guid parameter specified", http.StatusBadRequest)
return
}
conversation, err := m.Server.ConversationForGUID(guid)
if err != nil {
log.Printf("handleMessage: Error getting conversation (%s): %s", guid, err)
log.Error().Err(err).Msgf("handleMessage: Error getting conversation (%s)", guid)
http.Error(w, "conversation not found", http.StatusBadRequest)
return
}
@@ -108,7 +108,7 @@ func (m *MockHTTPServer) handleMessages(w http.ResponseWriter, r *http.Request)
jsonData, err := json.Marshal(messages)
if err != nil {
log.Printf("Error marshalling messages: %s", err)
log.Error().Err(err).Msg("Error marshalling messages")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -123,7 +123,7 @@ func (m *MockHTTPServer) handleAuthenticate(w http.ResponseWriter, r *http.Reque
var authReq AuthenticationRequest
err := json.NewDecoder(r.Body).Decode(&authReq)
if err != nil {
log.Printf("Authenticate: Error decoding request body: %s", err)
log.Error().Err(err).Msg("Authenticate: Error decoding request body")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
@@ -131,7 +131,7 @@ func (m *MockHTTPServer) handleAuthenticate(w http.ResponseWriter, r *http.Reque
// Authenticate
token, err := m.Server.Authenticate(authReq.Username, authReq.Password)
if err != nil {
log.Printf("Authenticate: Error authenticating: %s", err)
log.Error().Err(err).Msg("Authenticate: Error authenticating")
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
@@ -142,7 +142,7 @@ func (m *MockHTTPServer) handleAuthenticate(w http.ResponseWriter, r *http.Reque
// Encode token as JSON
jsonData, err := json.Marshal(token)
if err != nil {
log.Printf("Error marshalling token: %s", err)
log.Error().Err(err).Msg("Error marshalling token")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -152,7 +152,7 @@ func (m *MockHTTPServer) handleAuthenticate(w http.ResponseWriter, r *http.Reque
}
func (m *MockHTTPServer) handleNotFound(w http.ResponseWriter, r *http.Request) {
log.Printf("** Unimplemented API endpoint: %s %s", r.Method, r.URL.Path)
log.Error().Msgf("Unimplemented API endpoint: %s %s", r.Method, r.URL.Path)
http.NotFound(w, r)
}