Skip to content

Commit 049deca

Browse files
committed
set cors/fetch request config conditionally based on environment variables
1 parent 3fc2950 commit 049deca

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

.devcontainer/devcontainer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
// Use 'postCreateCommand' to run commands after the container is created.
1616
"postCreateCommand": "cd frontend && npm install",
1717

18+
"containerEnv": {
19+
"UVICORN_HOST": "localhost",
20+
"UVICORN_PORT": "3000",
21+
"VITE_LOCAL_DOWNLOAD_SERVER": "http://localhost:3000"
22+
},
23+
1824
// Configure tool-specific properties.
1925
"customizations": {
2026
"vscode": {

frontend/src/App.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import { useState } from "react"
22

3-
const DOWNLOAD_API = "http://localhost:3000/download";
3+
// For development, the Vite devserver is a separate process/host than the download server,
4+
// so we set this env var in the devcontainer config. Basically this var becomes:
5+
// dev -> http://localhost:3000/download // fetch on separate server
6+
// prod -> /download // fetch on same origin
7+
const download_server_url = `${import.meta.env.VITE_LOCAL_DOWNLOAD_SERVER ?? ''}/download`;
48

59
function App() {
610
const [url, setUrl] = useState("");
711

812
const handleDownload: React.FormEventHandler<HTMLFormElement> = async (e) => {
913
e.preventDefault();
10-
console.log("submit")
1114

1215
const params = new URLSearchParams({
1316
url
1417
});
15-
16-
const res = await fetch(`${DOWNLOAD_API}?${params}}`);
18+
const res = await fetch(`${download_server_url}?${params}}`);
1719
const blob = await res.blob();
1820

1921
// pull file name out of header

main.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
from yt_dlp import YoutubeDL
22
from fastapi import FastAPI, BackgroundTasks
3+
from fastapi.staticfiles import StaticFiles
34
from starlette.responses import FileResponse
45
from starlette.middleware.cors import CORSMiddleware
56
from tempfile import TemporaryDirectory
67
from copy import deepcopy
7-
from os import path
8+
from os import path, environ
89

910
app = FastAPI()
10-
app.add_middleware(
11-
CORSMiddleware,
12-
allow_origins=["*"],
13-
allow_credentials=True,
14-
allow_methods=["*"],
15-
allow_headers=["*"],
16-
expose_headers=["Content-Disposition"],
17-
)
11+
12+
# If we're in development, allow CORS to let Vite dev server communicate.
13+
# If we're in production, we don't need it as the static frontend is hosted
14+
# by this server and all requests are same-origin
15+
if environ.get("PYTHON_ENV") != "production":
16+
app.add_middleware(
17+
CORSMiddleware,
18+
allow_origins=["*"],
19+
allow_credentials=True,
20+
allow_methods=["*"],
21+
allow_headers=["*"],
22+
expose_headers=["Content-Disposition"],
23+
)
1824

1925
DEFAULT_OPTIONS = {
2026
"format": "best",
@@ -27,7 +33,6 @@
2733
"outtmpl": "%(title)s.%(ext)s",
2834
}
2935

30-
3136
@app.get("/download")
3237
def download(url: str, background_tasks: BackgroundTasks):
3338
tempdir = TemporaryDirectory()

0 commit comments

Comments
 (0)