FastAPI-fastkit 1.0.0__py3-none-any.whl → 1.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastapi_fastkit/__init__.py +10 -1
- fastapi_fastkit/backend/main.py +482 -482
- fastapi_fastkit/backend/transducer.py +106 -106
- fastapi_fastkit/cli.py +664 -631
- fastapi_fastkit/core/settings.py +155 -155
- fastapi_fastkit/fastapi_project_template/README.md +90 -90
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/.env-tpl +1 -1
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/.gitignore-tpl +30 -30
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/README.md-tpl +107 -107
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/requirements.txt-tpl +36 -36
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/scripts/format.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/scripts/lint.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/scripts/run-server.sh-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/scripts/test.sh-tpl +6 -6
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/setup.cfg-tpl +14 -14
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/setup.py-tpl +25 -25
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/api/api.py-tpl +9 -9
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/api/routes/items.py-tpl +57 -57
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/core/config.py-tpl +64 -64
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/crud/items.py-tpl +21 -21
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/main.py-tpl +26 -26
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/mocks/mock_items.json-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/schemas/items.py-tpl +22 -22
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/tests/conftest.py-tpl +42 -42
- fastapi_fastkit/fastapi_project_template/fastapi-async-crud/tests/test_items.py-tpl +48 -48
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/.env-tpl +1 -1
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/.gitignore-tpl +191 -191
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/README.md-tpl +156 -156
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/requirements.txt-tpl +36 -36
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/scripts/format.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/scripts/lint.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/scripts/run-server.sh-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/scripts/test.sh-tpl +6 -6
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/setup.cfg-tpl +12 -12
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/setup.py-tpl +27 -27
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/.DS_Store +0 -0
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/api/api.py-tpl +9 -9
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/api/routes/items.py-tpl +172 -172
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/core/config.py-tpl +64 -64
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/crud/items.py-tpl +21 -21
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/helper/exceptions.py-tpl +103 -103
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/helper/pagination.py-tpl +72 -72
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/main.py-tpl +39 -39
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/mocks/mock_items.json-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/schemas/base.py-tpl +49 -49
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/schemas/items.py-tpl +22 -22
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/utils/documents.py-tpl +20 -20
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/tests/conftest.py-tpl +42 -42
- fastapi_fastkit/fastapi_project_template/fastapi-custom-response/tests/test_items.py-tpl +61 -61
- fastapi_fastkit/fastapi_project_template/fastapi-default/README.md-tpl +101 -101
- fastapi_fastkit/fastapi_project_template/fastapi-default/scripts/run-server.sh-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-default/setup.py-tpl +22 -22
- fastapi_fastkit/fastapi_project_template/fastapi-default/src/mocks/mock_items.json-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-default/tests/conftest.py-tpl +30 -30
- fastapi_fastkit/fastapi_project_template/fastapi-default/tests/test_items.py-tpl +44 -44
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/.env-tpl +1 -1
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/.gitignore-tpl +30 -30
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/Dockerfile-tpl +23 -23
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/README.md-tpl +119 -119
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/requirements.txt-tpl +36 -36
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/scripts/format.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/scripts/lint.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/scripts/run-server.sh-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/scripts/test.sh-tpl +6 -6
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/setup.cfg-tpl +13 -13
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/setup.py-tpl +22 -22
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/api/api.py-tpl +6 -6
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/api/routes/items.py-tpl +54 -54
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/core/config.py-tpl +64 -64
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/crud/items.py-tpl +15 -15
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/main.py-tpl +26 -26
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/mocks/mock_items.json-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/schemas/items.py-tpl +19 -19
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/tests/conftest.py-tpl +30 -30
- fastapi_fastkit/fastapi_project_template/fastapi-dockerized/tests/test_items.py-tpl +44 -44
- fastapi_fastkit/fastapi_project_template/fastapi-empty/.env-tpl +1 -1
- fastapi_fastkit/fastapi_project_template/fastapi-empty/setup.py-tpl +22 -22
- fastapi_fastkit/fastapi_project_template/fastapi-empty/src/core/config.py-tpl +75 -75
- fastapi_fastkit/fastapi_project_template/fastapi-empty/src/main.py-tpl +36 -36
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/.env-tpl +9 -9
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/.gitignore-tpl +30 -30
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/Dockerfile-tpl +23 -23
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/README.md-tpl +144 -144
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/alembic.ini-tpl +119 -119
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/docker-compose.yml-tpl +44 -44
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/requirements.txt-tpl +41 -41
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/format.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/lint.sh-tpl +5 -5
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/pre-start.sh-tpl +16 -16
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/run-server.sh-tpl +8 -8
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/test.sh-tpl +10 -10
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/setup.cfg-tpl +13 -13
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/setup.py-tpl +25 -25
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/alembic/README-tpl +1 -1
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/alembic/env.py-tpl +85 -85
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/alembic/script.py.mako-tpl +26 -26
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/alembic/versions/bedcdc35b64a_first_alembic.py-tpl +37 -37
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/api/api.py-tpl +11 -11
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/api/deps.py-tpl +19 -19
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/api/routes/items.py-tpl +109 -109
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/core/config.py-tpl +88 -88
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/core/db.py-tpl +53 -53
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/crud/items.py-tpl +102 -102
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/main.py-tpl +45 -45
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/schemas/items.py-tpl +54 -54
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/utils/backend_pre_start.py-tpl +39 -39
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/utils/init_data.py-tpl +23 -23
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/utils/tests_pre_start.py-tpl +39 -39
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/tests/conftest.py-tpl +63 -63
- fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/tests/test_items.py-tpl +44 -44
- fastapi_fastkit/fastapi_project_template/modules/api/routes/new_route.py-tpl +64 -64
- fastapi_fastkit/fastapi_project_template/modules/crud/new_route.py-tpl +29 -29
- fastapi_fastkit/fastapi_project_template/modules/schemas/new_route.py-tpl +23 -23
- fastapi_fastkit/logs/fastkit_debug_20250702_161423.log +21 -0
- fastapi_fastkit/logs/fastkit_debug_20250702_162706.log +20 -0
- fastapi_fastkit/logs/fastkit_debug_20250702_163010.log +21 -0
- fastapi_fastkit/logs/fastkit_debug_20250702_163526.log +21 -0
- fastapi_fastkit/logs/fastkit_debug_20250702_164053.log +21 -0
- fastapi_fastkit/logs/fastkit_debug_20250702_165220.log +21 -0
- fastapi_fastkit/logs/fastkit_debug_20250702_165647.log +21 -0
- fastapi_fastkit/utils/logging.py +112 -1
- fastapi_fastkit/utils/main.py +140 -129
- fastapi_fastkit-1.0.2.dist-info/METADATA +320 -0
- {fastapi_fastkit-1.0.0.dist-info → fastapi_fastkit-1.0.2.dist-info}/RECORD +127 -119
- {fastapi_fastkit-1.0.0.dist-info → fastapi_fastkit-1.0.2.dist-info}/WHEEL +1 -1
- {fastapi_fastkit-1.0.0.dist-info → fastapi_fastkit-1.0.2.dist-info}/licenses/LICENSE +21 -21
- fastapi_fastkit-1.0.0.dist-info/METADATA +0 -208
- {fastapi_fastkit-1.0.0.dist-info → fastapi_fastkit-1.0.2.dist-info}/entry_points.txt +0 -0
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
# --------------------------------------------------------------------------
|
|
2
|
-
# Configuration module
|
|
3
|
-
# --------------------------------------------------------------------------
|
|
4
|
-
import secrets
|
|
5
|
-
import warnings
|
|
6
|
-
from typing import Annotated, Any, Literal
|
|
7
|
-
|
|
8
|
-
from pydantic import AnyUrl, BeforeValidator, computed_field, model_validator
|
|
9
|
-
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
10
|
-
from typing_extensions import Self
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def parse_cors(v: Any) -> list[str] | str:
|
|
14
|
-
if isinstance(v, str) and not v.startswith("["):
|
|
15
|
-
return [i.strip() for i in v.split(",")]
|
|
16
|
-
elif isinstance(v, list | str):
|
|
17
|
-
return v
|
|
18
|
-
raise ValueError(v)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class Settings(BaseSettings):
|
|
22
|
-
"""Default project configuraion module
|
|
23
|
-
|
|
24
|
-
This base module configures your project SECRET_KEY, CORS origins and project name
|
|
25
|
-
|
|
26
|
-
**Add your project configurations below**
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
For more information, please refer to the following link:
|
|
30
|
-
|
|
31
|
-
https://fastapi.tiangolo.com/advanced/settings/?h=config
|
|
32
|
-
"""
|
|
33
|
-
model_config = SettingsConfigDict(
|
|
34
|
-
env_file=".env",
|
|
35
|
-
env_ignore_empty=True,
|
|
36
|
-
extra="ignore",
|
|
37
|
-
)
|
|
38
|
-
|
|
39
|
-
SECRET_KEY: str = secrets.token_urlsafe(32)
|
|
40
|
-
ENVIRONMENT: Literal["development", "production"] = "development"
|
|
41
|
-
|
|
42
|
-
CLIENT_ORIGIN: str = ""
|
|
43
|
-
|
|
44
|
-
BACKEND_CORS_ORIGINS: Annotated[list[AnyUrl] | str, BeforeValidator(parse_cors)] = (
|
|
45
|
-
[]
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
@computed_field # type: ignore[prop-decorator]
|
|
49
|
-
@property
|
|
50
|
-
def all_cors_origins(self) -> list[str]:
|
|
51
|
-
return [str(origin).rstrip("/") for origin in self.BACKEND_CORS_ORIGINS] + [
|
|
52
|
-
self.CLIENT_ORIGIN
|
|
53
|
-
]
|
|
54
|
-
|
|
55
|
-
PROJECT_NAME: str = "<project_name>"
|
|
56
|
-
|
|
57
|
-
def _check_default_secret(self, var_name: str, value: str | None) -> None:
|
|
58
|
-
if value == "changethis":
|
|
59
|
-
message = (
|
|
60
|
-
f'The value of {var_name} is "changethis", '
|
|
61
|
-
"for security, please change it, at least for deployments."
|
|
62
|
-
)
|
|
63
|
-
if self.ENVIRONMENT == "development":
|
|
64
|
-
warnings.warn(message, stacklevel=1)
|
|
65
|
-
else:
|
|
66
|
-
raise ValueError(message)
|
|
67
|
-
|
|
68
|
-
@model_validator(mode="after")
|
|
69
|
-
def _enforce_non_default_secrets(self) -> Self:
|
|
70
|
-
self._check_default_secret("SECRET_KEY", self.SECRET_KEY)
|
|
71
|
-
|
|
72
|
-
return self
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
settings = Settings() # type: ignore
|
|
1
|
+
# --------------------------------------------------------------------------
|
|
2
|
+
# Configuration module
|
|
3
|
+
# --------------------------------------------------------------------------
|
|
4
|
+
import secrets
|
|
5
|
+
import warnings
|
|
6
|
+
from typing import Annotated, Any, Literal
|
|
7
|
+
|
|
8
|
+
from pydantic import AnyUrl, BeforeValidator, computed_field, model_validator
|
|
9
|
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
10
|
+
from typing_extensions import Self
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def parse_cors(v: Any) -> list[str] | str:
|
|
14
|
+
if isinstance(v, str) and not v.startswith("["):
|
|
15
|
+
return [i.strip() for i in v.split(",")]
|
|
16
|
+
elif isinstance(v, list | str):
|
|
17
|
+
return v
|
|
18
|
+
raise ValueError(v)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Settings(BaseSettings):
|
|
22
|
+
"""Default project configuraion module
|
|
23
|
+
|
|
24
|
+
This base module configures your project SECRET_KEY, CORS origins and project name
|
|
25
|
+
|
|
26
|
+
**Add your project configurations below**
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
For more information, please refer to the following link:
|
|
30
|
+
|
|
31
|
+
https://fastapi.tiangolo.com/advanced/settings/?h=config
|
|
32
|
+
"""
|
|
33
|
+
model_config = SettingsConfigDict(
|
|
34
|
+
env_file=".env",
|
|
35
|
+
env_ignore_empty=True,
|
|
36
|
+
extra="ignore",
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
SECRET_KEY: str = secrets.token_urlsafe(32)
|
|
40
|
+
ENVIRONMENT: Literal["development", "production"] = "development"
|
|
41
|
+
|
|
42
|
+
CLIENT_ORIGIN: str = ""
|
|
43
|
+
|
|
44
|
+
BACKEND_CORS_ORIGINS: Annotated[list[AnyUrl] | str, BeforeValidator(parse_cors)] = (
|
|
45
|
+
[]
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
@computed_field # type: ignore[prop-decorator]
|
|
49
|
+
@property
|
|
50
|
+
def all_cors_origins(self) -> list[str]:
|
|
51
|
+
return [str(origin).rstrip("/") for origin in self.BACKEND_CORS_ORIGINS] + [
|
|
52
|
+
self.CLIENT_ORIGIN
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
PROJECT_NAME: str = "<project_name>"
|
|
56
|
+
|
|
57
|
+
def _check_default_secret(self, var_name: str, value: str | None) -> None:
|
|
58
|
+
if value == "changethis":
|
|
59
|
+
message = (
|
|
60
|
+
f'The value of {var_name} is "changethis", '
|
|
61
|
+
"for security, please change it, at least for deployments."
|
|
62
|
+
)
|
|
63
|
+
if self.ENVIRONMENT == "development":
|
|
64
|
+
warnings.warn(message, stacklevel=1)
|
|
65
|
+
else:
|
|
66
|
+
raise ValueError(message)
|
|
67
|
+
|
|
68
|
+
@model_validator(mode="after")
|
|
69
|
+
def _enforce_non_default_secrets(self) -> Self:
|
|
70
|
+
self._check_default_secret("SECRET_KEY", self.SECRET_KEY)
|
|
71
|
+
|
|
72
|
+
return self
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
settings = Settings() # type: ignore
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
# --------------------------------------------------------------------------
|
|
2
|
-
# Main server application module
|
|
3
|
-
# --------------------------------------------------------------------------
|
|
4
|
-
from contextlib import asynccontextmanager
|
|
5
|
-
|
|
6
|
-
from fastapi import FastAPI
|
|
7
|
-
from starlette.middleware.cors import CORSMiddleware
|
|
8
|
-
|
|
9
|
-
from src.core.config import settings
|
|
10
|
-
|
|
11
|
-
app = FastAPI(
|
|
12
|
-
title=settings.PROJECT_NAME,
|
|
13
|
-
)
|
|
14
|
-
|
|
15
|
-
if settings.all_cors_origins:
|
|
16
|
-
app.add_middleware(
|
|
17
|
-
CORSMiddleware,
|
|
18
|
-
allow_origins=settings.all_cors_origins,
|
|
19
|
-
allow_credentials=True,
|
|
20
|
-
allow_methods=["*"],
|
|
21
|
-
allow_headers=["*"],
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"""Add your FastAPI application main routine here
|
|
26
|
-
|
|
27
|
-
You can attach customized middlewares, api routes, etc.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
For more information, please refer to the following link:
|
|
31
|
-
|
|
32
|
-
https://fastapi.tiangolo.com/tutorial/
|
|
33
|
-
https://fastapi.tiangolo.com/reference/apirouter/?h=apiroute
|
|
34
|
-
https://fastapi.tiangolo.com/tutorial/middleware/?h=middle
|
|
35
|
-
https://fastapi.tiangolo.com/tutorial/cors/
|
|
36
|
-
"""
|
|
1
|
+
# --------------------------------------------------------------------------
|
|
2
|
+
# Main server application module
|
|
3
|
+
# --------------------------------------------------------------------------
|
|
4
|
+
from contextlib import asynccontextmanager
|
|
5
|
+
|
|
6
|
+
from fastapi import FastAPI
|
|
7
|
+
from starlette.middleware.cors import CORSMiddleware
|
|
8
|
+
|
|
9
|
+
from src.core.config import settings
|
|
10
|
+
|
|
11
|
+
app = FastAPI(
|
|
12
|
+
title=settings.PROJECT_NAME,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
if settings.all_cors_origins:
|
|
16
|
+
app.add_middleware(
|
|
17
|
+
CORSMiddleware,
|
|
18
|
+
allow_origins=settings.all_cors_origins,
|
|
19
|
+
allow_credentials=True,
|
|
20
|
+
allow_methods=["*"],
|
|
21
|
+
allow_headers=["*"],
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
"""Add your FastAPI application main routine here
|
|
26
|
+
|
|
27
|
+
You can attach customized middlewares, api routes, etc.
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
For more information, please refer to the following link:
|
|
31
|
+
|
|
32
|
+
https://fastapi.tiangolo.com/tutorial/
|
|
33
|
+
https://fastapi.tiangolo.com/reference/apirouter/?h=apiroute
|
|
34
|
+
https://fastapi.tiangolo.com/tutorial/middleware/?h=middle
|
|
35
|
+
https://fastapi.tiangolo.com/tutorial/cors/
|
|
36
|
+
"""
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
# Backend
|
|
2
|
-
SECRET_KEY=changethis
|
|
3
|
-
|
|
4
|
-
# Postgres
|
|
5
|
-
POSTGRES_SERVER=db
|
|
6
|
-
POSTGRES_PORT=5432
|
|
7
|
-
POSTGRES_USER=postgres
|
|
8
|
-
POSTGRES_PASSWORD=postgres
|
|
9
|
-
POSTGRES_DB=item_db
|
|
1
|
+
# Backend
|
|
2
|
+
SECRET_KEY=changethis
|
|
3
|
+
|
|
4
|
+
# Postgres
|
|
5
|
+
POSTGRES_SERVER=db
|
|
6
|
+
POSTGRES_PORT=5432
|
|
7
|
+
POSTGRES_USER=postgres
|
|
8
|
+
POSTGRES_PASSWORD=postgres
|
|
9
|
+
POSTGRES_DB=item_db
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
.idea
|
|
2
|
-
.ipynb_checkpoints
|
|
3
|
-
.mypy_cache
|
|
4
|
-
.vscode
|
|
5
|
-
__pycache__
|
|
6
|
-
.pytest_cache
|
|
7
|
-
htmlcov
|
|
8
|
-
dist
|
|
9
|
-
site
|
|
10
|
-
.coverage*
|
|
11
|
-
coverage.xml
|
|
12
|
-
.netlify
|
|
13
|
-
test.db
|
|
14
|
-
log.txt
|
|
15
|
-
Pipfile.lock
|
|
16
|
-
env3.*
|
|
17
|
-
env
|
|
18
|
-
docs_build
|
|
19
|
-
site_build
|
|
20
|
-
venv
|
|
21
|
-
docs.zip
|
|
22
|
-
archive.zip
|
|
23
|
-
|
|
24
|
-
# vim temporary files
|
|
25
|
-
*~
|
|
26
|
-
.*.sw?
|
|
27
|
-
.cache
|
|
28
|
-
|
|
29
|
-
# macOS
|
|
30
|
-
.DS_Store
|
|
1
|
+
.idea
|
|
2
|
+
.ipynb_checkpoints
|
|
3
|
+
.mypy_cache
|
|
4
|
+
.vscode
|
|
5
|
+
__pycache__
|
|
6
|
+
.pytest_cache
|
|
7
|
+
htmlcov
|
|
8
|
+
dist
|
|
9
|
+
site
|
|
10
|
+
.coverage*
|
|
11
|
+
coverage.xml
|
|
12
|
+
.netlify
|
|
13
|
+
test.db
|
|
14
|
+
log.txt
|
|
15
|
+
Pipfile.lock
|
|
16
|
+
env3.*
|
|
17
|
+
env
|
|
18
|
+
docs_build
|
|
19
|
+
site_build
|
|
20
|
+
venv
|
|
21
|
+
docs.zip
|
|
22
|
+
archive.zip
|
|
23
|
+
|
|
24
|
+
# vim temporary files
|
|
25
|
+
*~
|
|
26
|
+
.*.sw?
|
|
27
|
+
.cache
|
|
28
|
+
|
|
29
|
+
# macOS
|
|
30
|
+
.DS_Store
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
FROM python:3.12-slim
|
|
2
|
-
|
|
3
|
-
WORKDIR /app
|
|
4
|
-
|
|
5
|
-
RUN apt-get update && \
|
|
6
|
-
apt-get install -y --no-install-recommends \
|
|
7
|
-
build-essential \
|
|
8
|
-
&& rm -rf /var/lib/apt/lists/*
|
|
9
|
-
|
|
10
|
-
COPY requirements.txt .
|
|
11
|
-
COPY setup.py .
|
|
12
|
-
COPY src/ src/
|
|
13
|
-
COPY tests/ tests/
|
|
14
|
-
COPY scripts/ scripts/
|
|
15
|
-
|
|
16
|
-
RUN pip install --no-cache-dir -r requirements.txt
|
|
17
|
-
|
|
18
|
-
ENV PYTHONPATH=/app
|
|
19
|
-
ENV ENVIRONMENT=production
|
|
20
|
-
|
|
21
|
-
EXPOSE 8000
|
|
22
|
-
|
|
23
|
-
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
|
1
|
+
FROM python:3.12-slim
|
|
2
|
+
|
|
3
|
+
WORKDIR /app
|
|
4
|
+
|
|
5
|
+
RUN apt-get update && \
|
|
6
|
+
apt-get install -y --no-install-recommends \
|
|
7
|
+
build-essential \
|
|
8
|
+
&& rm -rf /var/lib/apt/lists/*
|
|
9
|
+
|
|
10
|
+
COPY requirements.txt .
|
|
11
|
+
COPY setup.py .
|
|
12
|
+
COPY src/ src/
|
|
13
|
+
COPY tests/ tests/
|
|
14
|
+
COPY scripts/ scripts/
|
|
15
|
+
|
|
16
|
+
RUN pip install --no-cache-dir -r requirements.txt
|
|
17
|
+
|
|
18
|
+
ENV PYTHONPATH=/app
|
|
19
|
+
ENV ENVIRONMENT=production
|
|
20
|
+
|
|
21
|
+
EXPOSE 8000
|
|
22
|
+
|
|
23
|
+
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
|
@@ -1,144 +1,144 @@
|
|
|
1
|
-
# Dockerized FastAPI Item Management API with PostgreSQL
|
|
2
|
-
|
|
3
|
-
This project is a Docker containerized FastAPI-based item management API application with PostgreSQL database integration.
|
|
4
|
-
|
|
5
|
-
## Requirements
|
|
6
|
-
|
|
7
|
-
- Docker and Docker Compose
|
|
8
|
-
|
|
9
|
-
## Tech Stack
|
|
10
|
-
|
|
11
|
-
- Python 3.12+
|
|
12
|
-
- FastAPI + uvicorn
|
|
13
|
-
- PostgreSQL
|
|
14
|
-
- SQLModel (SQLAlchemy + Pydantic)
|
|
15
|
-
- Alembic
|
|
16
|
-
- Docker & Docker Compose
|
|
17
|
-
- pytest
|
|
18
|
-
- mypy + black + isort
|
|
19
|
-
|
|
20
|
-
## Project Structure
|
|
21
|
-
|
|
22
|
-
````
|
|
23
|
-
.
|
|
24
|
-
├── Dockerfile
|
|
25
|
-
├── README.md
|
|
26
|
-
├── docker-compose.yml
|
|
27
|
-
├── requirements.txt
|
|
28
|
-
├── setup.py
|
|
29
|
-
├── alembic.ini
|
|
30
|
-
├── scripts
|
|
31
|
-
│ ├── pre-start.sh
|
|
32
|
-
│ └── test.sh
|
|
33
|
-
├── src
|
|
34
|
-
│ ├── api
|
|
35
|
-
│ │ ├── api.py
|
|
36
|
-
│ │ ├── deps.py
|
|
37
|
-
│ │ └── routes
|
|
38
|
-
│ │ └── items.py
|
|
39
|
-
│ ├── core
|
|
40
|
-
│ │ ├── config.py
|
|
41
|
-
│ │ └── db.py
|
|
42
|
-
│ ├── crud
|
|
43
|
-
│ │ └── items.py
|
|
44
|
-
│ ├── alembic
|
|
45
|
-
│ │ ├── env.py
|
|
46
|
-
│ │ └── versions/
|
|
47
|
-
│ ├── schemas
|
|
48
|
-
│ │ └── items.py
|
|
49
|
-
│ ├── utils
|
|
50
|
-
│ │ ├── backend_pre_start.py
|
|
51
|
-
│ │ ├── init_data.py
|
|
52
|
-
│ │ └── tests_pre_start.py
|
|
53
|
-
│ └── main.py
|
|
54
|
-
└── tests
|
|
55
|
-
├── __init__.py
|
|
56
|
-
├── test_items.py
|
|
57
|
-
└── conftest.py
|
|
58
|
-
````
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
## How to Run
|
|
62
|
-
|
|
63
|
-
### Run with Docker
|
|
64
|
-
|
|
65
|
-
````bash
|
|
66
|
-
# Build and run with Docker Compose
|
|
67
|
-
$ docker-compose up -d
|
|
68
|
-
|
|
69
|
-
# Check container status
|
|
70
|
-
$ docker-compose ps
|
|
71
|
-
````
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
Once the containers are running, you can check the API documentation at:
|
|
75
|
-
|
|
76
|
-
````
|
|
77
|
-
http://localhost:8000/docs # Swagger format
|
|
78
|
-
http://localhost:8000/redoc # ReDoc format
|
|
79
|
-
````
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
### Run in Development Mode (Inside Container)
|
|
83
|
-
|
|
84
|
-
````bash
|
|
85
|
-
# Run the application in development mode (using `--reload` option)
|
|
86
|
-
$ docker-compose exec app uvicorn src.main:app --reload --host 0.0.0.0
|
|
87
|
-
````
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
## API Endpoints
|
|
91
|
-
|
|
92
|
-
| Method | Endpoint | Description |
|
|
93
|
-
|--------|------------------|----------------------------|
|
|
94
|
-
| GET | `/items/` | List all items |
|
|
95
|
-
| GET | `/items/{id}` | Get single item |
|
|
96
|
-
| POST | `/items/` | Create new item |
|
|
97
|
-
| PUT | `/items/{id}` | Update existing item |
|
|
98
|
-
| DELETE | `/items/{id}` | Delete item |
|
|
99
|
-
|
|
100
|
-
## Key Features
|
|
101
|
-
|
|
102
|
-
- Persistent storage using PostgreSQL database
|
|
103
|
-
- Database migrations using Alembic
|
|
104
|
-
- ORM using SQLModel (SQLAlchemy + Pydantic)
|
|
105
|
-
- Full containerization via Docker Compose
|
|
106
|
-
- Automatic loading of initial sample data
|
|
107
|
-
- Pydantic model-based data validation
|
|
108
|
-
- Integrated test cases
|
|
109
|
-
|
|
110
|
-
## Running Tests
|
|
111
|
-
|
|
112
|
-
Tests __must__ be run inside the Docker container:
|
|
113
|
-
|
|
114
|
-
````bash
|
|
115
|
-
# Run all tests inside the container
|
|
116
|
-
$ docker exec fastapi-psql-orm-app-1 bash scripts/test.sh
|
|
117
|
-
|
|
118
|
-
# Run specific test file (example)
|
|
119
|
-
$ docker exec fastapi-psql-orm-app-1 bash -c "cd /app && pytest tests/test_items.py -v"
|
|
120
|
-
````
|
|
121
|
-
|
|
122
|
-
## Database Management
|
|
123
|
-
|
|
124
|
-
After making a new database's change, you must migrate it.
|
|
125
|
-
|
|
126
|
-
````bash
|
|
127
|
-
# Apply database migrations
|
|
128
|
-
$ docker exec fastapi-psql-orm-app-1 bash -c "alembic upgrade head"
|
|
129
|
-
|
|
130
|
-
# Create new migrations
|
|
131
|
-
$ docker exec fastapi-psql-orm-app-1 bash -c "alembic revision --autogenerate -m 'description'"
|
|
132
|
-
````
|
|
133
|
-
|
|
134
|
-
For other FastAPI guides, please refer <https://fastapi.tiangolo.com/tutorial/>
|
|
135
|
-
|
|
136
|
-
# Project Origin
|
|
137
|
-
|
|
138
|
-
This project was created based on the template from the [FastAPI-fastkit](https://github.com/bnbong/FastAPI-fastkit) project.
|
|
139
|
-
|
|
140
|
-
The `FastAPI-fastkit` is an open-source project that helps Python and FastAPI beginners quickly set up a FastAPI-based application development environment in a framework-like structure.
|
|
141
|
-
|
|
142
|
-
### Template Information
|
|
143
|
-
- Template creator: [bnbong](mailto:bbbong9@gmail.com)
|
|
144
|
-
- FastAPI-fastkit project maintainer: [bnbong](mailto:bbbong9@gmail.com)
|
|
1
|
+
# Dockerized FastAPI Item Management API with PostgreSQL
|
|
2
|
+
|
|
3
|
+
This project is a Docker containerized FastAPI-based item management API application with PostgreSQL database integration.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- Docker and Docker Compose
|
|
8
|
+
|
|
9
|
+
## Tech Stack
|
|
10
|
+
|
|
11
|
+
- Python 3.12+
|
|
12
|
+
- FastAPI + uvicorn
|
|
13
|
+
- PostgreSQL
|
|
14
|
+
- SQLModel (SQLAlchemy + Pydantic)
|
|
15
|
+
- Alembic
|
|
16
|
+
- Docker & Docker Compose
|
|
17
|
+
- pytest
|
|
18
|
+
- mypy + black + isort
|
|
19
|
+
|
|
20
|
+
## Project Structure
|
|
21
|
+
|
|
22
|
+
````
|
|
23
|
+
.
|
|
24
|
+
├── Dockerfile
|
|
25
|
+
├── README.md
|
|
26
|
+
├── docker-compose.yml
|
|
27
|
+
├── requirements.txt
|
|
28
|
+
├── setup.py
|
|
29
|
+
├── alembic.ini
|
|
30
|
+
├── scripts
|
|
31
|
+
│ ├── pre-start.sh
|
|
32
|
+
│ └── test.sh
|
|
33
|
+
├── src
|
|
34
|
+
│ ├── api
|
|
35
|
+
│ │ ├── api.py
|
|
36
|
+
│ │ ├── deps.py
|
|
37
|
+
│ │ └── routes
|
|
38
|
+
│ │ └── items.py
|
|
39
|
+
│ ├── core
|
|
40
|
+
│ │ ├── config.py
|
|
41
|
+
│ │ └── db.py
|
|
42
|
+
│ ├── crud
|
|
43
|
+
│ │ └── items.py
|
|
44
|
+
│ ├── alembic
|
|
45
|
+
│ │ ├── env.py
|
|
46
|
+
│ │ └── versions/
|
|
47
|
+
│ ├── schemas
|
|
48
|
+
│ │ └── items.py
|
|
49
|
+
│ ├── utils
|
|
50
|
+
│ │ ├── backend_pre_start.py
|
|
51
|
+
│ │ ├── init_data.py
|
|
52
|
+
│ │ └── tests_pre_start.py
|
|
53
|
+
│ └── main.py
|
|
54
|
+
└── tests
|
|
55
|
+
├── __init__.py
|
|
56
|
+
├── test_items.py
|
|
57
|
+
└── conftest.py
|
|
58
|
+
````
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
## How to Run
|
|
62
|
+
|
|
63
|
+
### Run with Docker
|
|
64
|
+
|
|
65
|
+
````bash
|
|
66
|
+
# Build and run with Docker Compose
|
|
67
|
+
$ docker-compose up -d
|
|
68
|
+
|
|
69
|
+
# Check container status
|
|
70
|
+
$ docker-compose ps
|
|
71
|
+
````
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
Once the containers are running, you can check the API documentation at:
|
|
75
|
+
|
|
76
|
+
````
|
|
77
|
+
http://localhost:8000/docs # Swagger format
|
|
78
|
+
http://localhost:8000/redoc # ReDoc format
|
|
79
|
+
````
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
### Run in Development Mode (Inside Container)
|
|
83
|
+
|
|
84
|
+
````bash
|
|
85
|
+
# Run the application in development mode (using `--reload` option)
|
|
86
|
+
$ docker-compose exec app uvicorn src.main:app --reload --host 0.0.0.0
|
|
87
|
+
````
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
## API Endpoints
|
|
91
|
+
|
|
92
|
+
| Method | Endpoint | Description |
|
|
93
|
+
|--------|------------------|----------------------------|
|
|
94
|
+
| GET | `/items/` | List all items |
|
|
95
|
+
| GET | `/items/{id}` | Get single item |
|
|
96
|
+
| POST | `/items/` | Create new item |
|
|
97
|
+
| PUT | `/items/{id}` | Update existing item |
|
|
98
|
+
| DELETE | `/items/{id}` | Delete item |
|
|
99
|
+
|
|
100
|
+
## Key Features
|
|
101
|
+
|
|
102
|
+
- Persistent storage using PostgreSQL database
|
|
103
|
+
- Database migrations using Alembic
|
|
104
|
+
- ORM using SQLModel (SQLAlchemy + Pydantic)
|
|
105
|
+
- Full containerization via Docker Compose
|
|
106
|
+
- Automatic loading of initial sample data
|
|
107
|
+
- Pydantic model-based data validation
|
|
108
|
+
- Integrated test cases
|
|
109
|
+
|
|
110
|
+
## Running Tests
|
|
111
|
+
|
|
112
|
+
Tests __must__ be run inside the Docker container:
|
|
113
|
+
|
|
114
|
+
````bash
|
|
115
|
+
# Run all tests inside the container
|
|
116
|
+
$ docker exec fastapi-psql-orm-app-1 bash scripts/test.sh
|
|
117
|
+
|
|
118
|
+
# Run specific test file (example)
|
|
119
|
+
$ docker exec fastapi-psql-orm-app-1 bash -c "cd /app && pytest tests/test_items.py -v"
|
|
120
|
+
````
|
|
121
|
+
|
|
122
|
+
## Database Management
|
|
123
|
+
|
|
124
|
+
After making a new database's change, you must migrate it.
|
|
125
|
+
|
|
126
|
+
````bash
|
|
127
|
+
# Apply database migrations
|
|
128
|
+
$ docker exec fastapi-psql-orm-app-1 bash -c "alembic upgrade head"
|
|
129
|
+
|
|
130
|
+
# Create new migrations
|
|
131
|
+
$ docker exec fastapi-psql-orm-app-1 bash -c "alembic revision --autogenerate -m 'description'"
|
|
132
|
+
````
|
|
133
|
+
|
|
134
|
+
For other FastAPI guides, please refer <https://fastapi.tiangolo.com/tutorial/>
|
|
135
|
+
|
|
136
|
+
# Project Origin
|
|
137
|
+
|
|
138
|
+
This project was created based on the template from the [FastAPI-fastkit](https://github.com/bnbong/FastAPI-fastkit) project.
|
|
139
|
+
|
|
140
|
+
The `FastAPI-fastkit` is an open-source project that helps Python and FastAPI beginners quickly set up a FastAPI-based application development environment in a framework-like structure.
|
|
141
|
+
|
|
142
|
+
### Template Information
|
|
143
|
+
- Template creator: [bnbong](mailto:bbbong9@gmail.com)
|
|
144
|
+
- FastAPI-fastkit project maintainer: [bnbong](mailto:bbbong9@gmail.com)
|