This helps avoid issues where model classes are not evaluated. If the models are not properly imported then Sqlalchemy can explode during database initialization since the mapped columns have not yet been registered. Later, having the models all imported into __init__.py will help with setting up a test db since it will ensure that the registrations for all models is happening as the classes are evaluated during the imports in the package init file.
70 lines
2 KiB
Python
70 lines
2 KiB
Python
#!/usr/bin/env python
|
|
|
|
from src.services.router import Router
|
|
from src.services.notifications import NotificationService, get_notification_service
|
|
from src.services.email import EmailService, get_email_service
|
|
from src.services.auth import AuthService
|
|
from src.services.users.service import UserService
|
|
from dataclasses import dataclass
|
|
from src.infra import get_database, UserRepoImpl
|
|
from waitress import serve
|
|
from flask import Flask
|
|
import argparse
|
|
import logging
|
|
|
|
from src.config import parse_config, Config
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@dataclass
|
|
class AppServices:
|
|
users: UserService
|
|
auth: AuthService
|
|
email: EmailService
|
|
notifications: NotificationService
|
|
router: Router
|
|
|
|
|
|
class MyFlask(Flask):
|
|
services: AppServices
|
|
|
|
|
|
def create_app(name: str, config: Config) -> Flask:
|
|
logging.basicConfig(level=config.logging.level, format=('%(asctime)s %(levelname)s [%(name)s] %(message)s'))
|
|
app = MyFlask(name)
|
|
|
|
# Configure Services
|
|
database = get_database(config.database)
|
|
user_repo = UserRepoImpl(database)
|
|
email_service = get_email_service(config.email)
|
|
auth_service = AuthService(config.auth)
|
|
router = Router(config.host, config.port)
|
|
|
|
app.services = AppServices(
|
|
users=UserService(repo=user_repo, email_service=email_service, auth_service=auth_service, router=router),
|
|
auth=auth_service,
|
|
email=email_service,
|
|
notifications=get_notification_service(email_service),
|
|
router=router,
|
|
)
|
|
|
|
if config.host != '0.0.0.0':
|
|
app.config['SERVER_NAME'] = config.host
|
|
|
|
return app
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser('Cereal', description='run the Cereal server')
|
|
parser.add_argument('--config', '-c', help='The path to the configuration file, expects JSON', required=True)
|
|
args = parser.parse_args()
|
|
|
|
config = parse_config(args.config)
|
|
app = create_app('Cereal', config)
|
|
logger.info(str(config))
|
|
serve(app, host=config.host, port=config.port)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|