Settings.get() -> Settings._

This commit is contained in:
Jörn-Michael Miehe 2022-03-28 20:04:19 +00:00
parent 799b2f7585
commit aa8563995e
2 changed files with 12 additions and 13 deletions

View file

@ -29,14 +29,16 @@ class Settings(BaseSettings):
production_mode: bool = False production_mode: bool = False
data_dir: Path = Path("./tmp") data_dir: Path = Path("./tmp")
api_v1_prefix: str = "api/v1"
openapi_url: str = "/openapi.json" openapi_url: str = "/openapi.json"
docs_url: str | None = "/docs" docs_url: str | None = "/docs"
redoc_url: str | None = "/redoc" redoc_url: str | None = "/redoc"
@staticmethod @classmethod
@property
@functools.lru_cache @functools.lru_cache
def get() -> Settings: def _(cls) -> Settings:
return Settings() return cls()
@property @property
def config_file(self) -> Path: def config_file(self) -> Path:
@ -61,7 +63,7 @@ class DBConfig(BaseModel):
user: str | None = None user: str | None = None
password: str | None = None password: str | None = None
host: 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_driver: str = "pymysql"
mysql_args: list[str] = ["charset=utf8mb4"] mysql_args: list[str] = ["charset=utf8mb4"]
@ -201,7 +203,7 @@ class Config(BaseModel):
return cls.__singleton return cls.__singleton
try: 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)) cls.__singleton = Config.parse_obj(json.load(config_file))
return cls.__singleton return cls.__singleton
@ -222,5 +224,5 @@ class Config(BaseModel):
Save configuration to config file 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)) config_file.write(self.json(indent=2))

View file

@ -16,9 +16,6 @@ from .config import Config, Settings
from .db import Connection, User from .db import Connection, User
from .routers import main_router from .routers import main_router
settings = Settings.get()
app = FastAPI( app = FastAPI(
title="kiwi-vpn API", title="kiwi-vpn API",
description="This API enables the `kiwi-vpn` service.", description="This API enables the `kiwi-vpn` service.",
@ -30,12 +27,12 @@ app = FastAPI(
"name": "MIT License", "name": "MIT License",
"url": "https://opensource.org/licenses/mit-license.php", "url": "https://opensource.org/licenses/mit-license.php",
}, },
openapi_url=settings.openapi_url, openapi_url=Settings._.openapi_url,
docs_url=settings.docs_url if not settings.production_mode else None, docs_url=Settings._.docs_url if not Settings._.production_mode else None,
redoc_url=settings.redoc_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") @app.on_event("startup")