Skip to content
Merged

Dev #49

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ volumes:
redis-data:
services:
db:
image: postgres:18rc1-alpine3.22
image: postgres:18
env_file:
- .env
restart: always
Expand Down
2 changes: 2 additions & 0 deletions fastapi-base/src/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8")

PROJECT_ROOT_PATH: Path = Path(__file__).parent.parent.parent
PROJECT_NAME: str = Field(default="fastapi-base")
PROJECT_DESCRIPTION: str = Field(default="base project for fastapi backend")

LOGGING_CONFIG_PATH: Path = PROJECT_ROOT_PATH / "logconfig.yml"

Expand Down
40 changes: 17 additions & 23 deletions fastapi-base/src/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import logging.config
from contextlib import asynccontextmanager

import sentry_sdk
import yaml # type: ignore
Expand Down Expand Up @@ -32,49 +33,42 @@
}
]

app = FastAPI(
title="fastapi-base",
description="base project for fastapi backend",
version=settings.VERSION,
openapi_url=f"/{settings.VERSION}/openapi.json",
openapi_tags=tags_metadata,
)


# pragma: no cover start
async def on_startup() -> None:
@asynccontextmanager
async def lifespan(app: FastAPI):
await add_postgresql_extension()
redis_client = await get_redis_client()

# Initialize FastAPI-Cache with Redis backend
FastAPICache.init(RedisBackend(redis_client), prefix="fastapi-cache")

if settings.SENTRY_DSN:
sentry_sdk.init(
dsn=settings.SENTRY_DSN,
# Add data like request headers and IP for users,
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
send_default_pii=True,
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# Set profile_session_sample_rate to 1.0 to profile 100%
# of profile sessions.
profile_session_sample_rate=1.0,
# Set profile_lifecycle to "trace" to automatically
# run the profiler on when there is an active transaction
profile_lifecycle="trace",
)
logger.info("FastAPI app running...")

yield

# pragma: no cover stop

if redis_client:
await redis_client.close()
logger.info("Redis connection closed.")

app.add_middleware(CORSMiddleware, allow_origins=["*"])

app.add_event_handler("startup", on_startup)
app = FastAPI(
title=settings.PROJECT_NAME,
description=settings.PROJECT_DESCRIPTION,
version=settings.VERSION,
openapi_url=f"/{settings.VERSION}/openapi.json",
openapi_tags=tags_metadata,
lifespan=lifespan,
)

app.add_middleware(CORSMiddleware, allow_origins=["*"])
add_pagination(app)

app.include_router(routes.home_router)
app.include_router(routes.api_router, prefix=f"/{settings.VERSION}")
2 changes: 1 addition & 1 deletion fastapi-base/src/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BaseModel(SQLModel):
default_factory=uuid_ext_pkg.uuid7,
primary_key=True,
index=True,
sa_column_kwargs={"unique": True},
sa_column_kwargs={"server_default": expression.text("uuid7()"), "unique": True},
)

created_at: Optional[datetime] = Field(
Expand Down
2 changes: 1 addition & 1 deletion ops/docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
expose:
- "6379"
db-test:
image: postgres:18rc1-alpine3.22
image: postgres:18
expose:
- "5432"
environment:
Expand Down