fastapi-basic 0.0.999992__py3-none-any.whl → 0.1.1__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.
Potentially problematic release.
This version of fastapi-basic might be problematic. Click here for more details.
- fastapi_basic/base_config.py +17 -0
- fastapi_basic/base_factory.py +70 -5
- fastapi_basic/const.py +2 -0
- fastapi_basic/database/__init__.py +0 -0
- fastapi_basic/database/mongodb.py +29 -0
- fastapi_basic/exception/__init__.py +0 -0
- fastapi_basic/exception/base_exception.py +13 -0
- fastapi_basic/ext/__init__.py +0 -0
- fastapi_basic/ext/aws/__init__.py +24 -0
- fastapi_basic/ext/aws/const.py +1 -0
- {fastapi_basic-0.0.999992.dist-info → fastapi_basic-0.1.1.dist-info}/METADATA +10 -2
- fastapi_basic-0.1.1.dist-info/RECORD +16 -0
- {fastapi_basic-0.0.999992.dist-info → fastapi_basic-0.1.1.dist-info}/WHEEL +1 -1
- fastapi_basic-0.0.999992.dist-info/RECORD +0 -8
- {fastapi_basic-0.0.999992.dist-info → fastapi_basic-0.1.1.dist-info}/top_level.txt +0 -0
fastapi_basic/base_config.py
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from pydantic_settings import BaseSettings
|
|
2
|
+
|
|
3
|
+
class BaseConfig(BaseSettings):
|
|
4
|
+
# Swagger docs config
|
|
5
|
+
DOCS_URL: str|None = '/docs'
|
|
6
|
+
REDOC_URL: str|None = '/redoc'
|
|
7
|
+
OPENAPI_URL: str|None = '/openapi.json'
|
|
8
|
+
|
|
9
|
+
# Logging config
|
|
10
|
+
LOGGER_NAME: str|None = 'uvicorn'
|
|
11
|
+
|
|
12
|
+
# AWS
|
|
13
|
+
AWS_ACCESS_KEY_ID: str = ""
|
|
14
|
+
AWS_SECRET_KEY: str = ""
|
|
15
|
+
AWS_REGION: str = ""
|
|
16
|
+
AWS_PARAMETER_PATH_PREFIX: str = ""
|
|
17
|
+
AWS_LOGGROUP_NAME: str = ""
|
fastapi_basic/base_factory.py
CHANGED
|
@@ -2,8 +2,16 @@ from abc import ABCMeta, abstractmethod
|
|
|
2
2
|
from functools import lru_cache
|
|
3
3
|
import os, dotenv
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import watchtower
|
|
6
|
+
from fastapi import FastAPI, HTTPException, Request
|
|
7
|
+
from fastapi.responses import JSONResponse
|
|
8
|
+
from fastapi.middleware.cors import CORSMiddleware
|
|
9
|
+
from starlette.concurrency import iterate_in_threadpool
|
|
10
|
+
import logging
|
|
6
11
|
|
|
12
|
+
from .const import LOG_DEFAULT_LOGGER_NAME, LOG_FMT
|
|
13
|
+
from .ext.aws import init_app as init_aws_app
|
|
14
|
+
from .exception.base_exception import InternalBaseException
|
|
7
15
|
from .utils import update_dict_with_cast
|
|
8
16
|
|
|
9
17
|
class BaseFactory(metaclass=ABCMeta):
|
|
@@ -14,6 +22,32 @@ class BaseFactory(metaclass=ABCMeta):
|
|
|
14
22
|
Each factory should define what config it wants.
|
|
15
23
|
"""
|
|
16
24
|
|
|
25
|
+
def __load_local_config(self):
|
|
26
|
+
dotenv.load_dotenv(dotenv_path='.env', override=True)
|
|
27
|
+
update_dict_with_cast(self.get_app_config(), os.environ)
|
|
28
|
+
|
|
29
|
+
def __setup_main_logger(self, app, logger_name=LOG_DEFAULT_LOGGER_NAME, level=logging.DEBUG):
|
|
30
|
+
logger = self.__setup_logger(app, logger_name, level)
|
|
31
|
+
app.logger = logger
|
|
32
|
+
|
|
33
|
+
def __setup_logger(self, app, logger_name, level=logging.INFO):
|
|
34
|
+
logger = logging.getLogger(logger_name)
|
|
35
|
+
logger.setLevel(level)
|
|
36
|
+
|
|
37
|
+
stream_handler = logging.StreamHandler()
|
|
38
|
+
stream_handler.setFormatter(logging.Formatter(LOG_FMT))
|
|
39
|
+
logger.addHandler(stream_handler)
|
|
40
|
+
return logger
|
|
41
|
+
|
|
42
|
+
def __setup_aws_cloud_log(self, app):
|
|
43
|
+
if app.state.aws_session and app.state.config.get("AWS_LOGGROUP_NAME"):
|
|
44
|
+
logs_client = app.state.aws_session.client("logs")
|
|
45
|
+
watchtower_handler = watchtower.CloudWatchLogHandler(
|
|
46
|
+
log_group_name=app.state.config.get("AWS_LOGGROUP_NAME"),
|
|
47
|
+
boto3_client=logs_client, create_log_group=True)
|
|
48
|
+
watchtower_handler.setFormatter(logging.Formatter(LOG_FMT))
|
|
49
|
+
app.logger.addHandler(watchtower_handler)
|
|
50
|
+
|
|
17
51
|
def create_app(self):
|
|
18
52
|
"""
|
|
19
53
|
Create an application instance.
|
|
@@ -22,8 +56,39 @@ class BaseFactory(metaclass=ABCMeta):
|
|
|
22
56
|
app_config = self.get_app_config()
|
|
23
57
|
app = FastAPI(docs_url=app_config.get('DOCS_URL'), redoc_url=app_config.get('REDOC_URL'), openapi_url=app_config.get('OPENAPI_URL'))
|
|
24
58
|
app.state.config = app_config
|
|
25
|
-
return app
|
|
26
59
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
60
|
+
app.add_middleware(
|
|
61
|
+
CORSMiddleware,
|
|
62
|
+
allow_origins=['*'],
|
|
63
|
+
allow_credentials=True,
|
|
64
|
+
allow_methods=['*'],
|
|
65
|
+
allow_headers=['*'],
|
|
66
|
+
)
|
|
67
|
+
self.__setup_main_logger(app, logger_name=app.state.config.get('LOGGER_NAME', LOG_DEFAULT_LOGGER_NAME), level=logging.DEBUG)
|
|
68
|
+
app.state.aws_session = init_aws_app(app)
|
|
69
|
+
self.__setup_aws_cloud_log(app)
|
|
70
|
+
|
|
71
|
+
@app.exception_handler(InternalBaseException)
|
|
72
|
+
async def http_exception_handler(request: Request, exc: InternalBaseException):
|
|
73
|
+
return JSONResponse(
|
|
74
|
+
status_code=exc.status_code,
|
|
75
|
+
content={
|
|
76
|
+
"code": exc.detail["code"],
|
|
77
|
+
"message": exc.detail["message"],
|
|
78
|
+
"data": exc.detail["data"]
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
@app.middleware("http")
|
|
83
|
+
async def handle_request_headers(request: Request, call_next):
|
|
84
|
+
body = await request.body()
|
|
85
|
+
form = await request.form()
|
|
86
|
+
app.logger.info(f"request.url: {request.url}, method: {request.method}, headers: {request.headers}, body: {body}, form: {form}")
|
|
87
|
+
response = await call_next(request)
|
|
88
|
+
response_body = [section async for section in response.body_iterator]
|
|
89
|
+
response.body_iterator = iterate_in_threadpool(iter(response_body))
|
|
90
|
+
if response_body:
|
|
91
|
+
app.logger.info(f"response_body: {response_body[0].decode()}")
|
|
92
|
+
return response
|
|
93
|
+
|
|
94
|
+
return app
|
fastapi_basic/const.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from beanie import init_beanie
|
|
2
|
+
from motor.motor_asyncio import AsyncIOMotorClient
|
|
3
|
+
|
|
4
|
+
class MongoDB:
|
|
5
|
+
def __init__(self, username: str, password: str, host: str, port: int, db_name: str, ssl: str=None, ssl_ca_certs: str=None):
|
|
6
|
+
self.username = username
|
|
7
|
+
self.password = password
|
|
8
|
+
self.host = host
|
|
9
|
+
self.port = port
|
|
10
|
+
self.db_name = db_name
|
|
11
|
+
self.ssl = ssl
|
|
12
|
+
self.ssl_ca_certs = ssl_ca_certs
|
|
13
|
+
|
|
14
|
+
async def connect(self, document_models: list):
|
|
15
|
+
db_settings = dict(
|
|
16
|
+
username=self.username,
|
|
17
|
+
password=self.password,
|
|
18
|
+
host=self.host,
|
|
19
|
+
port=self.port,
|
|
20
|
+
retryWrites=False
|
|
21
|
+
)
|
|
22
|
+
if self.ssl:
|
|
23
|
+
db_settings["ssl"] = self.ssl
|
|
24
|
+
|
|
25
|
+
if self.ssl_ca_certs and self.ssl_ca_certs != "":
|
|
26
|
+
db_settings["tlsCAFile"] = self.ssl_ca_certs
|
|
27
|
+
|
|
28
|
+
self.client = AsyncIOMotorClient(**db_settings)
|
|
29
|
+
await init_beanie(database=self.client[self.db_name], document_models=document_models)
|
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from fastapi import HTTPException
|
|
2
|
+
from fastapi import status
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class InternalBaseException(HTTPException):
|
|
6
|
+
def __init__(self, status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR, code: str = "internal_server_error",
|
|
7
|
+
message: str = "Internal server error", **kwargs):
|
|
8
|
+
detail = {
|
|
9
|
+
"code": code,
|
|
10
|
+
"message": message,
|
|
11
|
+
"data": kwargs,
|
|
12
|
+
}
|
|
13
|
+
super().__init__(status_code=status_code, detail=detail)
|
|
File without changes
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
|
2
|
+
import boto3
|
|
3
|
+
|
|
4
|
+
from .const import AWS_CONF_KEY
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def init_app(app):
|
|
8
|
+
if not all([app.state.config.get(key) for key in AWS_CONF_KEY]):
|
|
9
|
+
# pylint: disable=no-member
|
|
10
|
+
app.logger.info("Lack AWS credential keys, ignore connect to AWS")
|
|
11
|
+
return None
|
|
12
|
+
|
|
13
|
+
aws_session = boto3.session.Session(
|
|
14
|
+
aws_access_key_id=app.state.config.AWS_ACCESS_KEY_ID,
|
|
15
|
+
aws_secret_access_key=app.state.config.AWS_SECRET_KEY,
|
|
16
|
+
region_name=app.state.config.AWS_REGION
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
# This should be logging when create Logging handlers,
|
|
20
|
+
# But we have too many CloudWatchLogHandler, only print once here.
|
|
21
|
+
if not getattr(app.state.config, "AWS_LOGGROUP_NAME"):
|
|
22
|
+
app.logger.info("Lack AWS configuration keys, ignore AWS CloudWatch log handlers")
|
|
23
|
+
|
|
24
|
+
return aws_session
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
AWS_CONF_KEY = ["AWS_ACCESS_KEY_ID", "AWS_SECRET_KEY", "AWS_REGION"]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: fastapi_basic
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.1.1
|
|
4
4
|
Summary: A short description of your module
|
|
5
5
|
Home-page: https://github.com/szx21023/fastapi-base
|
|
6
6
|
Author: szx21023
|
|
@@ -12,6 +12,8 @@ Requires-Python: >=3.6
|
|
|
12
12
|
Description-Content-Type: text/markdown
|
|
13
13
|
Requires-Dist: annotated-types==0.7.0
|
|
14
14
|
Requires-Dist: anyio==4.9.0
|
|
15
|
+
Requires-Dist: boto3==1.37.36
|
|
16
|
+
Requires-Dist: botocore==1.37.36
|
|
15
17
|
Requires-Dist: certifi==2025.1.31
|
|
16
18
|
Requires-Dist: charset-normalizer==3.4.1
|
|
17
19
|
Requires-Dist: click==8.1.8
|
|
@@ -19,16 +21,22 @@ Requires-Dist: dotenv==0.9.9
|
|
|
19
21
|
Requires-Dist: fastapi==0.115.12
|
|
20
22
|
Requires-Dist: h11==0.14.0
|
|
21
23
|
Requires-Dist: idna==3.10
|
|
24
|
+
Requires-Dist: jmespath==1.0.1
|
|
22
25
|
Requires-Dist: pydantic==2.11.3
|
|
23
26
|
Requires-Dist: pydantic_core==2.33.1
|
|
27
|
+
Requires-Dist: python-dateutil==2.9.0.post0
|
|
24
28
|
Requires-Dist: python-dotenv==1.1.0
|
|
29
|
+
Requires-Dist: python-multipart==0.0.20
|
|
25
30
|
Requires-Dist: requests==2.32.3
|
|
31
|
+
Requires-Dist: s3transfer==0.11.5
|
|
32
|
+
Requires-Dist: six==1.17.0
|
|
26
33
|
Requires-Dist: sniffio==1.3.1
|
|
27
34
|
Requires-Dist: starlette==0.46.1
|
|
28
35
|
Requires-Dist: typing-inspection==0.4.0
|
|
29
36
|
Requires-Dist: typing_extensions==4.13.1
|
|
30
37
|
Requires-Dist: urllib3==2.3.0
|
|
31
38
|
Requires-Dist: uvicorn==0.34.0
|
|
39
|
+
Requires-Dist: watchtower==3.4.0
|
|
32
40
|
Dynamic: author
|
|
33
41
|
Dynamic: author-email
|
|
34
42
|
Dynamic: classifier
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
fastapi_basic/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
fastapi_basic/base_config.py,sha256=nimiiK5LRY9kmx3dBDbN7teO4R8E423-c2iyq9WMcCg,442
|
|
3
|
+
fastapi_basic/base_factory.py,sha256=xrM4tbKjrhtqpzP2hupGJxiNeGsDnXfMYe8urOcq-ss,3801
|
|
4
|
+
fastapi_basic/const.py,sha256=UA7-Eefu_dbWpbFn09Ei_BPb903SExnCgVbnm8_3ALE,99
|
|
5
|
+
fastapi_basic/utils.py,sha256=8ympyQIXsKkxLILTI_7ug85vmKWYKqX0mpADjgHekgU,306
|
|
6
|
+
fastapi_basic/database/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
fastapi_basic/database/mongodb.py,sha256=XW7ApqYkR1Jf7LM1HaZ6jzYgajzuu1whKw5cpsjSO-M,1017
|
|
8
|
+
fastapi_basic/exception/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
fastapi_basic/exception/base_exception.py,sha256=xkpvAI8G96sqXqHgQC_rs2ZPL1liwKVXbXBIPF_8HPQ,473
|
|
10
|
+
fastapi_basic/ext/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
fastapi_basic/ext/aws/__init__.py,sha256=N_cwnbv1N-F1SY6NenBOTZe20zGaKFu3UXAu17hFAXY,816
|
|
12
|
+
fastapi_basic/ext/aws/const.py,sha256=Mmb6lo11aZZDAVy-nK-v_JxtVPPKYUo9GB4ihH7XYuM,68
|
|
13
|
+
fastapi_basic-0.1.1.dist-info/METADATA,sha256=vopBHifYhaFjN8jw_Vm1sL2b7uiWfBpiVU0UAJGV3SY,1740
|
|
14
|
+
fastapi_basic-0.1.1.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
|
15
|
+
fastapi_basic-0.1.1.dist-info/top_level.txt,sha256=Q9PdwWxaB4dy135MQHiroRYOqArdUSaIeEkzYinN6W0,14
|
|
16
|
+
fastapi_basic-0.1.1.dist-info/RECORD,,
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
fastapi_basic/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
fastapi_basic/base_config.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
-
fastapi_basic/base_factory.py,sha256=1FvDmK6tky7iqa5MmeZzaGbQR4pI4UPNJjNYkaDCezg,858
|
|
4
|
-
fastapi_basic/utils.py,sha256=8ympyQIXsKkxLILTI_7ug85vmKWYKqX0mpADjgHekgU,306
|
|
5
|
-
fastapi_basic-0.0.999992.dist-info/METADATA,sha256=hZfpyI6Ee5HYvERYNiMCK_5a7jN_eD2vUaB-1GyX7Xo,1473
|
|
6
|
-
fastapi_basic-0.0.999992.dist-info/WHEEL,sha256=52BFRY2Up02UkjOa29eZOS2VxUrpPORXg1pkohGGUS8,91
|
|
7
|
-
fastapi_basic-0.0.999992.dist-info/top_level.txt,sha256=Q9PdwWxaB4dy135MQHiroRYOqArdUSaIeEkzYinN6W0,14
|
|
8
|
-
fastapi_basic-0.0.999992.dist-info/RECORD,,
|
|
File without changes
|