wip fix server pathname

This commit is contained in:
Rizky 2024-02-11 09:35:02 +07:00
parent 1514dffd4c
commit bf1a6bd3a2
7 changed files with 48 additions and 30 deletions

View File

@ -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);

View File

@ -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";

View File

@ -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<undefined | Response>;

View File

@ -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);

View File

@ -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();
}}

View File

@ -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 {

View File

@ -1,5 +0,0 @@
{
"linter": {
"enabled": false
}
}