[PIE-14] More robust API proxy

This commit is contained in:
June 2025-08-13 17:28:32 +12:00
parent b8de3e82e9
commit daab7a25b6
Signed by: breadone
GPG Key ID: FDC19FE143200483
4 changed files with 18 additions and 14 deletions

View File

@ -9,5 +9,5 @@ ENV PUBLIC_URL=http://localhost:4321
RUN npm run build
EXPOSE 4321
CMD ["npm", "run", "dev", "--", "--host"]
# CMD [ "node", "dist/sever/entry.mjs"]
# CMD ["npm", "run", "dev", "--", "--host"]
CMD [ "npm", "run", "preview", "--", "--host" ]

View File

@ -19,16 +19,5 @@ export default defineConfig({
vite: {
plugins: [tailwindcss()],
server: {
proxy: {
// The idea is to proxy the Pocketbase connection to the current domain so the user doesn't have to open two ports
// Currently works in dev (npm run dev -- --host) with the correct PUBLIC_URL var set but not through docker
'/api': {
target: PUBLIC_PB_URL,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '/api'),
},
}
}
}
});

View File

@ -2,7 +2,6 @@ services:
web:
build: .
env_file: .env
network_mode: host
ports:
- "4321:4321"

View File

@ -0,0 +1,16 @@
import type { APIRoute } from "astro";
// THANK YOU https://stackoverflow.com/a/77297521 !!!!!!!
const getProxyUrl = (request: Request) => {
const proxyUrl = new URL(import.meta.env.PUBLIC_PB_URL);
const requestUrl = new URL(request.url);
return new URL(requestUrl.pathname, proxyUrl);
};
export const ALL: APIRoute = async ({ request }) => {
const proxyUrl = getProxyUrl(request);
const response = await fetch(proxyUrl.href, request);
return new Response(response.body);
};