Switch to zerolog
This commit is contained in:
4
go.mod
4
go.mod
@@ -5,4 +5,8 @@ go 1.17
|
||||
require (
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/rs/zerolog v1.29.1 // indirect
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
|
||||
)
|
||||
|
||||
13
go.sum
13
go.sum
@@ -1,4 +1,17 @@
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
|
||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
|
||||
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
|
||||
28
main.go
28
main.go
@@ -1,28 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"flag"
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"code.severnaya.net/kordophone-mock/v2/web"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.Println("Initializing")
|
||||
debug := flag.Bool("debug", false, "enable debug logging")
|
||||
flag.Parse()
|
||||
|
||||
// Pretty logging
|
||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||
|
||||
// Default level for this example is info, unless debug flag is present
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
if *debug {
|
||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||
}
|
||||
|
||||
log.Info().Msg("Initializing")
|
||||
|
||||
c := web.MockHTTPServerConfiguration{
|
||||
AuthEnabled: false,
|
||||
}
|
||||
|
||||
addr := ":5738"
|
||||
s := web.NewMockHTTPServer(c)
|
||||
httpServer := &http.Server{
|
||||
Addr: ":5738",
|
||||
Addr: addr,
|
||||
Handler: s,
|
||||
}
|
||||
|
||||
// Populate with test data
|
||||
s.Server.PopulateWithTestData()
|
||||
log.Printf("Generated test data. %d conversations", len(s.Server.Conversations()))
|
||||
log.Info().Msgf("Generated test data. %d conversations", len(s.Server.Conversations()))
|
||||
|
||||
log.Fatal(httpServer.ListenAndServe())
|
||||
log.Info().Msgf("Listening on %s", addr)
|
||||
log.Fatal().Err(httpServer.ListenAndServe())
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ package model
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
)
|
||||
|
||||
@@ -40,7 +40,7 @@ func NewAuthToken(username string) (*AuthToken, error) {
|
||||
// Create a new JWT token
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
if token == nil {
|
||||
log.Printf("Error creating Jwt Token")
|
||||
log.Error().Msg("Error creating Jwt Token")
|
||||
return nil, &TokenGenerationError{"Error creating Jwt Token"}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func NewAuthToken(username string) (*AuthToken, error) {
|
||||
decodedSigningKey, _ := base64.StdEncoding.DecodeString(signingKey)
|
||||
signedToken, err := token.SignedString(decodedSigningKey)
|
||||
if err != nil {
|
||||
log.Printf("Error signing Jwt Token: %s", err)
|
||||
log.Error().Err(err).Msg("Error signing Jwt Token")
|
||||
return nil, &TokenGenerationError{"Error signing Jwt Token"}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user