diff --git a/cmd/api/main.go b/cmd/api/main.go index c5decc2..15fcb37 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -3,14 +3,13 @@ package main import ( "context" "log" + "log/slog" "net/http" "os" "os/signal" "syscall" "time" - "golang.org/x/exp/slog" - "github.com/prometheus/client_golang/prometheus/promhttp" adapterhttp "github.com/willejs/ports-service/internal/adapter/http" "github.com/willejs/ports-service/internal/adapter/repository" @@ -33,7 +32,7 @@ func main() { defer otelCleanup() // Initialize MemDB - db, err := memdb.NewMemDB() + db, err := memdb.NewMemDB(logger) if err != nil { logger.Error("Failed to initalize memdb", slog.String("component", "main"), slog.Any("error", err)) } @@ -41,11 +40,10 @@ func main() { // Create repository, service, controller, and handler portRepo := repository.NewMemDBPortRepository(db) portService := app.NewPortService(portRepo) - portController := controller.NewPortController(portService) + portController := controller.NewPortController(logger, portService) portHandler := adapterhttp.NewPortHandler(portController) // Load ports data from JSON file - log.Println("Upserting ports from file...") if err := portController.UpsertPortsFromFile(); err != nil { logger.Error("Failed to upsert all ports from file", slog.String("component", "main"), slog.Any("error", err)) os.Exit(1) diff --git a/internal/controller/port_controller.go b/internal/controller/port_controller.go index 1d810cd..a215ba2 100644 --- a/internal/controller/port_controller.go +++ b/internal/controller/port_controller.go @@ -3,7 +3,7 @@ package controller import ( "encoding/json" "io/ioutil" - "log" + "log/slog" "github.com/willejs/ports-service/internal/app" "github.com/willejs/ports-service/internal/domain/entity" @@ -12,11 +12,12 @@ import ( // PortController defines the controller for handling port-related operations. type PortController struct { service *app.PortService + logger *slog.Logger } // NewPortController creates a new PortController. -func NewPortController(service *app.PortService) *PortController { - return &PortController{service: service} +func NewPortController(logger *slog.Logger, service *app.PortService) *PortController { + return &PortController{service: service, logger: logger} } // ListAllPorts retrieves all ports and returns them. @@ -28,9 +29,9 @@ func (c *PortController) ListAllPorts() ([]*entity.Port, error) { func (c *PortController) UpsertPortsFromFile() error { // Load ports data from JSON file // I should open the file and get the handle here instead of reading it all into memory + c.logger.Info("Upserting ports from file", slog.String("component", "controller/port_controller")) data, err := ioutil.ReadFile("../../data/ports.json") if err != nil { - log.Fatalf("error reading ports file: %v", err) return err } @@ -45,6 +46,7 @@ func (c *PortController) UpsertPortsFromFile() error { for key, port := range ports { // the data sometimes missess the code attribute, instead of any logic to santize or normalise data lets just use the key of the map. if port.Code == "" { + c.logger.Debug("Port does not have a code attribute, using key as code", slog.String("component", "controller/port_controller"), slog.String("key", key)) // if port does not have a arrtibute code use key as code port.Code = key } diff --git a/internal/infrastructure/http/logging_middleware.go b/internal/infrastructure/http/logging_middleware.go index a8768e2..2b9ea1c 100644 --- a/internal/infrastructure/http/logging_middleware.go +++ b/internal/infrastructure/http/logging_middleware.go @@ -1,10 +1,9 @@ package http import ( + "log/slog" "net/http" "time" - - "golang.org/x/exp/slog" ) // LoggingMiddleware logs HTTP requests. diff --git a/internal/infrastructure/memdb/memdb.go b/internal/infrastructure/memdb/memdb.go index 5dc9738..f672386 100644 --- a/internal/infrastructure/memdb/memdb.go +++ b/internal/infrastructure/memdb/memdb.go @@ -1,11 +1,13 @@ package memdb import ( + "log/slog" + "github.com/hashicorp/go-memdb" ) // NewMemDB initializes a new MemDB instance. -func NewMemDB() (*memdb.MemDB, error) { +func NewMemDB(logger *slog.Logger) (*memdb.MemDB, error) { // Define the schema for the Port entity schema := &memdb.DBSchema{ Tables: map[string]*memdb.TableSchema{ @@ -23,6 +25,7 @@ func NewMemDB() (*memdb.MemDB, error) { } // Create a new database instance + logger.Info("Creating new MemDB instance", slog.String("component", "infrastructure/memdb")) db, err := memdb.NewMemDB(schema) if err != nil { return nil, err