mirror of
				https://github.com/ijaric/voice_assistant.git
				synced 2025-10-30 13:03:25 +00:00 
			
		
		
		
	Review fix
This commit is contained in:
		
							parent
							
								
									cb98517f67
								
							
						
					
					
						commit
						cf1a29d520
					
				|  | @ -1,27 +0,0 @@ | ||||||
| from jose import JWTError, jwt |  | ||||||
| from pydantic import ValidationError |  | ||||||
| 
 |  | ||||||
| from fastapi import FastAPI, HTTPException, Security, status |  | ||||||
| from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer |  | ||||||
| from lib.api.schemas.entity import Token |  | ||||||
| from lib.app import settings as lib_app_settings |  | ||||||
| 
 |  | ||||||
| app = FastAPI() |  | ||||||
| settings = lib_app_settings.get_settings() |  | ||||||
| 
 |  | ||||||
| security = HTTPBearer() |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| def get_token_data( |  | ||||||
|     authorization: HTTPAuthorizationCredentials = Security(security), |  | ||||||
| ) -> Token: |  | ||||||
|     token = authorization.credentials |  | ||||||
|     try: |  | ||||||
|         secret_key = settings.jwt_secret_key |  | ||||||
|         payload = jwt.decode(token, secret_key, algorithms=["HS256"]) |  | ||||||
|         return Token(**payload) |  | ||||||
|     except (JWTError, ValidationError): |  | ||||||
|         raise HTTPException( |  | ||||||
|             status_code=status.HTTP_401_UNAUTHORIZED, |  | ||||||
|             detail="Could not validate credentials", |  | ||||||
|         ) |  | ||||||
|  | @ -1,16 +1,15 @@ | ||||||
| import logging | import logging | ||||||
| 
 | 
 | ||||||
| import uvicorn |  | ||||||
| 
 |  | ||||||
| import lib.app.app as app_module | import lib.app.app as app_module | ||||||
| from lib.app import settings as libs_app_settings | import uvicorn | ||||||
|  | from lib.app import settings as app_settings | ||||||
| 
 | 
 | ||||||
| logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| app_instance = app_module.Application() | app_instance = app_module.Application() | ||||||
| app = app_instance.create_app() | app = app_instance.create_app() | ||||||
| settings = libs_app_settings.get_settings() | settings = app_settings.get_settings() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|  | @ -10,7 +10,7 @@ services: | ||||||
|     env_file: |     env_file: | ||||||
|       - .env |       - .env | ||||||
|     ports: |     ports: | ||||||
|       - "${db_port}:5432" |       - "${db_port}:{db_port}" | ||||||
|     volumes: |     volumes: | ||||||
|       - postgres_data:/var/lib/postgresql/data/ |       - postgres_data:/var/lib/postgresql/data/ | ||||||
|     restart: always |     restart: always | ||||||
|  | @ -20,12 +20,12 @@ services: | ||||||
|   api: |   api: | ||||||
|     build: |     build: | ||||||
|       context: . |       context: . | ||||||
|     container_name: fastapi |     container_name: fastapi_app | ||||||
|     image: fastapi_app |     image: fastapi_app | ||||||
|     restart: always |     restart: always | ||||||
|     entrypoint: ["/opt/app/entrypoint.sh"] |     entrypoint: ["/opt/app/entrypoint.sh"] | ||||||
|     ports: |     ports: | ||||||
|       - "${server_port}:5432" |       - "${server_port}:{server_port}" | ||||||
|     depends_on: |     depends_on: | ||||||
|       - db |       - db | ||||||
|     env_file: |     env_file: | ||||||
|  | @ -45,15 +45,6 @@ services: | ||||||
|     networks: |     networks: | ||||||
|       - api_network |       - api_network | ||||||
| 
 | 
 | ||||||
|   redis: |  | ||||||
|     image: redis:7.0.11 |  | ||||||
|     restart: always |  | ||||||
|     command: redis-server --bind 0.0.0.0 |  | ||||||
|     ports: |  | ||||||
|       - "6379:6379" |  | ||||||
|     networks: |  | ||||||
|       - backend_network |  | ||||||
| 
 |  | ||||||
| volumes: | volumes: | ||||||
|   postgres_data: |   postgres_data: | ||||||
| 
 | 
 | ||||||
|  | @ -10,7 +10,7 @@ services: | ||||||
|     env_file: |     env_file: | ||||||
|       - .env |       - .env | ||||||
|     ports: |     ports: | ||||||
|       - "127.0.0.1:${db_port}:5432" |       - "127.0.0.1:${db_port}:{db_port}" | ||||||
|     volumes: |     volumes: | ||||||
|       - postgres_data:/var/lib/postgresql/data/ |       - postgres_data:/var/lib/postgresql/data/ | ||||||
|     restart: always |     restart: always | ||||||
|  | @ -20,7 +20,7 @@ services: | ||||||
|   api: |   api: | ||||||
|     build: |     build: | ||||||
|       context: . |       context: . | ||||||
|     container_name: fastapi |     container_name: fastapi_app | ||||||
|     image: fastapi_app |     image: fastapi_app | ||||||
|     restart: always |     restart: always | ||||||
|     entrypoint: ["/opt/app/entrypoint.sh"] |     entrypoint: ["/opt/app/entrypoint.sh"] | ||||||
|  | @ -43,13 +43,6 @@ services: | ||||||
|     networks: |     networks: | ||||||
|       - api_network |       - api_network | ||||||
| 
 | 
 | ||||||
|   redis: |  | ||||||
|     image: redis:7.0.11 |  | ||||||
|     restart: always |  | ||||||
|     command: redis-server --bind 0.0.0.0 |  | ||||||
|     networks: |  | ||||||
|       - backend_network |  | ||||||
| 
 |  | ||||||
| volumes: | volumes: | ||||||
|   postgres_data: |   postgres_data: | ||||||
| 
 | 
 | ||||||
|  | @ -1,7 +1,6 @@ | ||||||
| import uuid | import uuid | ||||||
| 
 | 
 | ||||||
| import sqlalchemy | import sqlalchemy | ||||||
| from sqlalchemy import Column, DateTime |  | ||||||
| from sqlalchemy.dialects.postgresql import UUID | from sqlalchemy.dialects.postgresql import UUID | ||||||
| from sqlalchemy.ext.declarative import declared_attr | from sqlalchemy.ext.declarative import declared_attr | ||||||
| 
 | 
 | ||||||
|  | @ -9,7 +8,7 @@ from sqlalchemy.ext.declarative import declared_attr | ||||||
| class BaseMixin: | class BaseMixin: | ||||||
|     @declared_attr |     @declared_attr | ||||||
|     def id(cls): |     def id(cls): | ||||||
|         return Column( |         return sqlalchemy.Column( | ||||||
|             UUID(as_uuid=True), |             UUID(as_uuid=True), | ||||||
|             primary_key=True, |             primary_key=True, | ||||||
|             default=uuid.uuid4, |             default=uuid.uuid4, | ||||||
|  | @ -19,8 +18,8 @@ class BaseMixin: | ||||||
| 
 | 
 | ||||||
|     @declared_attr |     @declared_attr | ||||||
|     def created_at(cls): |     def created_at(cls): | ||||||
|         return Column(DateTime, server_default=sqlalchemy.func.now()) |         return sqlalchemy.Column(sqlalchemy.DateTime, server_default=sqlalchemy.sql.func.now()) | ||||||
| 
 | 
 | ||||||
|     @declared_attr |     @declared_attr | ||||||
|     def updated_at(cls): |     def updated_at(cls): | ||||||
|         return Column(DateTime, server_default=sqlalchemy.func.now()) |         return sqlalchemy.Column(sqlalchemy.DateTime, server_default=sqlalchemy.sql.func.now()) | ||||||
							
								
								
									
										27
									
								
								src/fastapi_app/lib/api/services/token.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/fastapi_app/lib/api/services/token.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,27 @@ | ||||||
|  | import fastapi | ||||||
|  | 
 | ||||||
|  | from jose import JWTError, jwt | ||||||
|  | from pydantic import ValidationError | ||||||
|  | 
 | ||||||
|  | from lib.api import schemas as app_schemas | ||||||
|  | from lib.app import settings as app_settings | ||||||
|  | 
 | ||||||
|  | app = fastapi.FastAPI() | ||||||
|  | settings = app_settings.get_settings() | ||||||
|  | 
 | ||||||
|  | security = fastapi.security.HTTPBearer() | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def get_token_data( | ||||||
|  |     authorization: fastapi.security.HTTPAuthorizationCredentials = fastapi.Security(security), | ||||||
|  | ) -> app_schemas.entity.Token: | ||||||
|  |     token = authorization.credentials | ||||||
|  |     try: | ||||||
|  |         secret_key = settings.jwt_secret_key | ||||||
|  |         payload = jwt.decode(token, secret_key, algorithms=["HS256"]) | ||||||
|  |         return app_schemas.entity.Token(**payload) | ||||||
|  |     except (JWTError, ValidationError): | ||||||
|  |         raise fastapi.HTTPException( | ||||||
|  |             status_code=fastapi.status.HTTP_401_UNAUTHORIZED, | ||||||
|  |             detail="Could not validate credentials", | ||||||
|  |         ) | ||||||
|  | @ -1,9 +1,10 @@ | ||||||
| import logging | import logging | ||||||
| import logging.config as logging_config | import logging.config as logging_config | ||||||
| 
 | 
 | ||||||
| import fastapi |  | ||||||
| import lib.api.handlers as admin_api_handlers | import lib.api.handlers as admin_api_handlers | ||||||
| 
 | 
 | ||||||
|  | import fastapi | ||||||
|  | 
 | ||||||
| from .logger import LOGGING | from .logger import LOGGING | ||||||
| from .settings import get_settings | from .settings import get_settings | ||||||
| 
 | 
 | ||||||
|  | @ -1,11 +1,10 @@ | ||||||
| import typing | import typing | ||||||
| 
 | 
 | ||||||
|  | from lib.app import settings as app_settings | ||||||
| from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine | from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine | ||||||
| from sqlalchemy.orm import DeclarativeBase | from sqlalchemy.orm import DeclarativeBase | ||||||
| 
 | 
 | ||||||
| from lib.app import settings as lib_app_settings | settings = app_settings.get_settings() | ||||||
| 
 |  | ||||||
| settings = lib_app_settings.get_settings() |  | ||||||
| 
 | 
 | ||||||
| # Создаём базовый класс для будущих моделей | # Создаём базовый класс для будущих моделей | ||||||
| 
 | 
 | ||||||
|  | @ -1,10 +1,9 @@ | ||||||
| import logging | import logging | ||||||
| 
 | 
 | ||||||
| import faker |  | ||||||
| 
 |  | ||||||
| import backend.user.handlers as user_handlers | import backend.user.handlers as user_handlers | ||||||
| import backend.user.repositories as user_repositories | import backend.user.repositories as user_repositories | ||||||
| import backend.user.services as user_services | import backend.user.services as user_services | ||||||
|  | import faker | ||||||
| 
 | 
 | ||||||
| logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
|  | @ -12,7 +11,9 @@ logger = logging.getLogger(__name__) | ||||||
| class App: | class App: | ||||||
|     def __init__(self): |     def __init__(self): | ||||||
|         self._faker_client = faker.Faker() |         self._faker_client = faker.Faker() | ||||||
|         self._user_faker_client2 = user_repositories.UserFakerClient2(self._faker_client) |         self._user_faker_client2 = user_repositories.UserFakerClient2( | ||||||
|  |             self._faker_client | ||||||
|  |         ) | ||||||
|         self._user_service = user_services.UserService(self._user_faker_client2) |         self._user_service = user_services.UserService(self._user_faker_client2) | ||||||
| 
 | 
 | ||||||
|         self._handler1 = user_handlers.UserHandler1(self._user_service) |         self._handler1 = user_handlers.UserHandler1(self._user_service) | ||||||
|  | @ -28,7 +29,7 @@ class App: | ||||||
|         del self._faker_client |         del self._faker_client | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == "__main__.py": | if __name__ == "__main__": | ||||||
|     app = App() |     app = App() | ||||||
|     try: |     try: | ||||||
|         app.run() |         app.run() | ||||||
|  |  | ||||||
|  | @ -33,5 +33,5 @@ def main() -> None: | ||||||
|         exit(os.EX_SOFTWARE) |         exit(os.EX_SOFTWARE) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == "__main__.py": | if __name__ == "__main__": | ||||||
|     main() |     main() | ||||||
|  |  | ||||||
|  | @ -1,7 +1,6 @@ | ||||||
| import json | import json | ||||||
| 
 | 
 | ||||||
| import aiohttp.web as aiohttp_web | import aiohttp.web as aiohttp_web | ||||||
| 
 |  | ||||||
| import lib.utils.aiohttp as aiohttp_utils | import lib.utils.aiohttp as aiohttp_utils | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -4,11 +4,10 @@ import logging | ||||||
| import typing | import typing | ||||||
| 
 | 
 | ||||||
| import aiohttp.web as aiohttp_web | import aiohttp.web as aiohttp_web | ||||||
| import typing_extensions |  | ||||||
| 
 |  | ||||||
| import lib.api.rest.v1.health as health_handlers | import lib.api.rest.v1.health as health_handlers | ||||||
| import lib.app.errors as app_errors | import lib.app.errors as app_errors | ||||||
| import lib.app.settings as app_settings | import lib.app.settings as app_settings | ||||||
|  | import typing_extensions | ||||||
| 
 | 
 | ||||||
| logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
|  | @ -103,7 +102,9 @@ class Application: | ||||||
|             logger.info("HTTP server has been interrupted") |             logger.info("HTTP server has been interrupted") | ||||||
|         except BaseException as unexpected_error: |         except BaseException as unexpected_error: | ||||||
|             logger.exception("HTTP server failed to start") |             logger.exception("HTTP server failed to start") | ||||||
|             raise app_errors.StartServerError("HTTP server failed to start") from unexpected_error |             raise app_errors.StartServerError( | ||||||
|  |                 "HTTP server failed to start" | ||||||
|  |             ) from unexpected_error | ||||||
| 
 | 
 | ||||||
|     async def dispose(self) -> None: |     async def dispose(self) -> None: | ||||||
|         logger.info("Application is shutting down...") |         logger.info("Application is shutting down...") | ||||||
|  | @ -121,7 +122,9 @@ class Application: | ||||||
| 
 | 
 | ||||||
|         if len(dispose_errors) != 0: |         if len(dispose_errors) != 0: | ||||||
|             logger.error("Application has shut down with errors") |             logger.error("Application has shut down with errors") | ||||||
|             raise app_errors.DisposeError("Application has shut down with errors, see logs above") |             raise app_errors.DisposeError( | ||||||
|  |                 "Application has shut down with errors, see logs above" | ||||||
|  |             ) | ||||||
| 
 | 
 | ||||||
|         logger.info("Application has successfully shut down") |         logger.info("Application has successfully shut down") | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user