68 lines
1.9 KiB
Python
68 lines
1.9 KiB
Python
"""Application class - central dependency holder."""
|
|
|
|
import logging
|
|
import threading
|
|
|
|
import argon2
|
|
|
|
from .dns import DNSService
|
|
from .email import EmailService
|
|
from .models import create_tables, init_database
|
|
from .ratelimit import BadLimiter, GoodLimiter
|
|
|
|
|
|
class Application:
|
|
"""
|
|
Central application state holder.
|
|
|
|
Holds configuration and all service instances.
|
|
"""
|
|
|
|
def __init__(self, config):
|
|
"""
|
|
Initialize application with configuration.
|
|
|
|
Args:
|
|
config: Configuration dictionary from TOML file.
|
|
"""
|
|
self.config = config
|
|
self.password_hasher = argon2.PasswordHasher()
|
|
self.shutdown_event = threading.Event()
|
|
|
|
# Service instances (initialized separately)
|
|
self.dns_service = None
|
|
self.email_service = None
|
|
self.good_limiter = None
|
|
self.bad_limiter = None
|
|
|
|
def init_database(self):
|
|
"""Initialize database connection and run migrations."""
|
|
init_database(self.config)
|
|
create_tables()
|
|
logging.debug("Database initialized")
|
|
|
|
def init_dns(self):
|
|
"""Initialize DNS service."""
|
|
self.dns_service = DNSService(self.config)
|
|
logging.info("DNS service initialized")
|
|
|
|
def init_email(self):
|
|
"""Initialize email service."""
|
|
self.email_service = EmailService(self.config)
|
|
logging.info("Email service initialized")
|
|
|
|
def init_rate_limiters(self):
|
|
"""Initialize rate limiters."""
|
|
self.good_limiter = GoodLimiter(self.config)
|
|
self.bad_limiter = BadLimiter(self.config)
|
|
logging.info("Rate limiters initialized")
|
|
|
|
def signal_shutdown(self):
|
|
"""Signal the application to shut down."""
|
|
logging.info("Shutdown signaled")
|
|
self.shutdown_event.set()
|
|
|
|
def is_shutting_down(self):
|
|
"""Check if shutdown has been signaled."""
|
|
return self.shutdown_event.is_set()
|