-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from HackAtUCI/setup/api
Configure API to work on Vercel from Next SSR
- Loading branch information
Showing
11 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
fastapi==0.104.1 | ||
python-multipart==0.0.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from typing import Annotated, Union | ||
|
||
from fastapi import APIRouter, Cookie, Form | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/me") | ||
async def me(username: Annotated[Union[str, None], Cookie()] = None) -> str: | ||
"""Who are you?""" | ||
return f"You are {username or 'nobody'}" | ||
|
||
|
||
@router.post("/square") | ||
async def square(value: Annotated[int, Form()]) -> int: | ||
"""Calculate the square of the value.""" | ||
return value * value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,25 @@ | ||
// All server-side API requests are handled by lib/utils/api.ts | ||
// but this rewrite still exists for locally testing native POST requests | ||
const LOCAL_API_URL = "http://localhost:8000"; | ||
|
||
// When deployed on Vercel, this path acts like a rewrite in vercel.json | ||
// Next.js would normally be unable to find the API endpoint | ||
// but Vercel somehow steps in and makes the Serverless Function visible | ||
const VERCEL_API_PATH = "/api/"; | ||
|
||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = {} | ||
const nextConfig = { | ||
rewrites: async () => { | ||
return [ | ||
{ | ||
source: "/api/:path*", | ||
destination: | ||
process.env.NODE_ENV === "development" | ||
? `${LOCAL_API_URL}/:path*` | ||
: VERCEL_API_PATH, | ||
}, | ||
]; | ||
}, | ||
}; | ||
|
||
module.exports = nextConfig | ||
module.exports = nextConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import api from "@/lib/utils/api"; | ||
|
||
async function getMessage(): Promise<string> { | ||
const res = await api.get<string>("/demo/me"); | ||
return res.data; | ||
} | ||
|
||
async function Demo() { | ||
const message = await getMessage(); | ||
return ( | ||
<div> | ||
<p>Demo: {message}</p> | ||
<form method="post" action="/api/demo/square"> | ||
<input type="number" required name="value" style={{ color: "black" }} /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
</div> | ||
); | ||
} | ||
|
||
export default Demo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as default } from "./Demo"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import axios from "axios"; | ||
import { cookies } from "next/headers"; | ||
|
||
const LOCAL_API_URL = "http://localhost:8000"; | ||
const SERVER_HOST = process.env.NEXT_PUBLIC_VERCEL_URL; | ||
|
||
// The Vercel Serverless Function for the API lives outside the scope of Next.js | ||
// so the publicly deployed URL must be used instead of a rewrite | ||
const api = axios.create({ | ||
baseURL: SERVER_HOST ? `https://${SERVER_HOST}/api/` : LOCAL_API_URL, | ||
}); | ||
|
||
api.interceptors.request.use((config) => { | ||
const cookieStore = cookies(); | ||
|
||
// Inject user's client-side cookies along with API request | ||
const provided = config.headers.get("Cookie"); | ||
const newCookies = (provided ? `${provided}; ` : "") + cookieStore.toString(); | ||
config.headers.set("Cookie", newCookies); | ||
|
||
return config; | ||
}); | ||
|
||
export default api; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.