open-redirect/api/src/startup.py

60 lines
1.9 KiB
Python

from flask_cors import CORS
from api.api import app
from api.auth.keycloak_client import Keycloak
from core.database.database import Database
from core.database.database_settings import DatabaseSettings
from core.database.db_context import DBContext
from core.environment import Environment
from core.logger import Logger
from data.seeder.api_key_seeder import ApiKeySeeder
from data.seeder.file_hash_seeder import FileHashSeeder
from data.seeder.permission_seeder import PermissionSeeder
from data.seeder.role_seeder import RoleSeeder
from data.seeder.short_url_seeder import ShortUrlSeeder
from data.service.migration_service import MigrationService
from service.file_service import FileService
logger = Logger(__name__)
class Startup:
@staticmethod
async def _seed_data():
seeders = [
PermissionSeeder,
RoleSeeder,
ApiKeySeeder,
FileHashSeeder,
ShortUrlSeeder,
]
for seeder in [x() for x in seeders]:
await seeder.seed()
@staticmethod
def _startup_keycloak():
logger.info("Init Keycloak")
Keycloak.init()
@classmethod
async def configure(cls):
Logger.set_level(Environment.get("LOG_LEVEL", str, "info"))
Environment.set_environment(Environment.get("ENVIRONMENT", str, "production"))
logger.info(f"Environment: {Environment.get_environment()}")
app.debug = Environment.get_environment() == "development"
await Database.startup_db()
await FileService.clean_files()
await cls._seed_data()
cls._startup_keycloak()
client_urls = Environment.get("CLIENT_URLS", str)
if client_urls is None:
raise EnvironmentError("CLIENT_URLS not set")
origins = client_urls.split(",")
CORS(app, support_credentials=True, resources={r"/api/*": {"origins": origins}})