fastapi-mongo-base 0.9.2__tar.gz → 0.9.4__tar.gz

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.
Files changed (31) hide show
  1. {fastapi_mongo_base-0.9.2/src/fastapi_mongo_base.egg-info → fastapi_mongo_base-0.9.4}/PKG-INFO +1 -1
  2. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/pyproject.toml +1 -1
  3. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/core/app_factory.py +31 -23
  4. fastapi_mongo_base-0.9.4/src/fastapi_mongo_base/core/middlewares.py +63 -0
  5. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4/src/fastapi_mongo_base.egg-info}/PKG-INFO +1 -1
  6. fastapi_mongo_base-0.9.2/src/fastapi_mongo_base/core/middlewares.py +0 -22
  7. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/LICENSE.txt +0 -0
  8. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/README.md +0 -0
  9. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/setup.cfg +0 -0
  10. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/__init__.py +0 -0
  11. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/cached.py +0 -0
  12. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/core/__init__.py +0 -0
  13. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/core/config.py +0 -0
  14. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/core/db.py +0 -0
  15. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/core/enums.py +0 -0
  16. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/core/exceptions.py +0 -0
  17. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/handlers.py +0 -0
  18. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/models.py +0 -0
  19. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/routes.py +0 -0
  20. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/schemas.py +0 -0
  21. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/tasks.py +0 -0
  22. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/utils/__init__.py +0 -0
  23. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/utils/aionetwork.py +0 -0
  24. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/utils/basic.py +0 -0
  25. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/utils/bsontools.py +0 -0
  26. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/utils/imagetools.py +0 -0
  27. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base/utils/texttools.py +0 -0
  28. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base.egg-info/SOURCES.txt +0 -0
  29. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base.egg-info/dependency_links.txt +0 -0
  30. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base.egg-info/requires.txt +0 -0
  31. {fastapi_mongo_base-0.9.2 → fastapi_mongo_base-0.9.4}/src/fastapi_mongo_base.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: fastapi-mongo-base
3
- Version: 0.9.2
3
+ Version: 0.9.4
4
4
  Summary: A simple boilerplate application, including models and schemas and abstract router, for FastAPI with MongoDB
5
5
  Author-email: Mahdi Kiani <mahdikiany@gmail.com>
6
6
  Maintainer-email: Mahdi Kiani <mahdikiany@gmail.com>
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "fastapi-mongo-base"
7
- version = "0.9.2"
7
+ version = "0.9.4"
8
8
  description = "A simple boilerplate application, including models and schemas and abstract router, for FastAPI with MongoDB"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -15,18 +15,6 @@ except ImportError:
15
15
  from .config import Settings
16
16
 
17
17
 
18
- async def health(request: fastapi.Request):
19
- return {
20
- "status": "up",
21
- "host": request.url.hostname,
22
- # "host2": request.base_url.hostname,
23
- # "original_host":request.headers.get("x-original-host", "!not found!"),
24
- # "forwarded_host": request.headers.get("X-Forwarded-Host", "forwarded_host"),
25
- # "forwarded_proto": request.headers.get("X-Forwarded-Proto", "forwarded_proto"),
26
- # "forwarded_for": request.headers.get("X-Forwarded-For", "forwarded_for"),
27
- }
28
-
29
-
30
18
  @asynccontextmanager
31
19
  async def lifespan(app: fastapi.FastAPI, worker=None, init_functions=[], settings: Settings = None): # type: ignore
32
20
  """Initialize application services."""
@@ -55,17 +43,23 @@ def setup_exception_handlers(
55
43
  ):
56
44
  exception_handlers = exceptions.EXCEPTION_HANDLERS
57
45
  if usso_handler:
58
- from usso.fastapi.integration import (
59
- EXCEPTION_HANDLERS as USSO_EXCEPTION_HANDLERS,
60
- )
61
-
62
- exception_handlers.update(USSO_EXCEPTION_HANDLERS)
46
+ try:
47
+ from usso.fastapi.integration import (
48
+ EXCEPTION_HANDLERS as USSO_EXCEPTION_HANDLERS,
49
+ )
50
+
51
+ exception_handlers.update(USSO_EXCEPTION_HANDLERS)
52
+ except ImportError:
53
+ pass
63
54
  if ufaas_handler:
64
- from ufaas.fastapi.integration import (
65
- EXCEPTION_HANDLERS as UFAAS_EXCEPTION_HANDLERS,
66
- )
55
+ try:
56
+ from ufaas.fastapi.integration import (
57
+ EXCEPTION_HANDLERS as UFAAS_EXCEPTION_HANDLERS,
58
+ )
67
59
 
68
- exception_handlers.update(UFAAS_EXCEPTION_HANDLERS)
60
+ exception_handlers.update(UFAAS_EXCEPTION_HANDLERS)
61
+ except ImportError:
62
+ pass
69
63
 
70
64
  for exc_class, handler in exception_handlers.items():
71
65
  app.exception_handler(exc_class)(handler)
@@ -98,8 +92,8 @@ def setup_middlewares(
98
92
 
99
93
 
100
94
  def create_app(
101
- *,
102
95
  settings: Settings = None,
96
+ *,
103
97
  title=None,
104
98
  description=None,
105
99
  version="0.1.0",
@@ -121,6 +115,7 @@ def create_app(
121
115
  ufaas_handler: bool = True,
122
116
  original_host_middleware: bool = False,
123
117
  request_log_middleware: bool = False,
118
+ log_route: bool = False,
124
119
  **kwargs,
125
120
  ) -> fastapi.FastAPI:
126
121
  settings.config_logger()
@@ -165,6 +160,18 @@ def create_app(
165
160
  **kwargs,
166
161
  )
167
162
 
163
+ async def health(request: fastapi.Request):
164
+ return {
165
+ "status": "up",
166
+ "host": request.url.hostname,
167
+ "project_name": settings.project_name,
168
+ # "host2": request.base_url.hostname,
169
+ # "original_host":request.headers.get("x-original-host", "!not found!"),
170
+ # "forwarded_host": request.headers.get("X-Forwarded-Host", "forwarded_host"),
171
+ # "forwarded_proto": request.headers.get("X-Forwarded-Proto", "forwarded_proto"),
172
+ # "forwarded_for": request.headers.get("X-Forwarded-For", "forwarded_for"),
173
+ }
174
+
168
175
  async def logs():
169
176
  with open(settings.get_log_config()["info_log_path"], "rb") as f:
170
177
  last_100_lines = deque(f, maxlen=100)
@@ -172,7 +179,8 @@ def create_app(
172
179
  return [line.decode("utf-8") for line in last_100_lines]
173
180
 
174
181
  app.get(f"{base_path}/health")(health)
175
- app.get(f"{base_path}/logs", include_in_schema=False)(logs)
182
+ if log_route:
183
+ app.get(f"{base_path}/logs", include_in_schema=False)(logs)
176
184
 
177
185
  if serve_coverage:
178
186
  app.mount(
@@ -0,0 +1,63 @@
1
+ import fastapi
2
+ from fastapi.responses import PlainTextResponse
3
+ from starlette.middleware.base import BaseHTTPMiddleware
4
+
5
+ import logging
6
+
7
+ from fastapi import Request
8
+ from starlette.middleware.base import BaseHTTPMiddleware
9
+
10
+
11
+ # Create logging middleware
12
+ class RequestLoggingMiddleware(BaseHTTPMiddleware):
13
+ async def dispatch(self, request: Request, call_next):
14
+ # Log the request details
15
+ # logging.info(f"Request: {request.method} {request.url}")
16
+ # logging.info(f"Headers: {request.headers}")
17
+
18
+ # You can also log other request details like body, client IP, etc.
19
+ # Accessing the request body requires it to be async, managing it carefully since it is a stream
20
+
21
+ response = await call_next(request)
22
+
23
+ # You can also log response details here if needed
24
+ logging.info(f"request: {request.method} {request.url} {response.status_code}")
25
+
26
+ return response
27
+
28
+
29
+ class DynamicCORSMiddleware(BaseHTTPMiddleware):
30
+
31
+ async def get_allowed_origins(self, origin, **kwargs):
32
+ from ufaas_fastapi_business.models import Business
33
+
34
+ business = await Business.get_by_origin(origin)
35
+ if not business:
36
+ return []
37
+ return business.config.allowed_origins
38
+
39
+ async def dispatch(self, request: fastapi.Request, call_next):
40
+ origin = request.headers.get("origin")
41
+ allowed_origins = await self.get_allowed_origins(origin=request.url.hostname)
42
+ headers = {}
43
+ if origin in allowed_origins:
44
+ headers = {
45
+ "Access-Control-Allow-Origin": origin,
46
+ "Access-Control-Allow-Credentials": "true",
47
+ "Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
48
+ "Access-Control-Allow-Headers": "Content-Type, Authorization, *",
49
+ }
50
+
51
+ if request.method == "OPTIONS":
52
+ return PlainTextResponse("", status_code=200, headers=headers)
53
+
54
+ # if origin and origin not in allowed_origins:
55
+ # raise BaseHTTPException(
56
+ # status_code=403,
57
+ # error="origin_not_allowed",
58
+ # message="Origin not allowed",
59
+ # )
60
+
61
+ response: fastapi.Response = await call_next(request)
62
+ response.headers.update(headers)
63
+ return response
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: fastapi-mongo-base
3
- Version: 0.9.2
3
+ Version: 0.9.4
4
4
  Summary: A simple boilerplate application, including models and schemas and abstract router, for FastAPI with MongoDB
5
5
  Author-email: Mahdi Kiani <mahdikiany@gmail.com>
6
6
  Maintainer-email: Mahdi Kiani <mahdikiany@gmail.com>
@@ -1,22 +0,0 @@
1
- import logging
2
-
3
- from fastapi import Request
4
- from starlette.middleware.base import BaseHTTPMiddleware
5
-
6
-
7
- # Create logging middleware
8
- class RequestLoggingMiddleware(BaseHTTPMiddleware):
9
- async def dispatch(self, request: Request, call_next):
10
- # Log the request details
11
- # logging.info(f"Request: {request.method} {request.url}")
12
- # logging.info(f"Headers: {request.headers}")
13
-
14
- # You can also log other request details like body, client IP, etc.
15
- # Accessing the request body requires it to be async, managing it carefully since it is a stream
16
-
17
- response = await call_next(request)
18
-
19
- # You can also log response details here if needed
20
- logging.info(f"request: {request.method} {request.url} {response.status_code}")
21
-
22
- return response