Initial commit: KHY Admin project
This commit is contained in:
commit
d3403cb5e2
65 changed files with 12739 additions and 0 deletions
167
start-server.js
Normal file
167
start-server.js
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Server Starter for KHY Admin Dashboard
|
||||
*
|
||||
* This script starts a local server and opens the preview pages
|
||||
* Usage: node start-server.js
|
||||
*/
|
||||
|
||||
const { spawn } = require("child_process");
|
||||
const { exec } = require("child_process");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
|
||||
const PORT = 8080;
|
||||
const BASE_URL = `http://localhost:${PORT}`;
|
||||
const PID_FILE = path.join(__dirname, ".server.pid");
|
||||
|
||||
console.log("Starting KHY Admin Preview Server");
|
||||
console.log("===================================");
|
||||
console.log("");
|
||||
|
||||
// Function to open URL in default browser
|
||||
function openBrowser(url) {
|
||||
const start =
|
||||
process.platform === "darwin"
|
||||
? "open"
|
||||
: process.platform === "win32"
|
||||
? "start"
|
||||
: "xdg-open";
|
||||
|
||||
exec(`${start} ${url}`, (error) => {
|
||||
if (error) {
|
||||
console.log(`Please manually open: ${url}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Function to save server PID
|
||||
function saveServerPID(pid) {
|
||||
try {
|
||||
fs.writeFileSync(PID_FILE, pid.toString());
|
||||
console.log(`Server PID saved: ${pid}`);
|
||||
} catch (error) {
|
||||
console.log(`Could not save PID: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Function to kill server by PID
|
||||
function killServerByPID() {
|
||||
try {
|
||||
if (fs.existsSync(PID_FILE)) {
|
||||
const pid = fs.readFileSync(PID_FILE, "utf8").trim();
|
||||
console.log(`Killing server with PID: ${pid}`);
|
||||
|
||||
// Kill the process
|
||||
process.kill(parseInt(pid), "SIGTERM");
|
||||
|
||||
// Remove PID file
|
||||
fs.unlinkSync(PID_FILE);
|
||||
console.log(`Server killed successfully`);
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`Could not kill server: ${error.message}`);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Function to start the server
|
||||
function startServer() {
|
||||
console.log(`Starting server on port ${PORT}...`);
|
||||
|
||||
// Try Python first, then Node.js http-server
|
||||
const pythonServer = spawn(
|
||||
"python3",
|
||||
["-m", "http.server", PORT.toString()],
|
||||
{
|
||||
stdio: "pipe",
|
||||
cwd: __dirname,
|
||||
}
|
||||
);
|
||||
|
||||
// Save the server PID
|
||||
saveServerPID(pythonServer.pid);
|
||||
|
||||
pythonServer.stdout.on("data", (data) => {
|
||||
console.log(`Server output: ${data}`);
|
||||
});
|
||||
|
||||
pythonServer.stderr.on("data", (data) => {
|
||||
console.log(`Server error: ${data}`);
|
||||
});
|
||||
|
||||
pythonServer.on("close", (code) => {
|
||||
console.log(`Server stopped with code ${code}`);
|
||||
// Clean up PID file when server stops
|
||||
if (fs.existsSync(PID_FILE)) {
|
||||
fs.unlinkSync(PID_FILE);
|
||||
}
|
||||
});
|
||||
|
||||
// Wait a moment for server to start
|
||||
setTimeout(() => {
|
||||
console.log("");
|
||||
console.log("Server started successfully!");
|
||||
console.log("");
|
||||
console.log("Preview URLs:");
|
||||
console.log(` Admin Dashboard: ${BASE_URL}/admin.html`);
|
||||
console.log(` Homepage: ${BASE_URL}/index.html`);
|
||||
console.log(` Product Catalog: ${BASE_URL}/product-catalog.html`);
|
||||
console.log(` Product Details: ${BASE_URL}/product-detail.html`);
|
||||
console.log("");
|
||||
console.log("Opening preview pages...");
|
||||
|
||||
// Open preview pages
|
||||
setTimeout(() => openBrowser(`${BASE_URL}/admin.html`), 1000);
|
||||
setTimeout(() => openBrowser(`${BASE_URL}/index.html`), 2000);
|
||||
setTimeout(() => openBrowser(`${BASE_URL}/product-catalog.html`), 3000);
|
||||
|
||||
console.log("");
|
||||
console.log("Preview environment ready!");
|
||||
console.log("Press Ctrl+C to stop the server");
|
||||
console.log("");
|
||||
}, 2000);
|
||||
|
||||
// Handle Ctrl+C
|
||||
process.on("SIGINT", () => {
|
||||
console.log("");
|
||||
console.log("Stopping server...");
|
||||
pythonServer.kill();
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
// Check if port is already in use
|
||||
const net = require("net");
|
||||
const server = net.createServer();
|
||||
|
||||
server.listen(PORT, () => {
|
||||
server.close(() => {
|
||||
startServer();
|
||||
});
|
||||
});
|
||||
|
||||
server.on("error", (err) => {
|
||||
if (err.code === "EADDRINUSE") {
|
||||
console.log(`Port ${PORT} is already in use`);
|
||||
console.log("The server might already be running");
|
||||
console.log("");
|
||||
console.log("If server is running, you can access:");
|
||||
console.log(` Admin Dashboard: ${BASE_URL}/admin.html`);
|
||||
console.log(` Homepage: ${BASE_URL}/index.html`);
|
||||
console.log(` Product Catalog: ${BASE_URL}/product-catalog.html`);
|
||||
console.log(` Product Details: ${BASE_URL}/product-detail.html`);
|
||||
console.log("");
|
||||
console.log("Opening preview pages...");
|
||||
|
||||
// Open preview pages anyway
|
||||
setTimeout(() => openBrowser(`${BASE_URL}/admin.html`), 1000);
|
||||
setTimeout(() => openBrowser(`${BASE_URL}/index.html`), 2000);
|
||||
setTimeout(() => openBrowser(`${BASE_URL}/product-catalog.html`), 3000);
|
||||
} else {
|
||||
console.error("Error starting server:", err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue