Working version

This commit is contained in:
Eden Kirin
2023-08-27 00:03:13 +02:00
parent 7f1acec1af
commit 9faca36ce1
5 changed files with 86 additions and 72 deletions

56
main.py
View File

@ -1,56 +1,30 @@
from uuid import UUID
from typing import Any
from litestar import Litestar, get
from litestar.contrib.repository import FilterTypes
from litestar.contrib.repository.exceptions import (
RepositoryError as RepositoryException,
)
from litestar.contrib.repository.filters import (
BeforeAfter,
CollectionFilter,
LimitOffset,
NotInCollectionFilter,
NotInSearchFilter,
OnBeforeAfter,
OrderBy,
SearchFilter,
)
from litestar.contrib.sqlalchemy.plugins import SQLAlchemyPlugin
from litestar.openapi import OpenAPIConfig
from sqlalchemy.ext.asyncio import AsyncSession
from app.controllers import create_router
from app.database import db_config, provide_transaction
from app.lib import exceptions
from app.lib.service import ServiceError
@get("/")
async def hello_world() -> str:
return "Hello, world!"
def create_app(**kwargs: Any) -> Litestar:
return Litestar(
route_handlers=[create_router()],
openapi_config=OpenAPIConfig(title="My API", version="1.0.0"),
dependencies={"session": provide_transaction},
plugins=[SQLAlchemyPlugin(db_config)],
exception_handlers={
RepositoryException: exceptions.repository_exception_to_http_response, # type: ignore[dict-item]
ServiceError: exceptions.service_exception_to_http_response, # type: ignore[dict-item]
},
debug=True,
**kwargs,
)
app = Litestar(
route_handlers=[hello_world, create_router()],
openapi_config=OpenAPIConfig(title="My API", version="1.0.0"),
dependencies={"session": provide_transaction},
plugins=[SQLAlchemyPlugin(db_config)],
exception_handlers={
RepositoryException: exceptions.repository_exception_to_http_response, # type: ignore[dict-item]
ServiceError: exceptions.service_exception_to_http_response, # type: ignore[dict-item]
},
signature_namespace={
"AsyncSession": AsyncSession,
"FilterTypes": FilterTypes,
"BeforeAfter": BeforeAfter,
"CollectionFilter": CollectionFilter,
"LimitOffset": LimitOffset,
"UUID": UUID,
"OrderBy": OrderBy,
"SearchFilter": SearchFilter,
"OnBeforeAfter": OnBeforeAfter,
"NotInSearchFilter": NotInSearchFilter,
"NotInCollectionFilter": NotInCollectionFilter,
},
debug=True,
)
app = create_app()