Update vite to have proper routing for api

This commit is contained in:
Joey Yakimowich-Payne 2023-08-10 10:26:16 -06:00
commit 4842cef07a
No known key found for this signature in database
GPG key ID: 6BFE655FA5ABD1E1
5 changed files with 37 additions and 29 deletions

5
.env
View file

@ -1,2 +1,3 @@
VITE_API_URL="http://localhost:8000/api" VITE_HOST="127.0.0.1"
VITE_APP_URL="http://localhost:5173" VITE_API_URL="http://$VITE_HOST:8000"
VITE_APP_URL="http://$VITE_HOST:5173"

View file

@ -1,25 +1,13 @@
from dotenv import dotenv_values from dotenv import dotenv_values
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from api.db.engine import engine from api.db.engine import engine
config = dotenv_values(".env") config = dotenv_values(".env")
app = FastAPI() app = FastAPI(root_path="/api")
origins = ["http://localhost", config["VITE_APP_URL"]] @app.get("/v1/python")
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/api/python")
def hello_world(): def hello_world():
return {"message": "Hello World"} return {"message": "Hello World"}

View file

@ -34,7 +34,7 @@ const config: PlaywrightTestConfig = {
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */ /* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
actionTimeout: 0, actionTimeout: 0,
/* Base URL to use in actions like `await page.goto('/')`. */ /* 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 */ /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry', trace: 'on-first-retry',

View file

@ -10,5 +10,8 @@
{ {
"path": "./tsconfig.vitest.json" "path": "./tsconfig.vitest.json"
} }
] ],
"compilerOptions": {
"types": ["vite/client"]
}
} }

View file

@ -1,11 +1,26 @@
import { fileURLToPath, URL } from 'node:url' import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite' import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx' import vueJsx from '@vitejs/plugin-vue-jsx'
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ 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: [ plugins: [
vue(), vue(),
vueJsx(), vueJsx(),
@ -15,4 +30,5 @@ export default defineConfig({
'@': fileURLToPath(new URL('./app', import.meta.url)) '@': fileURLToPath(new URL('./app', import.meta.url))
} }
} }
}) });
}