From bf1a6bd3a2029755036796c905f5fbe374d09847 Mon Sep 17 00:00:00 2001 From: Rizky Date: Sun, 11 Feb 2024 09:35:02 +0700 Subject: [PATCH] wip fix server pathname --- app/srv/init.ts | 2 +- app/srv/ws/sync/actions/code_action.ts | 2 +- app/srv/ws/sync/editor/code/server-main.ts | 8 ++- app/web/src/base/load/api/api-proxy.tsx | 51 +++++++++++-------- .../nova/ed/panel/popup/api/api-server.tsx | 7 ++- .../src/nova/ed/panel/popup/api/api-utils.ts | 3 ++ biome.json | 5 -- 7 files changed, 48 insertions(+), 30 deletions(-) delete mode 100644 biome.json diff --git a/app/srv/init.ts b/app/srv/init.ts index de62a55d..23e4cd70 100644 --- a/app/srv/init.ts +++ b/app/srv/init.ts @@ -8,7 +8,7 @@ glb.ws_hook = { server.ws("ping", ws, data); }, pong(ws, data) { - server.ws("pong", ws, data); + server.ws("pong", ws, data); }, drain(ws) { server.ws("drain", ws); diff --git a/app/srv/ws/sync/actions/code_action.ts b/app/srv/ws/sync/actions/code_action.ts index 5c5f1bff..850274d6 100644 --- a/app/srv/ws/sync/actions/code_action.ts +++ b/app/srv/ws/sync/actions/code_action.ts @@ -120,7 +120,7 @@ declare global { type PrasiServer = { ws?: WebSocketHandler<{ url: string }>; http: (arg: { - url: URL; + url: { raw: URL; pathname: string }; req: Request; server: Server; mode: "dev" | "prod"; diff --git a/app/srv/ws/sync/editor/code/server-main.ts b/app/srv/ws/sync/editor/code/server-main.ts index f1e7ab61..3b0f64f5 100644 --- a/app/srv/ws/sync/editor/code/server-main.ts +++ b/app/srv/ws/sync/editor/code/server-main.ts @@ -86,8 +86,14 @@ const serverMain = () => ({ return new Response("Upgrade failed :(", { status: 500 }); } + const pathname = `/${arg.url.pathname.split("/").slice(3).join("/")}`; + return await handler.http({ ...arg, + url: { + pathname, + raw: arg.url, + }, mode: "dev", index: prodIndex(site_id), }); @@ -104,7 +110,7 @@ const serverMain = () => ({ type PrasiServer = { ws?: WebSocketHandler<{ url: string }>; http: (arg: { - url: URL; + url: { raw: URL; pathname: string }; req: Request; server: Server; handle: (req: Request) => Promise; diff --git a/app/web/src/base/load/api/api-proxy.tsx b/app/web/src/base/load/api/api-proxy.tsx index a43cb776..08bc2b94 100644 --- a/app/web/src/base/load/api/api-proxy.tsx +++ b/app/web/src/base/load/api/api-proxy.tsx @@ -52,34 +52,45 @@ export const apiProxy = (api_url: string) => { ); return; } - } - let actionUrl = api_ref.apiEntry[actionName].url; - const actionParams = api_ref.apiEntry[actionName].args; - if (actionUrl && actionParams) { - if (rest.length > 0 && actionParams.length > 0) { - for (const [idx, p] of Object.entries(rest)) { - const paramName = actionParams[parseInt(idx)]; - if (actionParams && actionParams.includes(paramName)) { + let actionUrl = api_ref.apiEntry[actionName].url; + const actionParams = api_ref.apiEntry[actionName].args; + if (actionUrl && actionParams) { + if (rest.length > 0 && actionParams.length > 0) { + for (const [idx, p] of Object.entries(rest)) { + const paramName = actionParams[parseInt(idx)]; if ( - !!p && - typeof p !== "string" && - typeof p !== "number" + actionParams && + actionParams.includes(paramName) ) { - continue; + if ( + !!p && + typeof p !== "string" && + typeof p !== "number" + ) { + continue; + } } + actionUrl = actionUrl.replace( + `:${paramName}?`, + p + "" + ); + actionUrl = actionUrl.replace( + `:${paramName}`, + p + "" + ); } - actionUrl = actionUrl.replace(`:${paramName}?`, p + ""); - actionUrl = actionUrl.replace(`:${paramName}`, p + ""); } + + const url = `${base_url}${actionUrl}`; + + const result = await fetchSendApi(url, rest); + resolve(result); + } else { + console.error(`API Not Found: ${actionName.toString()}`); } - - const url = `${base_url}${actionUrl}`; - - const result = await fetchSendApi(url, rest); - resolve(result); } else { - console.error(`API Not Found: ${actionName.toString()}`); + reject("Failed to load API: " + base_url); } } catch (e) { reject(e); diff --git a/app/web/src/nova/ed/panel/popup/api/api-server.tsx b/app/web/src/nova/ed/panel/popup/api/api-server.tsx index f5aeb42b..77e7358a 100644 --- a/app/web/src/nova/ed/panel/popup/api/api-server.tsx +++ b/app/web/src/nova/ed/panel/popup/api/api-server.tsx @@ -57,7 +57,8 @@ export const EdApiServer = forwardRef< try { if (dev) { const vdev = JSON.parse(localStorage.getItem("prasi-dev") || "{}"); - if (vdev) { + + if (vdev && Object.keys(vdev).length > 0) { dev.url = vdev.url; dev.enabled = vdev.enabled; } @@ -167,7 +168,9 @@ export const EdApiServer = forwardRef< local.api_url.startsWith("http://") || local.api_url.startsWith("https://") ) { - local.api_url = trim(local.api_url, "/"); + if (local.api_url.length > 8) { + local.api_url = trim(local.api_url, "/"); + } } local.render(); }} diff --git a/app/web/src/nova/ed/panel/popup/api/api-utils.ts b/app/web/src/nova/ed/panel/popup/api/api-utils.ts index 62d9eb9e..e37cb10e 100644 --- a/app/web/src/nova/ed/panel/popup/api/api-utils.ts +++ b/app/web/src/nova/ed/panel/popup/api/api-utils.ts @@ -48,12 +48,14 @@ export const apiUrl = function (p: PG): string { export const checkAPI = async (p: PG) => { const url = apiUrl(p); + if (!url) return "offline"; try { if (!apiRef[url]) apiRef[url] = apiProxy(url) as any; const capi = apiRef[url]; + if (!capi) { console.error(`Cannot initialize API for ${url}.`, w.prasiApi[url]); } else { @@ -61,6 +63,7 @@ export const checkAPI = async (p: PG) => { type: "check", id_site: p.site.id, }); + if (!res) { return { deployable: false, db: "", hasDB: false, domains: [] }; } else { diff --git a/biome.json b/biome.json deleted file mode 100644 index 4214ad93..00000000 --- a/biome.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "linter": { - "enabled": false - } -} \ No newline at end of file