Skip to content

Commit 055f01b

Browse files
committed
feat: automatically set server host url in openapi endpoint
1 parent 87c4a6b commit 055f01b

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

packages/burger-api/src/functions/openapi-get.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { app, type HttpRequest, type InvocationContext } from '@azure/functions';
2+
import process from 'node:process';
23
import fs from 'node:fs/promises';
34
import path from 'node:path';
45
import yaml from 'js-yaml';
6+
import dotenv from 'dotenv';
7+
8+
// Env file is located in the root of the repository
9+
dotenv.config({ path: path.join(process.cwd(), "../../.env") });
510

611
app.http('openapi-get', {
712
methods: ['GET'],
@@ -11,17 +16,23 @@ app.http('openapi-get', {
1116
context.log('Processing request to get OpenAPI specification...');
1217

1318
try {
14-
const openapiPath = path.join(__dirname, '../../../openapi.yaml');
19+
const openapiPath = path.join(process.cwd(), 'openapi.yaml');
1520
const openapiContent = await fs.readFile(openapiPath, 'utf8');
1621

22+
// Replace BURGER_API_HOST placeholder with actual host URL
23+
console.log('BURGER_API_URL:', process.env.BURGER_API_URL);
24+
context.log('Replacing <BURGER_API_HOST> in OpenAPI specification...');
25+
const burgerApiHost = process.env.BURGER_API_URL || 'http://localhost:7071';
26+
const processedContent = openapiContent.replace('<BURGER_API_HOST>', burgerApiHost);
27+
1728
const url = new URL(request.url);
1829
const wantsJson =
1930
url.searchParams.get('format')?.toLowerCase() === 'json' ||
2031
(request.headers.get('accept')?.toLowerCase().includes('json') ?? false);
2132

2233
if (wantsJson) {
2334
try {
24-
const json = yaml.load(openapiContent);
35+
const json = yaml.load(processedContent);
2536
return {
2637
jsonBody: json,
2738
headers: {
@@ -39,19 +50,19 @@ app.http('openapi-get', {
3950
}
4051

4152
return {
42-
body: openapiContent,
53+
body: processedContent,
4354
headers: {
4455
'Content-Type': 'text/yaml'
4556
},
4657
status: 200
4758
};
4859
} catch (error) {
4960
context.error('Error reading OpenAPI specification file:', error);
50-
61+
5162
return {
5263
jsonBody: { error: 'Error reading OpenAPI specification' },
5364
status: 500
5465
};
5566
}
5667
}
57-
});
68+
});

0 commit comments

Comments
 (0)