diff --git a/api/kiwi_vpn_api/config.py b/api/kiwi_vpn_api/config.py index 531b69a..f577172 100644 --- a/api/kiwi_vpn_api/config.py +++ b/api/kiwi_vpn_api/config.py @@ -29,14 +29,16 @@ class Settings(BaseSettings): production_mode: bool = False data_dir: Path = Path("./tmp") + api_v1_prefix: str = "api/v1" openapi_url: str = "/openapi.json" docs_url: str | None = "/docs" redoc_url: str | None = "/redoc" - @staticmethod + @classmethod + @property @functools.lru_cache - def get() -> Settings: - return Settings() + def _(cls) -> Settings: + return cls() @property def config_file(self) -> Path: @@ -61,7 +63,7 @@ class DBConfig(BaseModel): user: str | None = None password: str | None = None host: str | None = None - database: str | None = Settings.get().data_dir.joinpath("vpn.db") + database: str | None = Settings._.data_dir.joinpath("vpn.db") mysql_driver: str = "pymysql" mysql_args: list[str] = ["charset=utf8mb4"] @@ -201,7 +203,7 @@ class Config(BaseModel): return cls.__singleton try: - with open(Settings.get().config_file, "r") as config_file: + with open(Settings._.config_file, "r") as config_file: cls.__singleton = Config.parse_obj(json.load(config_file)) return cls.__singleton @@ -222,5 +224,5 @@ class Config(BaseModel): Save configuration to config file """ - with open(Settings.get().config_file, "w") as config_file: + with open(Settings._.config_file, "w") as config_file: config_file.write(self.json(indent=2)) diff --git a/api/kiwi_vpn_api/main.py b/api/kiwi_vpn_api/main.py index c033959..86ae4f2 100755 --- a/api/kiwi_vpn_api/main.py +++ b/api/kiwi_vpn_api/main.py @@ -16,9 +16,6 @@ from .config import Config, Settings from .db import Connection, User from .routers import main_router -settings = Settings.get() - - app = FastAPI( title="kiwi-vpn API", description="This API enables the `kiwi-vpn` service.", @@ -30,12 +27,12 @@ app = FastAPI( "name": "MIT License", "url": "https://opensource.org/licenses/mit-license.php", }, - openapi_url=settings.openapi_url, - docs_url=settings.docs_url if not settings.production_mode else None, - redoc_url=settings.redoc_url if not settings.production_mode else None, + openapi_url=Settings._.openapi_url, + docs_url=Settings._.docs_url if not Settings._.production_mode else None, + redoc_url=Settings._.redoc_url if not Settings._.production_mode else None, ) -app.include_router(main_router) +app.include_router(main_router, prefix=f"/{Settings._.api_v1_prefix}") @app.on_event("startup")