env var name change requires deployment configuration change

This commit is contained in:
2025-06-03 16:27:42 -07:00
parent 740e3c8932
commit 954dbaeac6
2 changed files with 9 additions and 9 deletions

View File

@@ -26,10 +26,10 @@ Prerequisite: golang installed
Prerequisite: bun installed
The environment variables `BASE_URL` and `AUTH_HOST` will need to be set for the middleware.
The environment variables `API_HOST` and `AUTH_HOST` will need to be set for the middleware.
Example `.env` in web's root:
```
BASE_URL="http://localhost:8082/"
API_HOST="http://localhost:8082/"
AUTH_HOST="http://localhost:8083/"
```

View File

@@ -8,11 +8,11 @@ export function middleware(request: NextRequest) {
const { pathname, search } = request.nextUrl
if (pathname.startsWith("/api")) {
if (!process.env.BASE_URL) {
throw new Error('env variable "BASE_URL" is not set')
if (!process.env.API_HOST) {
throw new Error('env variable "API_HOST" is not set')
}
const baseUrl = process.env.BASE_URL.replace(/\/$/, "");
const baseUrl = process.env.API_HOST.replace(/\/$/, "");
const apiUrl = new URL(baseUrl + pathname + search);
return NextResponse.rewrite(apiUrl, { request });
@@ -20,13 +20,13 @@ export function middleware(request: NextRequest) {
if (!process.env.AUTH_HOST) {
throw new Error('env variable "AUTH_HOST" is not set')
}
const authHost = process.env.AUTH_HOST.replace(/\/$/, "");
const path = pathname.replace(/^\/auth/, "");
const redirectUrl = new URL(authHost + path + search);
return NextResponse.redirect(redirectUrl, 302);
}
}
return NextResponse.next()
}
}