checkpoint

This commit is contained in:
Rizky 2024-12-21 11:47:18 +07:00
parent b4d31bc897
commit 240d4e6ec1
11 changed files with 69 additions and 52 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -1,11 +1,19 @@
import { g } from "utils/global";
import { ensureDeployExists } from "./deploy/ensure";
import type { PrasiContent } from "./types";
import { config } from "utils/config";
import { loadCurrentDeploy } from "./deploy/load";
export const prasi_content_deploy: PrasiContent = {
async prepare(site_id) {
await ensureDeployExists(site_id);
},
async init() {
const ts = config.current?.deploy.current;
if (ts) {
await loadCurrentDeploy(ts);
}
},
async staticFile(ctx) {},
async route(ctx) {},
};

View File

@ -1,9 +1,33 @@
import { g } from "utils/global";
import type { PrasiContent } from "./types";
import { ipcSend } from "./ipc/send";
import { staticFile } from "utils/static";
export const prasi_content_ipc: PrasiContent = {
prepare(site_id) {
console.log("mantap jiwa");
prepare(site_id) {},
init() {
return new Promise<void>((done) => {
if (g.mode === "site" && g.ipc) {
ipcSend({ type: "init" });
if (g.server) {
console.log("restarting...");
process.exit();
} else {
process.on(
"message",
async (msg: { type: "start"; path: { asset: string } }) => {
if (g.mode === "site" && g.ipc) {
if (msg.type === "start") {
g.ipc.asset = await staticFile(msg.path.asset);
ipcSend({ type: "ready", port: g.server.port });
done();
}
}
}
);
}
}
});
},
async staticFile(ctx) {
const asset = g.mode === "site" && g.ipc?.asset!;

View File

@ -4,9 +4,14 @@ import { fs } from "utils/fs";
import { siteLog } from "utils/log";
export const downloadDeployedSite = async (site_id: string) => {
if (!site_id) {
siteLog(`No site_id defined in site/site.json`);
return 0;
}
let base_url = "https://prasi.avolut.com";
const ts = Date.now();
siteLog("Downloading site deploy: ");
siteLog(`Downloading site [${site_id}] deploy: `);
await downloadFile(
`${base_url}/prod-zip/${site_id}?ts=${ts}&msgpack=1`,
fs.path(`site:deploy/history/${ts}.gz`),

View File

@ -3,6 +3,7 @@ import { fs } from "utils/fs";
import { downloadDeployedSite } from "./download";
export const ensureDeployExists = async (site_id: string) => {
if (!site_id) return 0;
let download_deploy = false;
const ts = config.current?.deploy.current;
if (!ts) {

View File

@ -2,6 +2,7 @@ import type { ServerCtx } from "utils/server-ctx";
export type PrasiContent = {
prepare: (site_id: string) => void | Promise<void>;
init: () => Promise<void>;
staticFile: (ctx: ServerCtx) => Promise<Response | void>;
route: (ctx: ServerCtx) => Promise<Response | void>;
};

View File

@ -14,7 +14,7 @@ export const ensureServerReady = async (is_dev: boolean) => {
if (is_dev) {
const rebuild = async () => {
try {
await $`bun build --watch --target bun --entry ${fs.path(
await $`bun build --watch --no-clear-screen --target bun --entry ${fs.path(
"internal:server/server.ts"
)} --outdir ${fs.path("site:app")} --sourcemap=linked`.quiet();
} catch (e) {

View File

@ -1,45 +1,18 @@
import { config } from "utils/config";
import { g, startup } from "utils/global";
import { prasi_content_ipc } from "../content/content-ipc";
import { prasi_content_deploy } from "../content/content-deploy";
import { loadCurrentDeploy } from "../content/deploy/load";
import { ipcSend } from "../content/ipc/send";
import { staticFile } from "utils/static";
import type { ServerCtx } from "utils/server-ctx";
import { prasiContent } from "../content/content";
import { prasi_content_deploy } from "../content/content-deploy";
import { prasi_content_ipc } from "../content/content-ipc";
startup("site", async () => {
await config.init("site:site.json");
if (g.mode === "site") {
g.content = g.ipc ? prasi_content_ipc : prasi_content_deploy;
if (g.ipc) {
ipcSend({ type: "init" });
if (g.server) {
console.log("restarting...");
process.exit();
} else {
process.on(
"message",
async (msg: { type: "start"; path: { asset: string } }) => {
if (g.mode === "site" && g.ipc) {
if (msg.type === "start") {
g.ipc.asset = await staticFile(msg.path.asset);
const content = g.content;
await content.init();
startGlobalServer();
ipcSend({ type: "ready", port: g.server.port });
}
}
}
);
}
} else {
const ts = config.current?.deploy.current;
if (ts) {
await loadCurrentDeploy(ts);
}
startGlobalServer();
}
}
});

View File

@ -1,19 +1,24 @@
import { fs } from "utils/fs";
import { g } from "utils/global";
import { siteLog } from "utils/log";
import { spawn } from "utils/spawn";
export const startServer = (arg: { mode: "dev" | "prod" }) => {
if (g.mode === "supervisor") {
if (fs.exists("site:app/server.js")) {
g.supervisor = {
process: spawn({
cmd:
arg.mode === "dev"
? "bun run --watch server.js"
? "bun run --watch --no-clear-screen server.js"
: "bun run server.js",
cwd: fs.path("site:app"),
mode: "passthrough",
}),
};
} else {
siteLog("No server.js found in site/app directory");
}
} else {
import("./server");
}

View File

@ -19,18 +19,17 @@ startup("supervisor", async () => {
await prasi_content_deploy.prepare(site_id);
if (!site_id) {
siteLog("No Site Loaded");
siteLog(`Warning: site_id is empty. Please set it in site.json`);
} else {
siteLog(`Site ID: ${site_id}`);
}
await ensureDBReady();
await ensureServerReady(is_dev);
} else {
g.mode = "site";
if (g.mode === "site") g.ipc = {};
}
await ensureServerReady(is_dev);
startServer({
mode: is_dev ? "dev" : "prod",
});

View File

@ -3,8 +3,8 @@
"module": "index.ts",
"type": "module",
"scripts": {
"dev": "bun run --watch internal/supervisor.ts --dev",
"ipc": "bun run --hot internal/supervisor.ts --dev --ipc"
"dev": "bun run --watch --no-clear-screen internal/supervisor.ts --dev",
"ipc": "bun run --hot --no-clear-screen internal/supervisor.ts --dev --ipc"
},
"devDependencies": {
"@types/bun": "latest"
@ -23,6 +23,7 @@
"lodash.set": "^4.3.2",
"mime": "^4.0.6",
"msgpackr": "^1.11.2",
"rou3": "^0.5.1"
"rou3": "^0.5.1",
"uuid": "^11.0.3"
}
}