Skip to content

Commit 67e3b73

Browse files
authored
Merge pull request #472 from miurla/feature/base-url-for-docker
feat: add BASE_URL environment variable support for Docker environments
2 parents aca1410 + 9a2d232 commit 67e3b73

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

.env.local.example

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ OPENAI_API_KEY=[YOUR_OPENAI_API_KEY]
99
# Search Configuration
1010
TAVILY_API_KEY=[YOUR_TAVILY_API_KEY] # Get your API key at: https://app.tavily.com/home
1111

12+
# Optional Docker Configuration (only needed in some Docker environments)
13+
# BASE_URL=http://localhost:3000 # Use only if models.json fails to load in Docker
14+
1215
###############################################################################
1316
# Optional Features
1417
# Enable these features by uncommenting and configuring the settings below

lib/config/models.ts

+42-23
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,23 @@ export function validateModel(model: any): model is Model {
1717

1818
export async function getModels(): Promise<Model[]> {
1919
try {
20-
const headersList = await headers()
21-
const baseUrl = headersList.get('x-base-url')
22-
const url = headersList.get('x-url')
23-
const host = headersList.get('x-host')
24-
const protocol = headersList.get('x-protocol') || 'http:'
25-
26-
// Construct base URL using the headers
20+
// Check for BASE_URL environment variable first
21+
const baseUrlEnv = process.env.BASE_URL
2722
let baseUrlObj: URL
2823

29-
try {
30-
// Try to use the pre-constructed base URL if available
31-
if (baseUrl) {
32-
baseUrlObj = new URL(baseUrl)
33-
} else if (url) {
34-
baseUrlObj = new URL(url)
35-
} else if (host) {
36-
const constructedUrl = `${protocol}${
37-
protocol.endsWith(':') ? '//' : '://'
38-
}${host}`
39-
baseUrlObj = new URL(constructedUrl)
40-
} else {
41-
baseUrlObj = new URL('http://localhost:3000')
24+
if (baseUrlEnv) {
25+
try {
26+
baseUrlObj = new URL(baseUrlEnv)
27+
console.log('Using BASE_URL environment variable:', baseUrlEnv)
28+
} catch (error) {
29+
console.warn(
30+
'Invalid BASE_URL environment variable, falling back to headers'
31+
)
32+
baseUrlObj = await getBaseUrlFromHeaders()
4233
}
43-
} catch (urlError) {
44-
// Fallback to default URL if any error occurs during URL construction
45-
baseUrlObj = new URL('http://localhost:3000')
34+
} else {
35+
// If BASE_URL is not set, use headers
36+
baseUrlObj = await getBaseUrlFromHeaders()
4637
}
4738

4839
// Construct the models.json URL
@@ -100,3 +91,31 @@ export async function getModels(): Promise<Model[]> {
10091
console.warn('All attempts to load models failed, returning empty array')
10192
return []
10293
}
94+
95+
// Helper function to get base URL from headers
96+
async function getBaseUrlFromHeaders(): Promise<URL> {
97+
const headersList = await headers()
98+
const baseUrl = headersList.get('x-base-url')
99+
const url = headersList.get('x-url')
100+
const host = headersList.get('x-host')
101+
const protocol = headersList.get('x-protocol') || 'http:'
102+
103+
try {
104+
// Try to use the pre-constructed base URL if available
105+
if (baseUrl) {
106+
return new URL(baseUrl)
107+
} else if (url) {
108+
return new URL(url)
109+
} else if (host) {
110+
const constructedUrl = `${protocol}${
111+
protocol.endsWith(':') ? '//' : '://'
112+
}${host}`
113+
return new URL(constructedUrl)
114+
} else {
115+
return new URL('http://localhost:3000')
116+
}
117+
} catch (urlError) {
118+
// Fallback to default URL if any error occurs during URL construction
119+
return new URL('http://localhost:3000')
120+
}
121+
}

0 commit comments

Comments
 (0)