Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package config

import "go.uber.org/zap"

// NewLogger creates a new zap.Logger based on the provided configuration.
func NewLogger(config *Config) (*zap.Logger, error) {
if config.Development {
return zap.NewDevelopment()
}
return zap.NewProduction()
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ require (
github.com/gorilla/sessions v1.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/dig v1.19.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.26.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.1 // indirect
)

require (
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,15 @@ go.uber.org/fx v1.24.0 h1:wE8mruvpg2kiiL1Vqd0CC+tr0/24XIB10Iwp2lLWzkg=
go.uber.org/fx v1.24.0/go.mod h1:AmDeGyS+ZARGKM4tlH4FY2Jr63VjbEDJHtqXTGP5hbo=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc=
go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/arch v0.16.0 h1:foMtLTdyOmIniqWCHjY6+JxuC54XP1fDwx4N0ASyW+U=
golang.org/x/arch v0.16.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
4 changes: 4 additions & 0 deletions internal/repository/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ import (
"github.com/google/uuid"
_ "github.com/mattn/go-sqlite3"
"go.uber.org/fx"
"go.uber.org/zap"
)

var _ Repository = (*SQLite)(nil)

type SQLite struct {
conn *sql.DB
config *config.Config
logger *zap.Logger
}

type SQLiteParams struct {
fx.In
Config *config.Config
Logger *zap.Logger
}

var (
Expand Down Expand Up @@ -54,6 +57,7 @@ func NewSQLite(p SQLiteParams) (Repository, error) {
repo := &SQLite{
conn: db,
config: p.Config,
logger: p.Logger,
}

err = repo.migrate(p.Config.Database.Schema)
Expand Down
17 changes: 10 additions & 7 deletions internal/repository/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"time"
Expand All @@ -15,16 +14,19 @@ import (
"github.com/google/uuid"
"github.com/gorilla/websocket"
"go.uber.org/fx"
"go.uber.org/zap"
)

var _ WebSocketRepository = (*WebSocket)(nil)

type WebSocket struct {
connections map[uuid.UUID][]*websocket.Conn
logger *zap.Logger
}

type WebSocketParams struct {
fx.In
Logger *zap.Logger
}

var upgrader = websocket.Upgrader{
Expand All @@ -39,6 +41,7 @@ func NewWebSocket(p WebSocketParams) WebSocketRepository {
connections := make(map[uuid.UUID][]*websocket.Conn)
return &WebSocket{
connections: connections,
logger: p.Logger,
}
}

Expand All @@ -64,7 +67,7 @@ func (ws *WebSocket) AddConnection(ctx context.Context, w http.ResponseWriter, r
for {
err := conn.SetReadDeadline(time.Now().Add(30 * time.Second))
if err != nil {
fmt.Println(err)
ws.logger.Error("error setting read deadline", zap.Error(err))
break
}

Expand All @@ -75,7 +78,7 @@ func (ws *WebSocket) AddConnection(ctx context.Context, w http.ResponseWriter, r
if message.Type == "ping" {
errWrite := conn.WriteJSON(types.WebSocketMessage{Type: "pong"})
if errWrite != nil {
fmt.Println(errWrite)
ws.logger.Error("error writing pong message", zap.Error(errWrite))
}
}
continue
Expand All @@ -86,7 +89,7 @@ func (ws *WebSocket) AddConnection(ctx context.Context, w http.ResponseWriter, r
break
}

fmt.Println(err)
ws.logger.Error("error reading json message", zap.Error(err))
}
conn.Close()
ws.connections[retrospectiveID][i] = nil
Expand Down Expand Up @@ -119,7 +122,7 @@ func (w *WebSocket) sendMessageToRetro(ctx context.Context, message types.WebSoc
}
err := conn.WriteJSON(message)
if err != nil {
log.Printf("Error sending message %+v to connection: %v", message, err)
w.logger.Error("error sending message to connection", zap.Any("message", message), zap.Error(err))
}
}

Expand Down Expand Up @@ -170,11 +173,11 @@ func (w *WebSocket) DeleteQuestion(ctx context.Context, id uuid.UUID) (*types.Qu
return nil, w.sendMessageToRetro(ctx, message, nil)
}

func (s *WebSocket) GetOldRetrospectives(ctx context.Context, date time.Time) ([]uuid.UUID, error) {
func (*WebSocket) GetOldRetrospectives(ctx context.Context, date time.Time) ([]uuid.UUID, error) {
panic("unimplemented")
}

func (s *WebSocket) GetAllRetrospectives(ctx context.Context) ([]uuid.UUID, error) {
func (*WebSocket) GetAllRetrospectives(ctx context.Context) ([]uuid.UUID, error) {
panic("unimplemented")
}

Expand Down
9 changes: 5 additions & 4 deletions internal/schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"api/config"
"api/internal/service"
"context"
"log"
"time"

"go.uber.org/fx"
"go.uber.org/zap"
)

type Schedule struct {
service *service.Service
config *config.Config
logger *zap.Logger
stopCh chan struct{}
}

Expand Down Expand Up @@ -54,7 +55,7 @@ func (s *Schedule) start() {
case <-ticker.C:
s.cleanUp()
case <-s.stopCh:
log.Println("stopping schedule")
s.logger.Info("stopping schedule")
return
}
}
Expand All @@ -66,9 +67,9 @@ func (s *Schedule) stop() {
}

func (s *Schedule) cleanUp() {
log.Println("starting clean up routine")
s.logger.Info("starting clean up routine")
ctx := context.Background()
if err := s.service.CleanUpRetros(ctx); err != nil {
log.Printf("error running clean up routine: %s", err.Error())
s.logger.Error("error running clean up routine", zap.Error(err))
}
}
Loading