fastapi-basic 0.0.8__py3-none-any.whl → 0.0.10__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_basic/base_config.py +8 -1
- fastapi_basic/base_factory.py +21 -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.8.dist-info → fastapi_basic-0.0.10.dist-info}/METADATA +8 -1
- fastapi_basic-0.0.10.dist-info/RECORD +12 -0
- fastapi_basic-0.0.8.dist-info/RECORD +0 -9
- {fastapi_basic-0.0.8.dist-info → fastapi_basic-0.0.10.dist-info}/WHEEL +0 -0
- {fastapi_basic-0.0.8.dist-info → fastapi_basic-0.0.10.dist-info}/top_level.txt +0 -0
fastapi_basic/base_config.py
CHANGED
|
@@ -7,4 +7,11 @@ class BaseConfig(BaseSettings):
|
|
|
7
7
|
OPENAPI_URL: str|None = '/openapi.json'
|
|
8
8
|
|
|
9
9
|
# Logging config
|
|
10
|
-
LOGGER_NAME: str|None = 'uvicorn'
|
|
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,10 +2,13 @@ from abc import ABCMeta, abstractmethod
|
|
|
2
2
|
from functools import lru_cache
|
|
3
3
|
import os, dotenv
|
|
4
4
|
|
|
5
|
+
import watchtower
|
|
5
6
|
from fastapi import FastAPI, Request
|
|
7
|
+
from fastapi.middleware.cors import CORSMiddleware
|
|
6
8
|
from starlette.concurrency import iterate_in_threadpool
|
|
7
9
|
import logging
|
|
8
10
|
|
|
11
|
+
from .ext.aws import init_app as init_aws_app
|
|
9
12
|
from .const import LOG_DEFAULT_LOGGER_NAME, LOG_FMT
|
|
10
13
|
from .utils import update_dict_with_cast
|
|
11
14
|
|
|
@@ -34,6 +37,15 @@ class BaseFactory(metaclass=ABCMeta):
|
|
|
34
37
|
logger.addHandler(stream_handler)
|
|
35
38
|
return logger
|
|
36
39
|
|
|
40
|
+
def __setup_aws_cloud_log(self, app):
|
|
41
|
+
if app.state.aws_session and app.state.config.get("AWS_LOGGROUP_NAME"):
|
|
42
|
+
logs_client = app.state.aws_session.client("logs")
|
|
43
|
+
watchtower_handler = watchtower.CloudWatchLogHandler(
|
|
44
|
+
log_group_name=app.state.config.get("AWS_LOGGROUP_NAME"),
|
|
45
|
+
boto3_client=logs_client, create_log_group=True)
|
|
46
|
+
watchtower_handler.setFormatter(logging.Formatter(LOG_FMT))
|
|
47
|
+
app.logger.addHandler(watchtower_handler)
|
|
48
|
+
|
|
37
49
|
def create_app(self):
|
|
38
50
|
"""
|
|
39
51
|
Create an application instance.
|
|
@@ -43,7 +55,16 @@ class BaseFactory(metaclass=ABCMeta):
|
|
|
43
55
|
app = FastAPI(docs_url=app_config.get('DOCS_URL'), redoc_url=app_config.get('REDOC_URL'), openapi_url=app_config.get('OPENAPI_URL'))
|
|
44
56
|
app.state.config = app_config
|
|
45
57
|
|
|
58
|
+
app.add_middleware(
|
|
59
|
+
CORSMiddleware,
|
|
60
|
+
allow_origins=['*'],
|
|
61
|
+
allow_credentials=True,
|
|
62
|
+
allow_methods=['*'],
|
|
63
|
+
allow_headers=['*'],
|
|
64
|
+
)
|
|
46
65
|
self.__setup_main_logger(app, logger_name=app.state.config.get('LOGGER_NAME', LOG_DEFAULT_LOGGER_NAME), level=logging.DEBUG)
|
|
66
|
+
app.state.aws_session = init_aws_app(app)
|
|
67
|
+
self.__setup_aws_cloud_log(app)
|
|
47
68
|
|
|
48
69
|
@app.middleware("http")
|
|
49
70
|
async def handle_request_headers(request: Request, call_next):
|
|
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
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastapi_basic
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.10
|
|
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,17 +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
|
|
25
29
|
Requires-Dist: python-multipart==0.0.20
|
|
26
30
|
Requires-Dist: requests==2.32.3
|
|
31
|
+
Requires-Dist: s3transfer==0.11.5
|
|
32
|
+
Requires-Dist: six==1.17.0
|
|
27
33
|
Requires-Dist: sniffio==1.3.1
|
|
28
34
|
Requires-Dist: starlette==0.46.1
|
|
29
35
|
Requires-Dist: typing-inspection==0.4.0
|
|
30
36
|
Requires-Dist: typing_extensions==4.13.1
|
|
31
37
|
Requires-Dist: urllib3==2.3.0
|
|
32
38
|
Requires-Dist: uvicorn==0.34.0
|
|
39
|
+
Requires-Dist: watchtower==3.4.0
|
|
33
40
|
Dynamic: author
|
|
34
41
|
Dynamic: author-email
|
|
35
42
|
Dynamic: classifier
|
|
@@ -0,0 +1,12 @@
|
|
|
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=wrQ6YYyqyEme7Hzkc_4HSddQzFZdE8GUsmUQsORTkI4,3255
|
|
4
|
+
fastapi_basic/const.py,sha256=UA7-Eefu_dbWpbFn09Ei_BPb903SExnCgVbnm8_3ALE,99
|
|
5
|
+
fastapi_basic/utils.py,sha256=8ympyQIXsKkxLILTI_7ug85vmKWYKqX0mpADjgHekgU,306
|
|
6
|
+
fastapi_basic/ext/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
fastapi_basic/ext/aws/__init__.py,sha256=N_cwnbv1N-F1SY6NenBOTZe20zGaKFu3UXAu17hFAXY,816
|
|
8
|
+
fastapi_basic/ext/aws/const.py,sha256=Mmb6lo11aZZDAVy-nK-v_JxtVPPKYUo9GB4ihH7XYuM,68
|
|
9
|
+
fastapi_basic-0.0.10.dist-info/METADATA,sha256=cRMHpw2uH7Ub-vDyQogQmPu_vNDMt5Bx_3MG7aaYLBg,1741
|
|
10
|
+
fastapi_basic-0.0.10.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
|
11
|
+
fastapi_basic-0.0.10.dist-info/top_level.txt,sha256=Q9PdwWxaB4dy135MQHiroRYOqArdUSaIeEkzYinN6W0,14
|
|
12
|
+
fastapi_basic-0.0.10.dist-info/RECORD,,
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
fastapi_basic/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
fastapi_basic/base_config.py,sha256=_AMa1BDpCw0e00BUYKzOGVBjHrbAR5WfIWwlRS_Dmug,273
|
|
3
|
-
fastapi_basic/base_factory.py,sha256=3SFewCi2SKpvAZp5kjpGYz_hTxqS_WJa0ecEBicq9mc,2332
|
|
4
|
-
fastapi_basic/const.py,sha256=UA7-Eefu_dbWpbFn09Ei_BPb903SExnCgVbnm8_3ALE,99
|
|
5
|
-
fastapi_basic/utils.py,sha256=8ympyQIXsKkxLILTI_7ug85vmKWYKqX0mpADjgHekgU,306
|
|
6
|
-
fastapi_basic-0.0.8.dist-info/METADATA,sha256=w9LeU9tlfQRfa2whpukhvTotXOZDDdtxC4A1r8nuxi4,1508
|
|
7
|
-
fastapi_basic-0.0.8.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
|
8
|
-
fastapi_basic-0.0.8.dist-info/top_level.txt,sha256=Q9PdwWxaB4dy135MQHiroRYOqArdUSaIeEkzYinN6W0,14
|
|
9
|
-
fastapi_basic-0.0.8.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|