First Commit
This commit is contained in:
Executable
+168
@@ -0,0 +1,168 @@
|
||||
// @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}`);
|
||||
});
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "webhook",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "webhook",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"dotenv": "^16.3.1"
|
||||
}
|
||||
},
|
||||
"node_modules/dotenv": {
|
||||
"version": "16.3.1",
|
||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz",
|
||||
"integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==",
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/motdotla/dotenv?sponsor=1"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "webhook",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"dotenv": "^16.3.1"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user