[PIE-14] More robust API proxy
This commit is contained in:
parent
b8de3e82e9
commit
daab7a25b6
@ -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" ]
|
||||
|
@ -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'),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
@ -2,7 +2,6 @@ services:
|
||||
web:
|
||||
build: .
|
||||
env_file: .env
|
||||
network_mode: host
|
||||
ports:
|
||||
- "4321:4321"
|
||||
|
||||
|
16
src/pages/api/[...proxy].ts
Normal file
16
src/pages/api/[...proxy].ts
Normal 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);
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user