64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
#!/usr/bin/env python
|
|
|
|
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 import UserService
|
|
from dataclasses import dataclass
|
|
from src.infra.db import get_database
|
|
from src.infra.users import 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
|
|
|
|
|
|
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)
|
|
database = get_database(config.database)
|
|
user_repo = UserRepoImpl(database)
|
|
email_service = get_email_service(config.email)
|
|
app.services = AppServices(
|
|
users=UserService(user_repo),
|
|
auth=AuthService(config.auth),
|
|
email=email_service,
|
|
notifications=get_notification_service(email_service),
|
|
)
|
|
|
|
if config.host:
|
|
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='0.0.0.0' if config.host is None else config.host, port=config.port)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|