Switch to zerolog
This commit is contained in:
4
go.mod
4
go.mod
@@ -5,4 +5,8 @@ go 1.17
|
|||||||
require (
|
require (
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
||||||
github.com/google/uuid v1.3.0 // 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 h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
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 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"os"
|
||||||
|
"flag"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"code.severnaya.net/kordophone-mock/v2/web"
|
"code.severnaya.net/kordophone-mock/v2/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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{
|
c := web.MockHTTPServerConfiguration{
|
||||||
AuthEnabled: false,
|
AuthEnabled: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addr := ":5738"
|
||||||
s := web.NewMockHTTPServer(c)
|
s := web.NewMockHTTPServer(c)
|
||||||
httpServer := &http.Server{
|
httpServer := &http.Server{
|
||||||
Addr: ":5738",
|
Addr: addr,
|
||||||
Handler: s,
|
Handler: s,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate with test data
|
// Populate with test data
|
||||||
s.Server.PopulateWithTestData()
|
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 (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ func NewAuthToken(username string) (*AuthToken, error) {
|
|||||||
// Create a new JWT token
|
// Create a new JWT token
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
if token == nil {
|
if token == nil {
|
||||||
log.Printf("Error creating Jwt Token")
|
log.Error().Msg("Error creating Jwt Token")
|
||||||
return nil, &TokenGenerationError{"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)
|
decodedSigningKey, _ := base64.StdEncoding.DecodeString(signingKey)
|
||||||
signedToken, err := token.SignedString(decodedSigningKey)
|
signedToken, err := token.SignedString(decodedSigningKey)
|
||||||
if err != nil {
|
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"}
|
return nil, &TokenGenerationError{"Error signing Jwt Token"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ package web
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"code.severnaya.net/kordophone-mock/v2/server"
|
"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) {
|
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 {
|
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) {
|
func (m *MockHTTPServer) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := m.checkAuthentication(r); err != nil {
|
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)
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ func (m *MockHTTPServer) handleConversations(w http.ResponseWriter, r *http.Requ
|
|||||||
// Encode convos as JSON
|
// Encode convos as JSON
|
||||||
jsonData, err := json.Marshal(convos)
|
jsonData, err := json.Marshal(convos)
|
||||||
if err != nil {
|
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)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -92,14 +92,14 @@ func (m *MockHTTPServer) handleMessages(w http.ResponseWriter, r *http.Request)
|
|||||||
|
|
||||||
guid := r.URL.Query().Get("guid")
|
guid := r.URL.Query().Get("guid")
|
||||||
if len(guid) == 0 {
|
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)
|
http.Error(w, "no guid parameter specified", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conversation, err := m.Server.ConversationForGUID(guid)
|
conversation, err := m.Server.ConversationForGUID(guid)
|
||||||
if err != nil {
|
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)
|
http.Error(w, "conversation not found", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -108,7 +108,7 @@ func (m *MockHTTPServer) handleMessages(w http.ResponseWriter, r *http.Request)
|
|||||||
|
|
||||||
jsonData, err := json.Marshal(messages)
|
jsonData, err := json.Marshal(messages)
|
||||||
if err != nil {
|
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)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -123,7 +123,7 @@ func (m *MockHTTPServer) handleAuthenticate(w http.ResponseWriter, r *http.Reque
|
|||||||
var authReq AuthenticationRequest
|
var authReq AuthenticationRequest
|
||||||
err := json.NewDecoder(r.Body).Decode(&authReq)
|
err := json.NewDecoder(r.Body).Decode(&authReq)
|
||||||
if err != nil {
|
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)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -131,7 +131,7 @@ func (m *MockHTTPServer) handleAuthenticate(w http.ResponseWriter, r *http.Reque
|
|||||||
// Authenticate
|
// Authenticate
|
||||||
token, err := m.Server.Authenticate(authReq.Username, authReq.Password)
|
token, err := m.Server.Authenticate(authReq.Username, authReq.Password)
|
||||||
if err != nil {
|
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)
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -142,7 +142,7 @@ func (m *MockHTTPServer) handleAuthenticate(w http.ResponseWriter, r *http.Reque
|
|||||||
// Encode token as JSON
|
// Encode token as JSON
|
||||||
jsonData, err := json.Marshal(token)
|
jsonData, err := json.Marshal(token)
|
||||||
if err != nil {
|
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)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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) {
|
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)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user