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 { g } from "utils/global";
import { ensureDeployExists } from "./deploy/ensure"; import { ensureDeployExists } from "./deploy/ensure";
import type { PrasiContent } from "./types"; import type { PrasiContent } from "./types";
import { config } from "utils/config";
import { loadCurrentDeploy } from "./deploy/load";
export const prasi_content_deploy: PrasiContent = { export const prasi_content_deploy: PrasiContent = {
async prepare(site_id) { async prepare(site_id) {
await ensureDeployExists(site_id); await ensureDeployExists(site_id);
}, },
async init() {
const ts = config.current?.deploy.current;
if (ts) {
await loadCurrentDeploy(ts);
}
},
async staticFile(ctx) {}, async staticFile(ctx) {},
async route(ctx) {}, async route(ctx) {},
}; };

View File

@ -1,9 +1,33 @@
import { g } from "utils/global"; import { g } from "utils/global";
import type { PrasiContent } from "./types"; import type { PrasiContent } from "./types";
import { ipcSend } from "./ipc/send";
import { staticFile } from "utils/static";
export const prasi_content_ipc: PrasiContent = { export const prasi_content_ipc: PrasiContent = {
prepare(site_id) { prepare(site_id) {},
console.log("mantap jiwa"); 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) { async staticFile(ctx) {
const asset = g.mode === "site" && g.ipc?.asset!; const asset = g.mode === "site" && g.ipc?.asset!;

View File

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

View File

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

View File

@ -2,6 +2,7 @@ import type { ServerCtx } from "utils/server-ctx";
export type PrasiContent = { export type PrasiContent = {
prepare: (site_id: string) => void | Promise<void>; prepare: (site_id: string) => void | Promise<void>;
init: () => Promise<void>;
staticFile: (ctx: ServerCtx) => Promise<Response | void>; staticFile: (ctx: ServerCtx) => Promise<Response | void>;
route: (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) { if (is_dev) {
const rebuild = async () => { const rebuild = async () => {
try { 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" "internal:server/server.ts"
)} --outdir ${fs.path("site:app")} --sourcemap=linked`.quiet(); )} --outdir ${fs.path("site:app")} --sourcemap=linked`.quiet();
} catch (e) { } catch (e) {

View File

@ -1,45 +1,18 @@
import { config } from "utils/config"; import { config } from "utils/config";
import { g, startup } from "utils/global"; 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 type { ServerCtx } from "utils/server-ctx";
import { prasiContent } from "../content/content"; import { prasiContent } from "../content/content";
import { prasi_content_deploy } from "../content/content-deploy";
import { prasi_content_ipc } from "../content/content-ipc";
startup("site", async () => { startup("site", async () => {
await config.init("site:site.json"); await config.init("site:site.json");
if (g.mode === "site") { if (g.mode === "site") {
g.content = g.ipc ? prasi_content_ipc : prasi_content_deploy; g.content = g.ipc ? prasi_content_ipc : prasi_content_deploy;
if (g.ipc) { const content = g.content;
ipcSend({ type: "init" }); await content.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);
startGlobalServer(); 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 { fs } from "utils/fs";
import { g } from "utils/global"; import { g } from "utils/global";
import { siteLog } from "utils/log";
import { spawn } from "utils/spawn"; import { spawn } from "utils/spawn";
export const startServer = (arg: { mode: "dev" | "prod" }) => { export const startServer = (arg: { mode: "dev" | "prod" }) => {
if (g.mode === "supervisor") { if (g.mode === "supervisor") {
if (fs.exists("site:app/server.js")) {
g.supervisor = { g.supervisor = {
process: spawn({ process: spawn({
cmd: cmd:
arg.mode === "dev" arg.mode === "dev"
? "bun run --watch server.js" ? "bun run --watch --no-clear-screen server.js"
: "bun run server.js", : "bun run server.js",
cwd: fs.path("site:app"), cwd: fs.path("site:app"),
mode: "passthrough", mode: "passthrough",
}), }),
}; };
} else {
siteLog("No server.js found in site/app directory");
}
} else { } else {
import("./server"); import("./server");
} }

View File

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

View File

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