Skip to content

Commit

Permalink
feat: add debug mode
Browse files Browse the repository at this point in the history
In debug mode all events will have their raw payload logged.
  • Loading branch information
crazybolillo committed Sep 28, 2024
1 parent 08fee39 commit 8ba1977
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ func run(ctx context.Context) int {
return 1
}

if cfg.Debug {
slog.SetLogLoggerLevel(slog.LevelDebug)
}

return serve.Start(ctx, cfg)
}
13 changes: 11 additions & 2 deletions internal/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {
ServiceHost string `env:"SERVICE_HOST"`
AppName string `env:"APP_NAME" envDefault:"vouncer"`
Credentials string `env:"CREDENTIALS"`
Debug bool `env:"DEBUG" envDefault:"false"`
}

type Call struct {
Expand Down Expand Up @@ -62,17 +63,25 @@ func Start(ctx context.Context, cfg Config) int {
return 2
}

return serve(ctx, c)
return serve(ctx, c, cfg.Debug)
}

func serve(ctx context.Context, conn *websocket.Conn) int {
func serve(ctx context.Context, conn *websocket.Conn, debug bool) int {
for {
_, payload, err := conn.ReadMessage()
if err != nil {
slog.Error("Websocket read failed", slog.String("reason", err.Error()))
return 1
}

if debug {
var pretty bytes.Buffer
err = json.Indent(&pretty, payload, "", " ")
if err == nil {
slog.Debug(pretty.String())
}
}

evt := ari.Event{}
if err := json.Unmarshal(payload, &evt); err != nil {
slog.Error("Websocket message processing failed", slog.String("reason", err.Error()))
Expand Down

0 comments on commit 8ba1977

Please sign in to comment.