169 lines
6.2 KiB
JavaScript
Executable File
169 lines
6.2 KiB
JavaScript
Executable File
// @ts-check
|
|
|
|
const http = require("http");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const { exec, execSync, execFile, spawn, spawnSync } = require("child_process");
|
|
const { createHmac } = require("crypto");
|
|
|
|
require("dotenv").config({
|
|
path: path.resolve(__dirname, ".env"),
|
|
});
|
|
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
|
|
/**
|
|
* ==============================================================================
|
|
* Initialization
|
|
* ==============================================================================
|
|
*/
|
|
/** ********************* Create HTTP Server */
|
|
const server = http.createServer();
|
|
|
|
/** ********************* Load environment variables */
|
|
const key = process.env.DSQL_GITHUB_SECRET;
|
|
|
|
/** ********************* Set environment */
|
|
let environment = process.env.NODE_ENVIRONMENT;
|
|
|
|
/** ********************* Set global variables */
|
|
global.NODE_ENVIRONMENT = environment;
|
|
|
|
/** ********************* Set PORT */
|
|
const PORT = process.env.DSQL_PORT;
|
|
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
|
|
const workingDirectory = path.resolve(__dirname, "../");
|
|
|
|
console.log(workingDirectory);
|
|
|
|
let child = spawn("node", ["./server.js"], {
|
|
cwd: workingDirectory,
|
|
stdio: "inherit",
|
|
env: environment?.match(/prod/i) ? { NODE_ENV: "production" } : undefined,
|
|
});
|
|
|
|
child.on("exit", () => {
|
|
console.log("Child Process Exited");
|
|
});
|
|
|
|
/**
|
|
* Handle Server Requests
|
|
* ==============================================================================
|
|
*/
|
|
server.on("request", (req, res) => {
|
|
/** ********************* Handle response Headers */
|
|
if (!req.method?.match(/post/i)) {
|
|
return res.writeHead(401, "Unauthorized").end();
|
|
}
|
|
|
|
/** ********************* Initialize data */
|
|
let data = "";
|
|
|
|
try {
|
|
/** ********************* Handle Request Methods */
|
|
/** # Handle POST Requests
|
|
* =================================================
|
|
*/
|
|
req.on("data", (chunk) => {
|
|
data += chunk;
|
|
});
|
|
|
|
/** ********************************************** */
|
|
/** ********************************************** */
|
|
/** ********************************************** */
|
|
|
|
req.on("end", () => {
|
|
console.log("Request Recieved");
|
|
|
|
let parsedData = (() => {
|
|
try {
|
|
return JSON.parse(data.toString());
|
|
} catch (error) {
|
|
console.log(error.message);
|
|
return data.toString();
|
|
}
|
|
})();
|
|
|
|
console.log("Request Received", parsedData);
|
|
|
|
if (req.url == process.env.ENDPOINT) {
|
|
/** @type {import("child_process").SpawnSyncOptionsWithBufferEncoding} */
|
|
const options = {
|
|
cwd: workingDirectory,
|
|
stdio: "inherit",
|
|
};
|
|
|
|
if (process.platform?.match(/win/i)) {
|
|
options.shell = "bash.exe";
|
|
}
|
|
|
|
if (environment?.match(/prod/i)) {
|
|
spawnSync("git", ["checkout", "."], options);
|
|
spawnSync("git", ["pull"], options);
|
|
spawnSync("npm", ["install"], options);
|
|
spawnSync("npm", ["run", "build"], options);
|
|
|
|
child.kill();
|
|
|
|
child = spawn("node", ["./server.js"], {
|
|
cwd: workingDirectory,
|
|
stdio: "inherit",
|
|
env: { NODE_ENV: "production" },
|
|
});
|
|
} else {
|
|
spawnSync("git", ["status"], options);
|
|
spawnSync("npm", ["list", "next"], options);
|
|
|
|
console.log("Not in production, Continuing ...");
|
|
}
|
|
|
|
res.statusCode = 200;
|
|
res.end("Success");
|
|
} else {
|
|
res.statusCode = 400;
|
|
res.end("Failed");
|
|
}
|
|
});
|
|
|
|
/** ********************************************** */
|
|
/** ********************************************** */
|
|
/** ********************************************** */
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.end(
|
|
JSON.stringify({
|
|
msg: "Not Handled Yet",
|
|
})
|
|
);
|
|
}
|
|
});
|
|
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
|
|
/**
|
|
* ==============================================================================
|
|
* Fire up server
|
|
* ==============================================================================
|
|
*/
|
|
// const numCpus = os.cpus().length;
|
|
server.listen(PORT, () => {
|
|
console.log(`Listening on PORT ${PORT} => env: ${environment}`);
|
|
});
|