Skip to content

Commit

Permalink
Less noisy logging
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Jun 23, 2023
1 parent 220f9da commit 27eb8f2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
10 changes: 5 additions & 5 deletions cmd/notification-service/di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions service/app/handler_save_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"context"

"github.com/planetary-social/go-notification-service/internal/logging"
"github.com/planetary-social/go-notification-service/service/domain"
)

Expand All @@ -16,17 +17,26 @@ func NewSaveRegistration(registration domain.Registration) SaveRegistration {

type SaveRegistrationHandler struct {
transactionProvider TransactionProvider
logger logging.Logger
}

func NewSaveRegistrationHandler(
transactionProvider TransactionProvider,
logger logging.Logger,
) *SaveRegistrationHandler {
return &SaveRegistrationHandler{
transactionProvider: transactionProvider,
logger: logger.New("saveRegistrationHandler"),
}
}

func (h *SaveRegistrationHandler) Handle(ctx context.Context, cmd SaveRegistration) error {
h.logger.Debug().
WithField("apnsToken", cmd.registration.APNSToken().Hex()).
WithField("publicKey", cmd.registration.PublicKey().Hex()).
WithField("relays", cmd.registration.Relays()).
Message("saving registration")

return h.transactionProvider.Transact(ctx, func(ctx context.Context, adapters Adapters) error {
return adapters.Registrations.Save(cmd.registration)
})
Expand Down
26 changes: 18 additions & 8 deletions service/ports/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package http
import (
"context"
"fmt"
"log"
"net"
"net/http"

"github.com/boreq/errors"
"github.com/gorilla/websocket"
"github.com/nbd-wtf/go-nostr"
"github.com/planetary-social/go-notification-service/internal/logging"
"github.com/planetary-social/go-notification-service/service/app"
"github.com/planetary-social/go-notification-service/service/config"
"github.com/planetary-social/go-notification-service/service/domain"
Expand All @@ -18,12 +18,18 @@ import (
type Server struct {
config config.Config
app app.Application
logger logging.Logger
}

func NewServer(config config.Config, app app.Application) Server {
func NewServer(
config config.Config,
app app.Application,
logger logging.Logger,
) Server {
return Server{
config: config,
app: app,
logger: logger.New("server"),
}
}

Expand Down Expand Up @@ -62,29 +68,33 @@ func (s *Server) serveWs(ctx context.Context, rw http.ResponseWriter, r *http.Re

conn, err := upgrader.Upgrade(rw, r, nil)
if err != nil {
log.Println("error upgrading the connection:", err)
s.logger.Error().WithError(err).Message("error upgrading the connection")
return
}

defer func() {
err := conn.Close()
fmt.Println("closed the connection, error:", err)
if err := conn.Close(); err != nil {
s.logger.Error().WithError(err).Message("error closing the connection")
}
}()

if err := s.handleConnection(ctx, conn); err != nil {
fmt.Println("error handling the connection:", err)
closeErr := &websocket.CloseError{}
if !errors.As(err, &closeErr) || closeErr.Code != websocket.CloseNormalClosure {
s.logger.Error().WithError(err).Message("error handling the connection")
}
}
}

func (s *Server) handleConnection(ctx context.Context, conn *websocket.Conn) error {
s.logger.Debug().Message("accepted websocket connection")

for {
_, messageBytes, err := conn.ReadMessage()
if err != nil {
return errors.Wrap(err, "error reading the websocket message")
}

fmt.Printf("received websocket message: %s\n", string(messageBytes))

message := nostr.ParseMessage(messageBytes)
if message == nil {
return errors.New("failed to parse the message")
Expand Down

0 comments on commit 27eb8f2

Please sign in to comment.