Skip to content

Commit 83e1b35

Browse files
committed
Add Docker support with Dockerfile and .dockerignore; implement middleware for API request handling
1 parent c42a162 commit 83e1b35

File tree

6 files changed

+86
-11
lines changed

6 files changed

+86
-11
lines changed

client/.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
node_modules
3+
dist

client/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM node:lts AS runtime
2+
WORKDIR /app
3+
4+
COPY . .
5+
6+
RUN npm install
7+
RUN npm run build
8+
9+
ENV HOST=0.0.0.0
10+
ENV PORT=4321
11+
EXPOSE 4321
12+
CMD node ./dist/server/entry.mjs

client/astro.config.mjs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,15 @@ import node from '@astrojs/node';
77

88
// https://astro.build/config
99
export default defineConfig({
10+
output: 'server',
11+
integrations: [
12+
svelte(),
13+
],
1014
vite: {
11-
plugins: [tailwindcss(), svelte()],
12-
server: {
13-
proxy: {
14-
'/api': {
15-
target: 'http://localhost:5100',
16-
changeOrigin: true,
17-
}
18-
}
19-
}
15+
plugins: [tailwindcss(), svelte()]
2016
},
2117

2218
adapter: node({
2319
mode: 'standalone'
2420
}),
25-
26-
integrations: [svelte()]
2721
});

client/package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"typescript": "^5.8.2"
1818
},
1919
"devDependencies": {
20+
"@types/node": "^22.13.11",
2021
"autoprefixer": "^10.4.21",
2122
"postcss": "^8.5.3",
2223
"tailwindcss": "^4.0.14"

client/src/middleware.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { defineMiddleware } from "astro:middleware";
2+
3+
// Get server URL from environment variable with fallback for local development
4+
const API_SERVER_URL = process.env.API_SERVER_URL || 'http://localhost:5100';
5+
6+
// Middleware to handle API requests
7+
export const onRequest = defineMiddleware(async (context, next) => {
8+
console.log('Request URL:', context.request.url);
9+
10+
// Guard clause: if not an API request, pass through to regular Astro handling
11+
if (!context.request.url.includes('/api/')) {
12+
return await next();
13+
}
14+
15+
// API request handling
16+
console.log('Forwarding request to server:', API_SERVER_URL);
17+
18+
const url = new URL(context.request.url);
19+
const apiPath = url.pathname + url.search;
20+
21+
// Create a new request to the backend server
22+
const serverRequest = new Request(`${API_SERVER_URL}${apiPath}`, {
23+
method: context.request.method,
24+
headers: context.request.headers,
25+
body: context.request.method !== 'GET' && context.request.method !== 'HEAD' ?
26+
await context.request.clone().arrayBuffer() : undefined,
27+
});
28+
29+
try {
30+
// Forward the request to the API server
31+
const response = await fetch(serverRequest);
32+
const data = await response.arrayBuffer();
33+
34+
// Return the response from the API server
35+
return new Response(data, {
36+
status: response.status,
37+
statusText: response.statusText,
38+
headers: response.headers,
39+
});
40+
} catch (error) {
41+
console.error('Error forwarding request to API:', error);
42+
return new Response(JSON.stringify({ error: 'Failed to reach API server' }), {
43+
status: 502,
44+
headers: { 'Content-Type': 'application/json' }
45+
});
46+
}
47+
});

0 commit comments

Comments
 (0)