Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimal Python API deployable with Next/Vercel #33

Merged
merged 2 commits into from
Nov 24, 2023
Merged
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
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PYTHONPATH=apps/api/src
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ yarn-error.log*

# vercel
.vercel

# Python
.venv
12 changes: 7 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"eslint.workingDirectories": [
{
"mode": "auto"
}
]
"eslint.workingDirectories": [
{
"mode": "auto"
}
],
"python.envFile": "${workspaceFolder}/.env",
"python.defaultInterpreterPath": ".venv/bin/python",
}
1 change: 1 addition & 0 deletions apps/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
19 changes: 19 additions & 0 deletions apps/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# IrvineHacks Site API

This "app" contains the source code for the backend API used by the site.

## Local Development

For local development, run

```shell
python src/dev.py
```

which will start a Uvicorn server with auto-reload.

## Deployment Configuration

For deployment, the following environment variables need to be set:

- `PYTHONPATH=src/api` to properly import Python modules
samderanova marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 12 additions & 0 deletions apps/api/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import logging

from fastapi import FastAPI

from app import app as api

# Override AWS Lambda logging configuration
logging.basicConfig(level=logging.INFO, force=True)

# This ASGI app is used by Vercel as a Serverless Function
app = FastAPI()
app.mount("/api", api)
7 changes: 7 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "api",
"private": true,
"scripts": {
"dev": "python src/dev.py"
}
}
1 change: 1 addition & 0 deletions apps/api/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uvicorn[standard]==0.23.2
1 change: 1 addition & 0 deletions apps/api/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fastapi==0.104.1
8 changes: 8 additions & 0 deletions apps/api/src/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root() -> dict[str, str]:
return {"message": "hello"}
10 changes: 10 additions & 0 deletions apps/api/src/dev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import uvicorn

if __name__ == "__main__":
uvicorn.run(
"app:app",
log_level="info",
access_log=True,
use_colors=True,
reload=True,
)
5 changes: 5 additions & 0 deletions apps/site/api/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
This is an empty source file necessary for Vercel to recognize the existence of
the Serverless Function for the backend API as defined in `vercel.json`.
The contents will be replaced by that of `apps/api/index.py` during Turbo build.
"""
4 changes: 4 additions & 0 deletions apps/site/copy-api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copy API files from apps/api
cp -R ../api/requirements.txt .
cp -R ../api/src/ src/api/
cp ../api/index.py api/index.py
11 changes: 11 additions & 0 deletions apps/site/vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"buildCommand": "cd ../.. && turbo run build --filter={apps/site} && cd apps/site && ./copy-api.sh",
"rewrites": [{ "source": "/api/(.*)", "destination": "api/index.py" }],
"functions": {
"api/index.py": {
"memory": 512,
"maxDuration": 10,
"excludeFiles": "{*.{js,ts,json},@(.next|.turbo|node_modules|public)/**,src/{*,!(api)/**}}"
}
}
}
26 changes: 13 additions & 13 deletions turbo.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": ["**/.env.*local"],
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": [".next/**", "!.next/cache/**"]
},
"lint": {},
"dev": {
"cache": false,
"persistent": true
}
}
"$schema": "https://turbo.build/schema.json",
"globalDependencies": ["**/.env.*local"],
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": [".next/**", "!.next/cache/**"]
},
"lint": {},
"dev": {
"cache": false,
"persistent": true
}
}
}
Loading