84 lines
2.7 KiB
Python
84 lines
2.7 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 _startup_db():
|
||
|
logger.info("Init DB")
|
||
|
db = DBContext()
|
||
|
host = Environment.get("DB_HOST", str)
|
||
|
port = Environment.get("DB_PORT", int)
|
||
|
user = Environment.get("DB_USER", str)
|
||
|
password = Environment.get("DB_PASSWORD", str)
|
||
|
database = Environment.get("DB_DATABASE", str)
|
||
|
|
||
|
if None in [host, port, user, password, database]:
|
||
|
logger.fatal(
|
||
|
"DB settings are not set correctly",
|
||
|
EnvironmentError("DB settings are not set correctly"),
|
||
|
)
|
||
|
|
||
|
await db.connect(
|
||
|
DatabaseSettings(
|
||
|
host=host, port=port, user=user, password=password, database=database
|
||
|
)
|
||
|
)
|
||
|
Database.init(db)
|
||
|
migrations = MigrationService(db)
|
||
|
await migrations.migrate()
|
||
|
|
||
|
@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 cls._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}})
|