This commit is contained in:
Rizky 2024-12-19 17:43:34 +07:00
parent e2a4a1a1f2
commit 683a050de8
7 changed files with 72 additions and 59 deletions

View File

@ -1,6 +0,0 @@
export const startServerWithIPC = () => {
return Bun.serve({
fetch(request, server) {},
websocket: { message(ws, message) {} },
});
};

View File

@ -1,9 +1,10 @@
import { config } from "utils/config"; import { config } from "utils/config";
import { startup } from "utils/global"; import { g, startup } from "utils/global";
startup("site", async () => { startup("site", async () => {
await config.init("site:site.json"); await config.init("site:site.json");
const ts = config.current?.deploy.current; const ts = config.current?.deploy.current;
if (ts) { if (ts) {
// g.server = process.argv.includes("--ipc") ? "ipc" : "gz";
} }
}); });

View File

@ -1,18 +1,20 @@
import { fs } from "utils/fs"; import { fs } from "utils/fs";
import { g } from "utils/global"; import { g } from "utils/global";
import { spawn } from "utils/spawn"; import { spawn } from "utils/spawn";
import { prasi_content_ipc } from "../content/content-ipc";
import { startServerWithIPC } from "./server-mode-ipc";
export const startServer = (is_dev: boolean, site_id: string) => { export const startServer = (arg: { site_id: string; mode: "dev" | "prod" }) => {
if (g.server.mode === "deploy") { if (g.mode === "supervisor") {
g.server.process = spawn({ g.supervisor = {
cmd: is_dev ? "bun run --watch server.js" : "bun run server.js", process: spawn({
cmd:
arg.mode === "dev"
? "bun run --watch server.js"
: "bun run server.js",
cwd: fs.path("site:app"), cwd: fs.path("site:app"),
mode: "passthrough", mode: "passthrough",
}); }),
};
} else { } else {
g.server.bun_server = startServerWithIPC(); import("./server");
prasi_content_ipc.prepare(site_id);
} }
}; };

View File

@ -9,6 +9,7 @@ import { ensureServerReady } from "./server/ensure";
import { startServer } from "./server/start"; import { startServer } from "./server/start";
const is_dev = process.argv.includes("--dev"); const is_dev = process.argv.includes("--dev");
const is_ipc = process.argv.includes("--ipc");
startup("supervisor", async () => { startup("supervisor", async () => {
console.log(`${c.green}Prasi Server:${c.esc} ${fs.path("site:")}`); console.log(`${c.green}Prasi Server:${c.esc} ${fs.path("site:")}`);
await config.init("site:site.json"); await config.init("site:site.json");
@ -19,13 +20,21 @@ startup("supervisor", async () => {
} else { } else {
siteLog(`Site ID: ${site_id}`); siteLog(`Site ID: ${site_id}`);
if (g.server.mode === "deploy") { if (!is_ipc) {
await prasi_content_deploy.prepare(site_id); await prasi_content_deploy.prepare(site_id);
} }
await ensureServerReady(is_dev); await ensureServerReady(is_dev);
await ensureDBReady(); await ensureDBReady();
startServer(is_dev, site_id); if (is_ipc) {
g.mode = "site";
if (g.mode === "site") g.ipc = true;
}
startServer({
site_id,
mode: is_dev ? "dev" : "prod",
});
} }
}); });

View File

@ -1,8 +1,8 @@
import { join, resolve } from "path";
import { fs } from "./fs";
import type { SiteConfig } from "./config";
import type { spawn } from "./spawn";
import type { Server } from "bun"; import type { Server } from "bun";
import { join, resolve } from "path";
import type { SiteConfig } from "./config";
import { fs } from "./fs";
import type { PrasiSpawn, spawn } from "./spawn";
if (!(globalThis as any).prasi) { if (!(globalThis as any).prasi) {
(globalThis as any).prasi = {}; (globalThis as any).prasi = {};
@ -10,11 +10,12 @@ if (!(globalThis as any).prasi) {
export const g = (globalThis as any).prasi as unknown as { export const g = (globalThis as any).prasi as unknown as {
dir: { root: string }; dir: { root: string };
mode: "supervisor" | "site"; } & (
server: | {
| { mode: "deploy"; process: ReturnType<typeof spawn> } mode: "site";
| { mode: "ipc"; bun_server: Server }; server: Server;
site?: { ipc: boolean;
site: {
db?: SiteConfig["db"]; db?: SiteConfig["db"];
layouts: { layouts: {
id: string; id: string;
@ -43,12 +44,16 @@ export const g = (globalThis as any).prasi as unknown as {
domain: string; domain: string;
}; };
}; };
}; }
| {
mode: "supervisor";
supervisor: { process: PrasiSpawn };
}
);
export const startup = (mode: "supervisor" | "site", fn: () => void) => { export const startup = (mode: "supervisor" | "site", fn: () => void) => {
g.dir = { root: "" }; g.dir = { root: "" };
g.mode = mode; g.mode = mode;
g.server.mode = process.argv.includes("--ipc") ? "ipc" : "deploy";
if (mode === "supervisor") { if (mode === "supervisor") {
const argv = process.argv.filter((e) => !e.startsWith("--")); const argv = process.argv.filter((e) => !e.startsWith("--"));

View File

@ -1,6 +1,7 @@
import { spawn as bunSpawn, type Subprocess } from "bun"; import { spawn as bunSpawn, type Subprocess } from "bun";
import { Readable } from "node:stream"; import { Readable } from "node:stream";
export type PrasiSpawn = ReturnType<typeof spawn>;
export const spawn = ( export const spawn = (
arg: { arg: {
cmd: string; cmd: string;

View File

@ -3,7 +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 internal/supervisor.ts --dev",
"ipc": "bun run --watch internal/supervisor.ts --dev --ipc"
}, },
"devDependencies": { "devDependencies": {
"@types/bun": "latest" "@types/bun": "latest"