diff --git a/.gitignore b/.gitignore index 505a3b1..bdaa6fe 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ wheels/ # Virtual environments .venv + +config.toml diff --git a/config.example.toml b/config.example.toml new file mode 100644 index 0000000..c311b05 --- /dev/null +++ b/config.example.toml @@ -0,0 +1,10 @@ +[bot] +token = "YOUR_BOT_TOKEN" +creator_id = 123456789 + +[database] +host = "localhost" +port = 5432 +user = "postgres" +password = "password" +database = "dutylog" diff --git a/src/dutylog/application/__main__.py b/src/dutylog/application/__main__.py index f747a61..cac735f 100644 --- a/src/dutylog/application/__main__.py +++ b/src/dutylog/application/__main__.py @@ -17,7 +17,7 @@ async def main(): config = load_config() bot = Bot( - token=config.bot_token, + token=config.bot.token, default=DefaultBotProperties(parse_mode=ParseMode.HTML) ) dp = Dispatcher() diff --git a/src/dutylog/ioc.py b/src/dutylog/infrastructure/ioc.py similarity index 100% rename from src/dutylog/ioc.py rename to src/dutylog/infrastructure/ioc.py diff --git a/src/dutylog/infrastructure/utils/config.py b/src/dutylog/infrastructure/utils/config.py index a9be48b..9bbae0d 100644 --- a/src/dutylog/infrastructure/utils/config.py +++ b/src/dutylog/infrastructure/utils/config.py @@ -1,15 +1,70 @@ from dataclasses import dataclass -from os import getenv +from pathlib import Path + +import tomllib + + +@dataclass +class BotConfig: + token: str + creator_id: int + + +@dataclass +class DatabaseConfig: + host: str + port: int + user: str + password: str + database: str + + @property + def url(self) -> str: + return f"postgresql+asyncpg://{self.user}:{self.password}@{self.host}:{self.port}/{self.database}" @dataclass class Config: - bot_token: str - database_url: str + bot: BotConfig + database: DatabaseConfig def load_config() -> Config: + config_path = Path("config.toml") + + if not config_path.exists(): + raise FileNotFoundError(f"Config file not found: {config_path}") + + with open(config_path, "rb") as f: + data = tomllib.load(f) + + if "bot" not in data: + raise KeyError("Missing required section: bot") + if "database" not in data: + raise KeyError("Missing required section: database") + + bot_data = data["bot"] + if "token" not in bot_data: + raise KeyError("Missing required field: bot.token") + if "creator_id" not in bot_data: + raise KeyError("Missing required field: bot.creator_id") + + database_data = data["database"] + required_db_fields = ["host", "port", "user", "password", "database"] + for field in required_db_fields: + if field not in database_data: + raise KeyError(f"Missing required field: database.{field}") + return Config( - bot_token=getenv("BOT_TOKEN", ""), - database_url=getenv("DATABASE_URL", "sqlite+aiosqlite:///./db.sqlite3"), + bot=BotConfig( + token=bot_data["token"], + creator_id=bot_data["creator_id"], + ), + database=DatabaseConfig( + host=database_data["host"], + port=database_data["port"], + user=database_data["user"], + password=database_data["password"], + database=database_data["database"], + ), )