Spaces:
Runtime error
Runtime error
Download backend/src/app.py from Wauplin/docker-space-fastapi-react: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/spaces/Wauplin/docker-space-fastapi-react/resolve/main/backend/src/app.py
- Command line
-
hf download hf://spaces/Wauplin/docker-space-fastapi-react/backend/src/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Wauplin/docker-space-fastapi-react/resolve/main/backend/src/app.py
1.61 kB
| from sqlmodel import select | |
| from .app_factory import OptionalOAuth, RequiredOAuth, create_app, get_session | |
| from .schemas import UserCount | |
| # Configure FastAPI app + database | |
| app = create_app() | |
| # Health check endpoint | |
| async def health(): | |
| """Health check endpoint.""" | |
| return {"status": "ok"} | |
| # User endpoints | |
| async def get_user(oauth_info: OptionalOAuth): | |
| """Get user information.""" | |
| return { | |
| "connected": oauth_info is not None, | |
| "username": oauth_info.user_info.preferred_username if oauth_info else None, | |
| } | |
| async def get_user_count(oauth_info: RequiredOAuth) -> UserCount: | |
| """Get user count.""" | |
| with get_session() as session: | |
| statement = select(UserCount).where(UserCount.name == oauth_info.user_info.name) | |
| user_count = session.exec(statement).first() | |
| if user_count is None: | |
| user_count = UserCount(name=oauth_info.user_info.name, count=0) | |
| return user_count | |
| async def increment_user_count(oauth_info: RequiredOAuth) -> UserCount: | |
| """Increment user count.""" | |
| with get_session() as session: | |
| statement = select(UserCount).where(UserCount.name == oauth_info.user_info.name) | |
| user_count = session.exec(statement).first() | |
| if user_count is None: | |
| user_count = UserCount(name=oauth_info.user_info.name, count=0) | |
| user_count.count += 1 | |
| session.add(user_count) | |
| session.commit() | |
| session.refresh(user_count) | |
| return user_count | |