-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from a5chin/feature/settings
Add settings
- Loading branch information
Showing
20 changed files
with
229 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
IS_LOCAL=True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
## Environment Variables | ||
- `.env` | ||
- Use this file when you want to set environment variables for the project. | ||
- `.env.local` | ||
- Use this file when you want to set environment variables for the local environment. | ||
|
||
Addendum environment variables to `tools/config/settings.py`: | ||
```{.py title="tools/config/settings.py" hl_lines="9"} | ||
class Settings(BaseSettings): | ||
"""Environment variables settings.""" | ||
|
||
model_config = SettingsConfigDict( | ||
env_file=(".env", ".env.local"), | ||
env_file_encoding="utf-8", | ||
) | ||
|
||
DEBUG: bool = False | ||
IS_LOCAL: bool = False | ||
``` | ||
|
||
## FastAPI | ||
```python | ||
from fastapi import FastAPI | ||
|
||
from tools import Settings | ||
|
||
|
||
settings = Settings() | ||
app = FastAPI(**settings.fastapi_kwargs) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
How to use tools in this repository. | ||
|
||
- [How to use config](config.md) | ||
- [How to use logger](logger.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import pytest | ||
|
||
from tools import Settings | ||
|
||
|
||
@pytest.fixture | ||
def settings() -> Settings: | ||
"""Fixture for settings.""" | ||
return Settings() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import pytest | ||
|
||
from tools import Settings | ||
from tools.config import FastAPIKwArgs | ||
|
||
|
||
class TestSettings: | ||
"""Test class for Settings.""" | ||
|
||
@pytest.mark.usefixtures("settings") | ||
def test_local(self, settings: Settings) -> None: | ||
"""Test local settings.""" | ||
assert settings.IS_LOCAL | ||
|
||
@pytest.mark.usefixtures("settings") | ||
def test_fastapi_kwargs(self, settings: Settings) -> None: | ||
"""Test fastapi_kwargs.""" | ||
assert ( | ||
settings.fastapi_kwargs | ||
== FastAPIKwArgs( | ||
debug=False, | ||
title="FastAPI", | ||
summary=None, | ||
description="", | ||
version="0.1.0", | ||
openapi_url="/openapi.json", | ||
docs_url="/docs", | ||
redoc_url="/redoc", | ||
openapi_prefix="", | ||
).model_dump() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
"""Tools.""" | ||
|
||
from tools.config import Settings | ||
from tools.logger import Logger, LogType | ||
|
||
__all__ = [ | ||
"LogType", | ||
"Logger", | ||
"Settings", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Settings.""" | ||
|
||
from tools.config.fastapi import FastAPIKwArgs | ||
from tools.config.settings import Settings | ||
|
||
__all__ = [ | ||
"FastAPIKwArgs", | ||
"Settings", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class FastAPIKwArgs(BaseModel): | ||
"""FastAPI kwargs.""" | ||
|
||
debug: bool | ||
title: str | ||
version: str | ||
summary: str | None | ||
description: str | ||
openapi_url: str | ||
docs_url: str | ||
redoc_url: str | ||
openapi_prefix: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import Any | ||
|
||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
from tools.config.fastapi import FastAPIKwArgs | ||
|
||
|
||
class Settings(BaseSettings): | ||
"""Environment variables settings.""" | ||
|
||
model_config = SettingsConfigDict( | ||
env_file=(".env", ".env.local"), | ||
env_file_encoding="utf-8", | ||
) | ||
|
||
IS_LOCAL: bool = False | ||
|
||
debug: bool = False | ||
title: str = "FastAPI" | ||
summary: str | None = None | ||
description: str = "" | ||
version: str = "0.1.0" | ||
openapi_url: str = "/openapi.json" | ||
docs_url: str = "/docs" | ||
redoc_url: str = "/redoc" | ||
openapi_prefix: str = "" | ||
api_prefix_v1: str = "/api/v1" | ||
allowed_hosts: list[str] = ["*"] | ||
|
||
@property | ||
def fastapi_kwargs(self) -> dict[str, Any]: | ||
"""FastAPI kwargs.""" | ||
return FastAPIKwArgs( | ||
debug=self.debug, | ||
title=self.title, | ||
summary=self.summary, | ||
description=self.description, | ||
version=self.version, | ||
openapi_url=self.openapi_url, | ||
docs_url=self.docs_url, | ||
redoc_url=self.redoc_url, | ||
openapi_prefix=self.openapi_prefix, | ||
).model_dump() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
ef0073d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report