Skip to content

Commit

Permalink
Merge pull request #55 from andersonresende/main
Browse files Browse the repository at this point in the history
Deploy
  • Loading branch information
andersonresende authored Dec 30, 2024
2 parents 8ad643e + 888af1b commit 717e0ec
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 16 deletions.
8 changes: 8 additions & 0 deletions .pre-commit-config.docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ repos:
language: system
# Only run frontend client generation if frontend files have changed:
files: ^local-shared-data/openapi\.json$
pass_filenames: false
# export requirements is required to vercel deploy
- id: export-requirements
name: Export requirements.txt
entry: sh -c 'docker compose run --rm --no-deps -T backend poetry export --without-hashes -f requirements.txt > requirements.txt'
language: system
# Trigger when poetry.lock is modified
files: poetry.lock
pass_filenames: false
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ repos:
language: system
# Only run frontend client generation if frontend files have changed:
files: openapi\.json$
pass_filenames: false
# export requirements is required to vercel deploy
- id: export-requirements
name: Export requirements.txt
entry: sh -c 'cd fastapi_backend && poetry export --without-hashes -f requirements.txt > requirements.txt'
language: system
files: poetry.lock # Trigger when poetry.lock is modified
pass_filenames: false
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Copy the `.env.example` files to `.env` and update the variables with your own v
```bash
python3 -c "import secrets; print(secrets.token_hex(32))"
```
2. The DATABASE, MAIL, OPENAPI, and FRONTEND_URL settings are ready to use locally.
2. The DATABASE, MAIL, OPENAPI, CORS, and FRONTEND_URL settings are ready to use locally.
3. You can check the .env.example file for more information about the variables.

**Frontend (`nextjs-frontend/.env.local`):**
Expand Down
3 changes: 3 additions & 0 deletions fastapi_backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ USE_CREDENTIALS=False

# Frontend (NextJS)
FRONTEND_URL=http://localhost:3000

# CORS (Allow all origins for development; adjust this to restrict origins in production deployments)
CORS_ORIGINS=["*"]
2 changes: 1 addition & 1 deletion fastapi_backend/alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[alembic]
# path to migration scripts.
# Use forward slashes (/) also on windows to provide an os agnostic path
script_location = alembic
script_location = alembic_migrations

# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions fastapi_backend/api/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from app.main import app # noqa: F401
7 changes: 6 additions & 1 deletion fastapi_backend/app/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from typing import Set

from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
# Database
DATABASE_URL: str
TEST_DATABASE_URL: str
TEST_DATABASE_URL: str = None
EXPIRE_ON_COMMIT: bool = False

# User
Expand All @@ -30,6 +32,9 @@ class Settings(BaseSettings):
# Frontend
FRONTEND_URL: str = "http://localhost:3000"

# CORS
CORS_ORIGINS: Set[str]

model_config = SettingsConfigDict(
env_file=".env", env_file_encoding="utf-8", extra="ignore"
)
Expand Down
4 changes: 3 additions & 1 deletion fastapi_backend/app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from fastapi import Depends
from fastapi_users.db import SQLAlchemyUserDatabase
from sqlalchemy import NullPool
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine

from .config import settings
from .models import Base, User

engine = create_async_engine(settings.DATABASE_URL)
# Disable connection pooling for serverless environments like Vercel
engine = create_async_engine(settings.DATABASE_URL, poolclass=NullPool)

async_session_maker = async_sessionmaker(
engine, expire_on_commit=settings.EXPIRE_ON_COMMIT
Expand Down
15 changes: 5 additions & 10 deletions fastapi_backend/app/main.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
from fastapi import FastAPI

from .schemas import UserCreate, UserRead, UserUpdate
from .users import auth_backend, fastapi_users, AUTH_URL_PATH
from fastapi.middleware.cors import CORSMiddleware
from .utils import simple_generate_unique_route_id
from app.routes.items import router as items_router

from app.config import settings

app = FastAPI(generate_unique_id_function=simple_generate_unique_route_id)


origins = [
"http://localhost:3000",
"http://localhost:8080",
]

# Middleware for CORS configuration
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_origins=settings.CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


# Include authentication and user management routes
app.include_router(
fastapi_users.get_auth_router(auth_backend),
prefix=f"/{AUTH_URL_PATH}/jwt",
Expand All @@ -50,4 +44,5 @@
tags=["users"],
)

# Include items routes
app.include_router(items_router, prefix="/items")
2 changes: 1 addition & 1 deletion fastapi_backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fastapi_backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ packages = [{include = "commands"}]
python = ">=3.12,<3.13"
fastapi = {extras = ["standard"], version = "^0.115.0"}
asyncpg = "^0.29.0"
alembic = "^1.13.3"
fastapi-users = {extras = ["sqlalchemy"], version = "^13.0.0"}
pydantic-settings = "^2.5.2"
fastapi-mail = "^1.4.1"
Expand All @@ -26,6 +25,7 @@ pytest = "^8.3.3"
pytest-mock = "^3.14.0"
mypy = "^1.13.0"
coveralls = "^4.0.1"
alembic = "^1.14.0"

[build-system]
requires = ["poetry-core"]
Expand Down
59 changes: 59 additions & 0 deletions fastapi_backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
aiosmtplib==2.0.2 ; python_version >= "3.12" and python_version < "3.13"
annotated-types==0.7.0 ; python_version >= "3.12" and python_version < "3.13"
anyio==4.6.2.post1 ; python_version >= "3.12" and python_version < "3.13"
argon2-cffi-bindings==21.2.0 ; python_version >= "3.12" and python_version < "3.13"
argon2-cffi==23.1.0 ; python_version >= "3.12" and python_version < "3.13"
asyncpg==0.29.0 ; python_version >= "3.12" and python_version < "3.13"
bcrypt==4.1.2 ; python_version >= "3.12" and python_version < "3.13"
blinker==1.9.0 ; python_version >= "3.12" and python_version < "3.13"
certifi==2024.8.30 ; python_version >= "3.12" and python_version < "3.13"
cffi==1.17.1 ; python_version >= "3.12" and python_version < "3.13"
click==8.1.7 ; python_version >= "3.12" and python_version < "3.13"
colorama==0.4.6 ; python_version >= "3.12" and python_version < "3.13" and (sys_platform == "win32" or platform_system == "Windows")
cryptography==43.0.3 ; python_version >= "3.12" and python_version < "3.13"
dnspython==2.7.0 ; python_version >= "3.12" and python_version < "3.13"
email-validator==2.1.2 ; python_version >= "3.12" and python_version < "3.13"
fastapi-cli[standard]==0.0.5 ; python_version >= "3.12" and python_version < "3.13"
fastapi-mail==1.4.1 ; python_version >= "3.12" and python_version < "3.13"
fastapi-users-db-sqlalchemy==6.0.1 ; python_version >= "3.12" and python_version < "3.13"
fastapi-users==13.0.0 ; python_version >= "3.12" and python_version < "3.13"
fastapi-users[sqlalchemy]==13.0.0 ; python_version >= "3.12" and python_version < "3.13"
fastapi==0.115.6 ; python_version >= "3.12" and python_version < "3.13"
fastapi[standard]==0.115.6 ; python_version >= "3.12" and python_version < "3.13"
greenlet==3.1.1 ; python_version >= "3.12" and python_version < "3.13"
h11==0.14.0 ; python_version >= "3.12" and python_version < "3.13"
httpcore==1.0.6 ; python_version >= "3.12" and python_version < "3.13"
httptools==0.6.4 ; python_version >= "3.12" and python_version < "3.13"
httpx==0.27.2 ; python_version >= "3.12" and python_version < "3.13"
idna==3.10 ; python_version >= "3.12" and python_version < "3.13"
iniconfig==2.0.0 ; python_version >= "3.12" and python_version < "3.13"
jinja2==3.1.4 ; python_version >= "3.12" and python_version < "3.13"
makefun==1.15.6 ; python_version >= "3.12" and python_version < "3.13"
markdown-it-py==3.0.0 ; python_version >= "3.12" and python_version < "3.13"
markupsafe==3.0.2 ; python_version >= "3.12" and python_version < "3.13"
mdurl==0.1.2 ; python_version >= "3.12" and python_version < "3.13"
packaging==24.1 ; python_version >= "3.12" and python_version < "3.13"
pluggy==1.5.0 ; python_version >= "3.12" and python_version < "3.13"
pwdlib[argon2,bcrypt]==0.2.0 ; python_version >= "3.12" and python_version < "3.13"
pycparser==2.22 ; python_version >= "3.12" and python_version < "3.13"
pydantic-core==2.23.4 ; python_version >= "3.12" and python_version < "3.13"
pydantic-settings==2.6.1 ; python_version >= "3.12" and python_version < "3.13"
pydantic==2.9.2 ; python_version >= "3.12" and python_version < "3.13"
pygments==2.18.0 ; python_version >= "3.12" and python_version < "3.13"
pyjwt[crypto]==2.8.0 ; python_version >= "3.12" and python_version < "3.13"
pytest-asyncio==0.24.0 ; python_version >= "3.12" and python_version < "3.13"
pytest==8.3.3 ; python_version >= "3.12" and python_version < "3.13"
python-dotenv==1.0.1 ; python_version >= "3.12" and python_version < "3.13"
python-multipart==0.0.9 ; python_version >= "3.12" and python_version < "3.13"
pyyaml==6.0.2 ; python_version >= "3.12" and python_version < "3.13"
rich==13.9.4 ; python_version >= "3.12" and python_version < "3.13"
shellingham==1.5.4 ; python_version >= "3.12" and python_version < "3.13"
sniffio==1.3.1 ; python_version >= "3.12" and python_version < "3.13"
sqlalchemy[asyncio]==2.0.36 ; python_version >= "3.12" and python_version < "3.13"
starlette==0.41.3 ; python_version >= "3.12" and python_version < "3.13"
typer==0.12.5 ; python_version >= "3.12" and python_version < "3.13"
typing-extensions==4.12.2 ; python_version >= "3.12" and python_version < "3.13"
uvicorn[standard]==0.32.0 ; python_version >= "3.12" and python_version < "3.13"
uvloop==0.21.0 ; (sys_platform != "win32" and sys_platform != "cygwin") and platform_python_implementation != "PyPy" and python_version >= "3.12" and python_version < "3.13"
watchfiles==0.24.0 ; python_version >= "3.12" and python_version < "3.13"
websockets==13.1 ; python_version >= "3.12" and python_version < "3.13"
8 changes: 8 additions & 0 deletions fastapi_backend/vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"routes": [
{
"src": "/(.*)",
"dest": "api/index.py"
}
]
}

0 comments on commit 717e0ec

Please sign in to comment.