respect API_HOST & API_PORT env vars for dev

Using Vite's proxy option allows us to avoid CORS issues when the host
for Uvicorn doesn't match the host for Vite, e.g. localhost vs. 127.0.0.1.
This commit is contained in:
ducklet 2023-11-26 19:43:56 +01:00
parent 1f42538481
commit c63bee072f
4 changed files with 34 additions and 6 deletions

View file

@ -2,6 +2,12 @@
cd "$RUN_DIR" cd "$RUN_DIR"
# Make Uvicorn defaults explicit.
: "${API_PORT:=8000}"
: "${API_HOST:=127.0.0.1}"
export API_PORT
export API_HOST
[ -z "${DEBUG:-}" ] || set -x [ -z "${DEBUG:-}" ] || set -x
exec honcho start exec honcho start

View file

@ -4,4 +4,9 @@ cd "$RUN_DIR"
[ -z "${DEBUG:-}" ] || set -x [ -z "${DEBUG:-}" ] || set -x
exec uvicorn unwind:create_app --factory --reload exec uvicorn \
--host "$API_HOST" \
--port "$API_PORT" \
--reload \
--factory \
unwind:create_app

View file

@ -11,4 +11,5 @@ export UNWIND_PORT
exec uvicorn \ exec uvicorn \
--host 0.0.0.0 \ --host 0.0.0.0 \
--port "$UNWIND_PORT" \ --port "$UNWIND_PORT" \
--factory unwind:create_app --factory \
unwind:create_app

View file

@ -1,13 +1,29 @@
import { defineConfig } from "vite" import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue" import vue from "@vitejs/plugin-vue"
// Vite defaults.
const vite_host = "localhost"
const vite_port = 3000
const base = process.env.BASE_URL || "/"
const proxied_api_url = `http://${vite_host}:${vite_port}/api/`
const real_api_url = `http://${process.env.API_HOST}:${process.env.API_PORT}/api/`
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
base: process.env.BASE_URL || "/", base,
define: { define: {
"process.env.API_URL": JSON.stringify( "process.env.API_URL": JSON.stringify(process.env.API_URL || proxied_api_url),
process.env.API_URL || "http://localhost:8000/api/", },
), server: {
host: vite_host,
port: vite_port,
proxy: {
[`${base}api`]: {
target: real_api_url,
prependPath: false,
},
},
}, },
plugins: [vue()], plugins: [vue()],
}) })