generated from ensaremirerol/nextjs-fastapi-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.py
71 lines (52 loc) · 1.92 KB
/
bootstrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import logging
import platform
from os import getenv
from pathlib import Path
from dotenv import load_dotenv
from kink import di
from server.services.core.config_service import (
ConfigServiceProtocol,
)
from server.services.core.sqlite_db_service import (
DBService,
)
async def bootstrap():
logger = logging.getLogger(__name__)
logging.getLogger("sqlalchemy.engine").setLevel(logging.ERROR)
logger.info("Bootstrapping...")
load_dotenv()
logger.info("Loading environment variables")
_debug = getenv("DEBUG", False)
if _debug:
logger.setLevel(logging.DEBUG)
di["DEBUG"] = True
logger.info("Debug mode enabled")
# Setting up application directory
str_application_directory = getenv("RDFCRAFT_PATH")
di["APP_DIR"] = (
Path(str_application_directory)
if str_application_directory
else Path.home() / "./rdfcraft"
)
if not di["APP_DIR"].exists():
di["APP_DIR"].mkdir()
logger.info(f"Application directory set to {di['APP_DIR']}")
di["TEMP_DIR"] = di["APP_DIR"] / "temp"
if not di["TEMP_DIR"].exists():
di["TEMP_DIR"].mkdir()
# Detecting system and architecture for later use
di["SYSTEM"] = platform.system()
di["ARCH"] = platform.machine()
logger.info(f"System: {di['SYSTEM']}")
logger.info(f"Architecture: {di['ARCH']}")
logger.info("Initializing services")
import server.services # noqa: F401 Reason: For DI to register services, they need to be imported
# This is a hack to ensure that the DBService is initialized
di[DBService].get_engine()
di[ConfigServiceProtocol].set("system", di["SYSTEM"])
di[ConfigServiceProtocol].set("arch", di["ARCH"])
di[ConfigServiceProtocol].set("app_dir", str(di["APP_DIR"]))
logger.info("Environment variables loaded")
logger.info("Bootstrapping complete, creating window")
async def teardown():
di[DBService].dispose()