const fs = require("fs");
const path = require("path");
const html = `
WriteBot
WriteBot
Ctrl+D bookmark this page
Loading...
`;
const fileContent = `// admin-panel.js - Minimal web admin panel + HTTP control API
const http = require("http");
const config = require("./config");
const { getLanIP } = require("./helpers");
const { startWriteServer, stopWriteServer, getWriteStatus } = require("./write-server");
const ADMIN_HTML = \`${html}\`.replace("var port=8016", "var port=\${config.WRITE_PORT}");
function createControlServer() {
const server = http.createServer((req, res) => {
const url = new URL(req.url, "http://127.0.0.1:" + config.CONTROL_PORT);
const path = url.pathname;
res.setHeader("Access-Control-Allow-Origin", "*");
if (path === "/" && req.method === "GET") {
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end(ADMIN_HTML);
} else if (path === "/status" && req.method === "GET") {
res.setHeader("Content-Type", "application/json");
const s = getWriteStatus();
s.ip = getLanIP();
s.port = config.WRITE_PORT;
res.end(JSON.stringify(s));
} else if (path === "/start" && req.method === "POST") {
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify(startWriteServer()));
} else if (path === "/stop" && req.method === "POST") {
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ ok: stopWriteServer() }));
} else if (path === "/ip" && req.method === "GET") {
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ ip: getLanIP(), port: config.WRITE_PORT }));
} else if (path === "/health" && req.method === "GET") {
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ ok: true }));
} else {
res.statusCode = 404;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ error: "not found" }));
}
});
server.listen(config.CONTROL_PORT, "127.0.0.1", () => {
console.log("[control] http://127.0.0.1:" + config.CONTROL_PORT);
});
return server;
}
module.exports = { createControlServer };
`;
fs.writeFileSync(path.join(__dirname, "src", "admin-panel.js"), fileContent, "utf8");
console.log("admin-panel.js written (" + fileContent.length + " bytes)");