feat(auth): #182 cleanup expired tokens
This commit was merged in pull request #184.
This commit is contained in:
@@ -64,7 +64,7 @@ func Run(ctx context.Context, database *sqlx.DB, migrationsPrefix string, env fu
|
||||
// init server
|
||||
httpServer := &http.Server{
|
||||
Addr: ":" + serverSettings.Port,
|
||||
Handler: createHandler(database, serverSettings),
|
||||
Handler: createHandlerWithServices(ctx, database, serverSettings),
|
||||
ReadHeaderTimeout: 2 * time.Second,
|
||||
}
|
||||
go startServer(httpServer)
|
||||
@@ -73,6 +73,7 @@ func Run(ctx context.Context, database *sqlx.DB, migrationsPrefix string, env fu
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go shutdownServer(httpServer, ctx, &wg)
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return nil
|
||||
@@ -102,7 +103,7 @@ func shutdownServer(s *http.Server, ctx context.Context, wg *sync.WaitGroup) {
|
||||
}
|
||||
}
|
||||
|
||||
func createHandler(d *sqlx.DB, serverSettings *types.Settings) http.Handler {
|
||||
func createHandlerWithServices(ctx context.Context, d *sqlx.DB, serverSettings *types.Settings) http.Handler {
|
||||
var router = http.NewServeMux()
|
||||
|
||||
authDb := db.NewAuthSqlite(d)
|
||||
@@ -126,6 +127,8 @@ func createHandler(d *sqlx.DB, serverSettings *types.Settings) http.Handler {
|
||||
transactionHandler := handler.NewTransaction(transactionService, accountService, treasureChestService, render)
|
||||
transactionRecurringHandler := handler.NewTransactionRecurring(transactionRecurringService, render)
|
||||
|
||||
go dailyTaskTimer(ctx, transactionRecurringService, authService)
|
||||
|
||||
indexHandler.Handle(router)
|
||||
accountHandler.Handle(router)
|
||||
treasureChestHandler.Handle(router)
|
||||
@@ -138,7 +141,6 @@ func createHandler(d *sqlx.DB, serverSettings *types.Settings) http.Handler {
|
||||
|
||||
wrapper := middleware.Wrapper(
|
||||
router,
|
||||
middleware.GenerateRecurringTransactions(transactionRecurringService),
|
||||
middleware.SecurityHeaders(serverSettings),
|
||||
middleware.CacheControl,
|
||||
middleware.CrossSiteRequestForgery(authService),
|
||||
@@ -151,3 +153,24 @@ func createHandler(d *sqlx.DB, serverSettings *types.Settings) http.Handler {
|
||||
|
||||
return wrapper
|
||||
}
|
||||
|
||||
func dailyTaskTimer(ctx context.Context, transactionRecurring service.TransactionRecurring, auth service.Auth) {
|
||||
runDailyTasks(ctx, transactionRecurring, auth)
|
||||
ticker := time.NewTicker(24 * time.Hour)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
runDailyTasks(ctx, transactionRecurring, auth)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func runDailyTasks(ctx context.Context, transactionRecurring service.TransactionRecurring, auth service.Auth) {
|
||||
slog.Info("Running daily tasks")
|
||||
_ = transactionRecurring.GenerateTransactions(ctx)
|
||||
_ = auth.CleanupSessionsAndTokens(ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user