@@ -14,11 +14,10 @@ import (
1414
1515 "github.com/steveiliop56/tinyauth/internal/config"
1616 "github.com/steveiliop56/tinyauth/internal/controller"
17- "github.com/steveiliop56/tinyauth/internal/model "
17+ "github.com/steveiliop56/tinyauth/internal/repository "
1818 "github.com/steveiliop56/tinyauth/internal/utils"
1919
2020 "github.com/rs/zerolog/log"
21- "gorm.io/gorm"
2221)
2322
2423type BootstrapApp struct {
@@ -108,8 +107,18 @@ func (app *BootstrapApp) Setup() error {
108107 log .Trace ().Str ("csrfCookieName" , app .context .csrfCookieName ).Msg ("CSRF cookie name" )
109108 log .Trace ().Str ("redirectCookieName" , app .context .redirectCookieName ).Msg ("Redirect cookie name" )
110109
110+ // Database
111+ db , err := app .SetupDatabase (app .config .DatabasePath )
112+
113+ if err != nil {
114+ return fmt .Errorf ("failed to setup database: %w" , err )
115+ }
116+
117+ // Queries
118+ queries := repository .New (db )
119+
111120 // Services
112- services , err := app .initServices ()
121+ services , err := app .initServices (queries )
113122
114123 if err != nil {
115124 return fmt .Errorf ("failed to initialize services: %w" , err )
@@ -155,9 +164,9 @@ func (app *BootstrapApp) Setup() error {
155164 return fmt .Errorf ("failed to setup routes: %w" , err )
156165 }
157166
158- // Start DB cleanup routine
167+ // Start db cleanup routine
159168 log .Debug ().Msg ("Starting database cleanup routine" )
160- go app .dbCleanup (services . databaseService . GetDatabase () )
169+ go app .dbCleanup (queries )
161170
162171 // If analytics are not disabled, start heartbeat
163172 if ! app .config .DisableAnalytics {
@@ -247,16 +256,16 @@ func (app *BootstrapApp) heartbeat() {
247256 }
248257}
249258
250- func (app * BootstrapApp ) dbCleanup (db * gorm. DB ) {
259+ func (app * BootstrapApp ) dbCleanup (queries * repository. Queries ) {
251260 ticker := time .NewTicker (time .Duration (30 ) * time .Minute )
252261 defer ticker .Stop ()
253262 ctx := context .Background ()
254263
255264 for ; true ; <- ticker .C {
256265 log .Debug ().Msg ("Cleaning up old database sessions" )
257- _ , err := gorm. G [model. Session ]( db ). Where ( "expiry < ?" , time .Now ().Unix ()). Delete ( ctx )
266+ err := queries . DeleteExpiredSessions ( ctx , time .Now ().Unix ())
258267 if err != nil {
259- log .Error ().Err (err ).Msg ("Failed to cleanup old sessions" )
268+ log .Error ().Err (err ).Msg ("Failed to clean up old database sessions" )
260269 }
261270 }
262271}
0 commit comments