Add logger to server

This commit is contained in:
2024-12-12 21:06:15 +01:00
parent 9d82b6426c
commit 1ee8d94789
2 changed files with 13 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package app
import (
"novamd/internal/auth"
"novamd/internal/db"
"novamd/internal/logging"
"novamd/internal/storage"
)
@@ -11,6 +12,7 @@ type Options struct {
Config *Config
Database db.Database
Storage storage.Manager
Logger logging.Logger
JWTManager auth.JWTManager
SessionManager auth.SessionManager
CookieService auth.CookieManager
@@ -33,6 +35,12 @@ func DefaultOptions(cfg *Config) (*Options, error) {
// Initialize storage
storageManager := storage.NewService(cfg.WorkDir)
// Initialize logger
logger, err := logging.New(cfg.LogDir, cfg.LogLevel, cfg.ConsoleOutput)
if err != nil {
return nil, err
}
// Initialize auth services
jwtManager, sessionService, cookieService, err := initAuth(cfg, database)
if err != nil {
@@ -48,6 +56,7 @@ func DefaultOptions(cfg *Config) (*Options, error) {
Config: cfg,
Database: database,
Storage: storageManager,
Logger: logger,
JWTManager: jwtManager,
SessionManager: sessionService,
CookieService: cookieService,

View File

@@ -1,7 +1,7 @@
package app
import (
"log"
"log/slog"
"net/http"
"github.com/go-chi/chi/v5"
@@ -11,6 +11,7 @@ import (
type Server struct {
router *chi.Mux
options *Options
logger *slog.Logger
}
// NewServer creates a new server instance with the given options
@@ -18,6 +19,7 @@ func NewServer(options *Options) *Server {
return &Server{
router: setupRouter(*options),
options: options,
logger: options.Logger.App(),
}
}
@@ -25,7 +27,7 @@ func NewServer(options *Options) *Server {
func (s *Server) Start() error {
// Start server
addr := ":" + s.options.Config.Port
log.Printf("Server starting on port %s", s.options.Config.Port)
s.logger.Info("Starting server", "address", addr)
return http.ListenAndServe(addr, s.router)
}