This commit is contained in:
Rizky 2024-02-10 15:11:55 +07:00
parent 8e24d40a0f
commit 1514dffd4c
6 changed files with 46 additions and 24 deletions

View File

@ -3,6 +3,7 @@ import { apiContext } from "service-srv";
import { validate } from "uuid";
import { code } from "../ws/sync/editor/code/util-code";
import { gzipAsync } from "../ws/sync/entity/zlib";
import { prodIndex } from "../util/prod-index";
export const _ = {
url: "/prod/:site_id/**",
@ -12,26 +13,9 @@ export const _ = {
const pathname: string = req.params["*"] || "";
const site_id = req.params.site_id as string;
const index_html = new Response(
`\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=1.0, minimum-scale=1.0, maximum-scale=1.0">
<title></title>
<link rel="stylesheet" href="https://prasi.app/index.css">
</head>
<body class="flex-col flex-1 w-full min-h-screen flex opacity-0">
<div id="root"></div>
<script>
window._prasi={basepath: "/prod/${site_id}",site_id:"${site_id}"}
</script>
<script src="/prod/${site_id}/main.js" type="module"></script>
</body>
</html>`,
{ headers: { "content-type": "text/html" } }
);
const index_html = new Response(prodIndex(site_id).render(), {
headers: { "content-type": "text/html" },
});
if (!validate(site_id))
return new Response("site not found", { status: 403 });

View File

@ -0,0 +1,30 @@
export const prodIndex = (site_id: string) => {
return {
head: [] as string[],
body: [] as string[],
render() {
return `\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=1.0, minimum-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" href="/index.css">
${this.head.join("\n")}
</head>
<body class="flex-col flex-1 w-full min-h-screen flex opacity-0">
${this.body.join("\n")}
<div id="root"></div>
<script>
window._prasi = { basepath: "/prod/${site_id}", site_id: "${site_id}" }
</script>
<script src="/prod/${site_id}/main.js" type="module"></script>
</body>
</html>`;
},
};
};

View File

@ -123,7 +123,9 @@ declare global {
url: URL;
req: Request;
server: Server;
mode: "dev" | "prod";
handle: (req: Request) => Promise<Response>;
index: { head: string[]; body: string[]; render: () => string };
}) => Promise<Response>;
};
}

View File

@ -24,7 +24,7 @@ export const codeBuild = async (id_site: any) => {
import type {} from "./typings/global";
export const server: PrasiServer = {
async http({ req, handle, url, server }) {
async http({ req, handle, mode, url, index, server }) {
return await handle(req);
},
};

View File

@ -6,6 +6,7 @@ import { waitUntil } from "web-utils";
import { code } from "./util-code";
import { WSData } from "../../../../../../pkgs/core/server/create";
import { codeBuild } from "./build-code";
import { prodIndex } from "../../../../util/prod-index";
const serverMain = () => ({
handler: {} as Record<string, PrasiServer>,
@ -85,7 +86,11 @@ const serverMain = () => ({
return new Response("Upgrade failed :(", { status: 500 });
}
return await handler.http(arg);
return await handler.http({
...arg,
mode: "dev",
index: prodIndex(site_id),
});
} catch (e: any) {
_fs.appendFile(
code.path(site_id, "site", "src", "server.log"),
@ -103,6 +108,8 @@ type PrasiServer = {
req: Request;
server: Server;
handle: (req: Request) => Promise<undefined | Response>;
mode: "dev" | "prod";
index: { head: string[]; body: string[]; render: () => string };
}) => Promise<Response>;
};

View File

@ -103,7 +103,7 @@ export const EdApiServer = forwardRef<
server.status = "saving";
p.render();
p.site.config.api_url = trim(local.api_url, "/");
await p.sync.site.update(p.site.id, {
await p.sync?.site.update(p.site.id, {
config: { api_url: local.api_url },
});
}
@ -254,7 +254,6 @@ export const EdApiServer = forwardRef<
)}
{local.deployable && (
<>
<EdApiDomain domains={local.domains} />
<EdApiDeploy deploy={local.deploy} />
</>
)}