diff --git a/internal/bootstrap/app_bootstrap.go b/internal/bootstrap/app_bootstrap.go index e763c0a..7df88ef 100644 --- a/internal/bootstrap/app_bootstrap.go +++ b/internal/bootstrap/app_bootstrap.go @@ -92,16 +92,28 @@ func (app *BootstrapApp) Setup() error { } } + // Bootstrap database databaseService := service.NewDatabaseService(service.DatabaseServiceConfig{ DatabasePath: app.Config.DatabasePath, }) + + log.Debug().Str("service", fmt.Sprintf("%T", databaseService)).Msg("Initializing service") + + err = databaseService.Init() + + if err != nil { + return fmt.Errorf("failed to initialize database service: %w", err) + } + + database := databaseService.GetDatabase() + + // Create services dockerService := service.NewDockerService() - authService := service.NewAuthService(authConfig, dockerService, ldapService, databaseService) + authService := service.NewAuthService(authConfig, dockerService, ldapService, database) oauthBrokerService := service.NewOAuthBrokerService(app.getOAuthBrokerConfig()) // Initialize services services := []Service{ - databaseService, dockerService, authService, oauthBrokerService, diff --git a/internal/middleware/zerolog_middleware.go b/internal/middleware/zerolog_middleware.go index 877ad4c..f3ca485 100644 --- a/internal/middleware/zerolog_middleware.go +++ b/internal/middleware/zerolog_middleware.go @@ -49,18 +49,24 @@ func (m *ZerologMiddleware) Middleware() gin.HandlerFunc { latency := time.Since(tStart).String() - // logPath check if the path should be logged normally or with debug + subLogger := log.With().Str("method", method). + Str("path", path). + Str("address", address). + Str("client_ip", clientIP). + Int("status", code). + Str("latency", latency).Logger() + if m.logPath(method + " " + path) { switch { - case code >= 200 && code < 300: - log.Info().Str("method", method).Str("path", path).Str("address", address).Str("clientIp", clientIP).Int("status", code).Str("latency", latency).Msg("Request") - case code >= 300 && code < 400: - log.Warn().Str("method", method).Str("path", path).Str("address", address).Str("clientIp", clientIP).Int("status", code).Str("latency", latency).Msg("Request") - case code >= 400: - log.Error().Str("method", method).Str("path", path).Str("address", address).Str("clientIp", clientIP).Int("status", code).Str("latency", latency).Msg("Request") + case code >= 400 && code < 500: + subLogger.Warn().Msg("Client Error") + case code >= 500: + subLogger.Error().Msg("Server Error") + default: + subLogger.Info().Msg("Request") } } else { - log.Debug().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request") + subLogger.Debug().Msg("Request") } } } diff --git a/internal/service/auth_service.go b/internal/service/auth_service.go index 1536714..b10f5de 100644 --- a/internal/service/auth_service.go +++ b/internal/service/auth_service.go @@ -14,6 +14,7 @@ import ( "github.com/google/uuid" "github.com/rs/zerolog/log" "golang.org/x/crypto/bcrypt" + "gorm.io/gorm" ) type LoginAttempt struct { @@ -39,10 +40,10 @@ type AuthService struct { LoginAttempts map[string]*LoginAttempt LoginMutex sync.RWMutex LDAP *LdapService - Database *DatabaseService + Database *gorm.DB } -func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapService, database *DatabaseService) *AuthService { +func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapService, database *gorm.DB) *AuthService { return &AuthService{ Config: config, Docker: docker, @@ -184,7 +185,6 @@ func (auth *AuthService) IsEmailWhitelisted(email string) bool { } func (auth *AuthService) CreateSessionCookie(c *gin.Context, data *config.SessionCookie) error { - db := auth.Database.GetDatabase() uuid, err := uuid.NewRandom() if err != nil { @@ -210,7 +210,7 @@ func (auth *AuthService) CreateSessionCookie(c *gin.Context, data *config.Sessio Expiry: time.Now().Add(time.Duration(expiry) * time.Second).Unix(), } - err = db.Create(&session).Error + err = auth.Database.Create(&session).Error if err != nil { return err @@ -222,14 +222,13 @@ func (auth *AuthService) CreateSessionCookie(c *gin.Context, data *config.Sessio } func (auth *AuthService) DeleteSessionCookie(c *gin.Context) error { - db := auth.Database.GetDatabase() session, err := auth.GetSessionCookie(c) if err != nil { return err } - res := db.Unscoped().Where("uuid = ?", session.UUID).Delete(&model.Session{}) + res := auth.Database.Unscoped().Where("uuid = ?", session.UUID).Delete(&model.Session{}) if res.Error != nil { return res.Error @@ -241,7 +240,6 @@ func (auth *AuthService) DeleteSessionCookie(c *gin.Context) error { } func (auth *AuthService) GetSessionCookie(c *gin.Context) (config.SessionCookie, error) { - db := auth.Database.GetDatabase() cookie, err := c.Cookie(auth.Config.SessionCookieName) if err != nil { @@ -250,7 +248,7 @@ func (auth *AuthService) GetSessionCookie(c *gin.Context) (config.SessionCookie, var session model.Session - res := db.Unscoped().Where("uuid = ?", cookie).First(&session) + res := auth.Database.Unscoped().Where("uuid = ?", cookie).First(&session) if res.Error != nil { return config.SessionCookie{}, res.Error @@ -263,7 +261,7 @@ func (auth *AuthService) GetSessionCookie(c *gin.Context) (config.SessionCookie, currentTime := time.Now().Unix() if currentTime > session.Expiry { - res := db.Unscoped().Where("uuid = ?", session.UUID).Delete(&model.Session{}) + res := auth.Database.Unscoped().Where("uuid = ?", session.UUID).Delete(&model.Session{}) if res.Error != nil { log.Error().Err(res.Error).Msg("Failed to delete expired session") }