File tree Expand file tree Collapse file tree 3 files changed +27
-14
lines changed Expand file tree Collapse file tree 3 files changed +27
-14
lines changed Original file line number Diff line number Diff line change 15
15
// Use 'postCreateCommand' to run commands after the container is created.
16
16
"postCreateCommand" : " cd frontend && npm install" ,
17
17
18
+ "containerEnv" : {
19
+ "UVICORN_HOST" : " localhost" ,
20
+ "UVICORN_PORT" : " 3000" ,
21
+ "VITE_LOCAL_DOWNLOAD_SERVER" : " http://localhost:3000"
22
+ },
23
+
18
24
// Configure tool-specific properties.
19
25
"customizations" : {
20
26
"vscode" : {
Original file line number Diff line number Diff line change 1
1
import { useState } from "react"
2
2
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` ;
4
8
5
9
function App ( ) {
6
10
const [ url , setUrl ] = useState ( "" ) ;
7
11
8
12
const handleDownload : React . FormEventHandler < HTMLFormElement > = async ( e ) => {
9
13
e . preventDefault ( ) ;
10
- console . log ( "submit" )
11
14
12
15
const params = new URLSearchParams ( {
13
16
url
14
17
} ) ;
15
-
16
- const res = await fetch ( `${ DOWNLOAD_API } ?${ params } }` ) ;
18
+ const res = await fetch ( `${ download_server_url } ?${ params } }` ) ;
17
19
const blob = await res . blob ( ) ;
18
20
19
21
// pull file name out of header
Original file line number Diff line number Diff line change 1
1
from yt_dlp import YoutubeDL
2
2
from fastapi import FastAPI , BackgroundTasks
3
+ from fastapi .staticfiles import StaticFiles
3
4
from starlette .responses import FileResponse
4
5
from starlette .middleware .cors import CORSMiddleware
5
6
from tempfile import TemporaryDirectory
6
7
from copy import deepcopy
7
- from os import path
8
+ from os import path , environ
8
9
9
10
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
+ )
18
24
19
25
DEFAULT_OPTIONS = {
20
26
"format" : "best" ,
27
33
"outtmpl" : "%(title)s.%(ext)s" ,
28
34
}
29
35
30
-
31
36
@app .get ("/download" )
32
37
def download (url : str , background_tasks : BackgroundTasks ):
33
38
tempdir = TemporaryDirectory ()
You can’t perform that action at this time.
0 commit comments