mirror of
https://github.com/kou029w/quot.git
synced 2025-02-07 09:38:37 +00:00
reverse proxy
This commit is contained in:
parent
e50e65c839
commit
34d4d46372
5 changed files with 17 additions and 32 deletions
5
app/src/env.d.ts
vendored
5
app/src/env.d.ts
vendored
|
@ -1,5 +0,0 @@
|
||||||
interface ImportMeta {
|
|
||||||
env: {
|
|
||||||
QUOT_API_URL: string;
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -4,9 +4,7 @@ import Cards from "../components/cards";
|
||||||
import Card from "../components/card";
|
import Card from "../components/card";
|
||||||
|
|
||||||
async function fetchPages(): Promise<Pages.Response> {
|
async function fetchPages(): Promise<Pages.Response> {
|
||||||
const res = await fetch(
|
const res = await fetch("/api/pages?order=updated.desc");
|
||||||
new URL("/pages?order=updated.desc", import.meta.env.QUOT_API_URL)
|
|
||||||
);
|
|
||||||
const data = (await res.json()) as Pages.Response;
|
const data = (await res.json()) as Pages.Response;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,29 +11,21 @@ async function updatePage(
|
||||||
id: number,
|
id: number,
|
||||||
content: Pages.RequestContentPage
|
content: Pages.RequestContentPage
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
const res = await fetch(
|
const res = await fetch(`/api/pages?id=eq.${id}`, {
|
||||||
new URL(`/pages?id=eq.${id}`, import.meta.env.QUOT_API_URL),
|
method: "PUT",
|
||||||
{
|
headers: { "content-type": "application/json" },
|
||||||
method: "PUT",
|
body: JSON.stringify(content),
|
||||||
headers: { "content-type": "application/json" },
|
});
|
||||||
body: JSON.stringify(content),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return res.ok;
|
return res.ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deletePage(id: number): Promise<boolean> {
|
async function deletePage(id: number): Promise<boolean> {
|
||||||
const res = await fetch(
|
const res = await fetch(`/api/pages?id=eq.${id}`, { method: "DELETE" });
|
||||||
new URL(`/pages?id=eq.${id}`, import.meta.env.QUOT_API_URL),
|
|
||||||
{ method: "DELETE" }
|
|
||||||
);
|
|
||||||
return res.ok;
|
return res.ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchPage(id: number): Promise<Pages.ResponsePage> {
|
async function fetchPage(id: number): Promise<Pages.ResponsePage> {
|
||||||
const res = await fetch(
|
const res = await fetch(`/api/pages?id=eq.${id}`);
|
||||||
new URL(`/pages?id=eq.${id}`, import.meta.env.QUOT_API_URL)
|
|
||||||
);
|
|
||||||
const data = (await res.json()) as Pages.Response;
|
const data = (await res.json()) as Pages.Response;
|
||||||
return data[0]!;
|
return data[0]!;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,24 +19,22 @@ async function main() {
|
||||||
bundle: true,
|
bundle: true,
|
||||||
minify: true,
|
minify: true,
|
||||||
entryPoints: [scriptPath],
|
entryPoints: [scriptPath],
|
||||||
define: {
|
|
||||||
"import.meta.env.QUOT_API_URL": JSON.stringify(apiUrl),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const handler: http.RequestListener = (req, res) => {
|
const handler: http.RequestListener = (req, res) => {
|
||||||
|
const api = req.url?.startsWith("/api/") && new URL(apiUrl);
|
||||||
const options = {
|
const options = {
|
||||||
hostname: result.host,
|
hostname: /**/ api ? api.hostname /* */ : result.host,
|
||||||
port: result.port,
|
port: /* */ api ? api.port /* */ : result.port,
|
||||||
path: req.url,
|
path: /* */ api ? req.url?.slice("/api".length) /**/ : req.url,
|
||||||
method: req.method,
|
method: req.method,
|
||||||
headers: req.headers,
|
headers: req.headers,
|
||||||
};
|
};
|
||||||
|
|
||||||
const proxyReq = http.request(options, (proxyRes) => {
|
const proxyReq = http.request(options, (proxyRes) => {
|
||||||
if (proxyRes.statusCode === 200) {
|
if (api || proxyRes.statusCode === 200) {
|
||||||
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
res.writeHead(proxyRes.statusCode ?? 500, proxyRes.headers);
|
||||||
proxyRes.pipe(res);
|
proxyRes.pipe(res);
|
||||||
} else {
|
} else {
|
||||||
res.writeHead(200, { "Content-Type": "text/html" });
|
res.writeHead(200, { "Content-Type": "text/html" });
|
||||||
|
|
|
@ -8,10 +8,12 @@ services:
|
||||||
ports: ["8080:8080"]
|
ports: ["8080:8080"]
|
||||||
volumes:
|
volumes:
|
||||||
- ./app:/app
|
- ./app:/app
|
||||||
|
environment:
|
||||||
|
QUOT_API_URL: http://api:3000/
|
||||||
|
depends_on: [api]
|
||||||
api:
|
api:
|
||||||
image: postgrest/postgrest:v10.0.0
|
image: postgrest/postgrest:v10.0.0
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports: ["127.0.0.1:3000:3000"]
|
|
||||||
environment:
|
environment:
|
||||||
PGRST_DB_URI: postgresql://postgres:${POSTGRES_PASSWORD}@/postgres?host=/var/run/postgresql
|
PGRST_DB_URI: postgresql://postgres:${POSTGRES_PASSWORD}@/postgres?host=/var/run/postgresql
|
||||||
PGRST_DB_SCHEMA: public
|
PGRST_DB_SCHEMA: public
|
||||||
|
|
Loading…
Add table
Reference in a new issue