dsql-admin/dsql-app/server.js

104 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-11-05 11:12:42 +00:00
// @ts-check
const { createServer } = require("http");
const { parse } = require("url");
const fs = require("fs");
const path = require("path");
require("dotenv").config({ path: path.join(__dirname, ".env") });
const next = require("next");
const production = process.env.NODE_ENV == "production";
const dev = process.env.NODE_ENV !== "production";
const isLocal = process.env.NEXT_PUBLIC_DSQL_LOCAL || null;
const hostname = "localhost";
const port = process.env.WEB_PORT || 7070;
2024-12-05 07:03:33 +00:00
const suAdminUserAuth = require("./functions/backend/suAdminUserAuth");
const userAuth = require("./functions/backend/userAuth");
2024-11-05 11:12:42 +00:00
// @ts-ignore
const app = next({
dev,
hostname,
port,
});
const handle = app.getRequestHandler();
const serverSocket = require("./utils/socket");
app.prepare()
.then(() => {
const server = createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url || "", true);
const { pathname, query } = parsedUrl;
2024-12-05 07:03:33 +00:00
// if (pathname?.match(/^\/admin\//)) {
// const user = await userAuth(req, res);
// console.log("user", user);
// if (!user?.logged_in_status) {
// res.writeHead(302, { Location: "/" });
// res.end();
// return;
// }
// }
// if (pathname?.match(/^\/su$/) || pathname?.match(/^\/su\//)) {
// const suAdminUser = await suAdminUserAuth(req);
// console.log("suAdminUser", suAdminUser);
// if (!suAdminUser?.email) {
// res.writeHead(302, { Location: "/admin" });
// res.end();
// return;
// }
// }
2024-11-05 11:12:42 +00:00
await handle(req, res, parsedUrl);
} catch (err) {
console.error("Error occurred handling", req.url, err);
res.statusCode = 500;
res.end("internal server error");
}
});
server.on("error", (err) => {
console.error("⚠️ SERVER ERROR =>", err.message);
});
server.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
serverSocket(server);
})
.catch((error) => {
console.log(error);
});
/**
* Setup needed directories
*/
if (!fs.existsSync("./.tmp")) {
fs.mkdirSync("./.tmp", { recursive: true });
}
// const MAX_LOG_FILE_LENGTH = 2000;
// const originalConsoleLog = console.log;
// console.log = function (...args) {
// const logMessage = args
// .map((arg) => (typeof arg === "object" ? JSON.stringify(arg) : arg))
// .join(" ");
// fs.appendFileSync("./log.log", logMessage + "\n", "utf8");
// originalConsoleLog.apply(console, args);
// };
/**
* # Start Cron Jobs
*/
// require("./utils/cron")();