@@ -17,32 +17,23 @@ export function validateModel(model: any): model is Model {
17
17
18
18
export async function getModels ( ) : Promise < Model [ ] > {
19
19
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
27
22
let baseUrlObj : URL
28
23
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 ( )
42
33
}
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 ( )
46
37
}
47
38
48
39
// Construct the models.json URL
@@ -100,3 +91,31 @@ export async function getModels(): Promise<Model[]> {
100
91
console . warn ( 'All attempts to load models failed, returning empty array' )
101
92
return [ ]
102
93
}
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