From 4842cef07a1e1500ced80232c7999758437b1986 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Thu, 10 Aug 2023 10:26:16 -0600 Subject: [PATCH] Update vite to have proper routing for api --- .env | 5 +++-- api/index.py | 16 ++-------------- playwright.config.ts | 2 +- tsconfig.json | 5 ++++- vite.config.ts | 38 +++++++++++++++++++++++++++----------- 5 files changed, 37 insertions(+), 29 deletions(-) diff --git a/.env b/.env index 47a5cc8..5699a4d 100644 --- a/.env +++ b/.env @@ -1,2 +1,3 @@ -VITE_API_URL="http://localhost:8000/api" -VITE_APP_URL="http://localhost:5173" +VITE_HOST="127.0.0.1" +VITE_API_URL="http://$VITE_HOST:8000" +VITE_APP_URL="http://$VITE_HOST:5173" diff --git a/api/index.py b/api/index.py index 9b622aa..85cd7e4 100644 --- a/api/index.py +++ b/api/index.py @@ -1,25 +1,13 @@ from dotenv import dotenv_values from fastapi import FastAPI -from fastapi.middleware.cors import CORSMiddleware from api.db.engine import engine config = dotenv_values(".env") -app = FastAPI() +app = FastAPI(root_path="/api") -origins = ["http://localhost", config["VITE_APP_URL"]] - -app.add_middleware( - CORSMiddleware, - allow_origins=origins, - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) - - -@app.get("/api/python") +@app.get("/v1/python") def hello_world(): return {"message": "Hello World"} diff --git a/playwright.config.ts b/playwright.config.ts index 333a4dc..4786406 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -34,7 +34,7 @@ const config: PlaywrightTestConfig = { /* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */ actionTimeout: 0, /* Base URL to use in actions like `await page.goto('/')`. */ - baseURL: 'http://localhost:5173', + baseURL: 'http://127.0.0.1:5173', /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ trace: 'on-first-retry', diff --git a/tsconfig.json b/tsconfig.json index 100cf6a..5fbe652 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,5 +10,8 @@ { "path": "./tsconfig.vitest.json" } - ] + ], + "compilerOptions": { + "types": ["vite/client"] + } } diff --git a/vite.config.ts b/vite.config.ts index abf9023..07298fe 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,18 +1,34 @@ import { fileURLToPath, URL } from 'node:url' -import { defineConfig } from 'vite' +import { defineConfig, loadEnv } from 'vite' import vue from '@vitejs/plugin-vue' import vueJsx from '@vitejs/plugin-vue-jsx' // https://vitejs.dev/config/ -export default defineConfig({ - plugins: [ - vue(), - vueJsx(), - ], - resolve: { - alias: { - '@': fileURLToPath(new URL('./app', import.meta.url)) +export default ({ mode }: { mode: string }) => { + process.env = {...process.env, ...loadEnv(mode, process.cwd())}; + + return defineConfig({ + server: { + host: process.env.VITE_HOST, + proxy: { + '/api': { + target: process.env.VITE_API_URL, + changeOrigin: true, + rewrite: (path) => { + return path.replace(/^\/api/, ''); + }, + } + } + }, + plugins: [ + vue(), + vueJsx(), + ], + resolve: { + alias: { + '@': fileURLToPath(new URL('./app', import.meta.url)) + } } - } -}) + }); +}