diff --git a/README.md b/README.md index c21493d..dcb6509 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,9 @@ pip install -r requirements.txt # Start the backend server python main.py + +# Or run on a different port +PORT=8001 python main.py ``` ```bash @@ -118,6 +121,9 @@ python main.py cd examples/fastapi-example/frontend npm install npm run dev + +# If the backend is on a different port, set BACKEND_PORT +BACKEND_PORT=8001 npm run dev ``` Then open http://localhost:5173 to see the demo. diff --git a/examples/fastapi-example/frontend/package-lock.json b/examples/fastapi-example/frontend/package-lock.json index 45a9864..cc82983 100644 --- a/examples/fastapi-example/frontend/package-lock.json +++ b/examples/fastapi-example/frontend/package-lock.json @@ -12,6 +12,7 @@ "react-dom": "^18.2.0" }, "devDependencies": { + "@types/node": "^20.0.0", "@types/react": "^18.2.0", "@types/react-dom": "^18.2.0", "@vitejs/plugin-react": "^4.0.0", @@ -1109,6 +1110,16 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/node": { + "version": "20.19.27", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.27.tgz", + "integrity": "sha512-N2clP5pJhB2YnZJ3PIHFk5RkygRX5WO/5f0WC08tp0wd+sv0rsJk3MqWn3CbNmT2J505a5336jaQj4ph1AdMug==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~6.21.0" + } + }, "node_modules/@types/prop-types": { "version": "15.7.15", "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.15.tgz", @@ -1579,6 +1590,13 @@ "node": ">=14.17" } }, + "node_modules/undici-types": { + "version": "6.21.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz", + "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==", + "dev": true, + "license": "MIT" + }, "node_modules/update-browserslist-db": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.2.3.tgz", diff --git a/examples/fastapi-example/frontend/package.json b/examples/fastapi-example/frontend/package.json index 2f67832..bc30bf8 100644 --- a/examples/fastapi-example/frontend/package.json +++ b/examples/fastapi-example/frontend/package.json @@ -13,6 +13,7 @@ "react-dom": "^18.2.0" }, "devDependencies": { + "@types/node": "^20.0.0", "@types/react": "^18.2.0", "@types/react-dom": "^18.2.0", "@vitejs/plugin-react": "^4.0.0", diff --git a/examples/fastapi-example/frontend/vite.config.ts b/examples/fastapi-example/frontend/vite.config.ts index fa9a78e..4c2f6ca 100644 --- a/examples/fastapi-example/frontend/vite.config.ts +++ b/examples/fastapi-example/frontend/vite.config.ts @@ -1,24 +1,32 @@ -import { defineConfig } from "vite"; +import { defineConfig, loadEnv } from "vite"; import react from "@vitejs/plugin-react"; import path from "path"; +import { fileURLToPath } from "url"; -export default defineConfig({ - plugins: [react()], - resolve: { - alias: { - "@llmpane/react": path.resolve(__dirname, "../../../packages/llmpane-react/src"), +const __dirname = path.dirname(fileURLToPath(import.meta.url)); + +export default defineConfig(({ mode }) => { + const env = loadEnv(mode, process.cwd(), ""); + const backendPort = env.BACKEND_PORT || "8000"; + + return { + plugins: [react()], + resolve: { + alias: { + "@llmpane/react": path.resolve(__dirname, "../../../packages/llmpane-react/src"), + }, }, - }, - server: { - proxy: { - "/api": { - target: "http://localhost:8000", - changeOrigin: true, + server: { + proxy: { + "/api": { + target: `http://localhost:${backendPort}`, + changeOrigin: true, + }, }, }, - }, - build: { - outDir: "../static", - emptyOutDir: true, - }, + build: { + outDir: "../static", + emptyOutDir: true, + }, + }; }); diff --git a/examples/fastapi-example/main.py b/examples/fastapi-example/main.py index d0eb097..b1dcfb9 100644 --- a/examples/fastapi-example/main.py +++ b/examples/fastapi-example/main.py @@ -2,6 +2,7 @@ import asyncio import json +import os import random import uuid from collections.abc import AsyncGenerator @@ -257,4 +258,5 @@ async def serve_index(): if __name__ == "__main__": import uvicorn - uvicorn.run(app, host="0.0.0.0", port=8000) + port = int(os.environ.get("PORT", 8000)) + uvicorn.run(app, host="0.0.0.0", port=port)