111 lines
3.0 KiB
TypeScript
111 lines
3.0 KiB
TypeScript
|
import { createServer } from "http";
|
||
|
import { parse } from "url";
|
||
|
import fs from "fs";
|
||
|
import mysql, { ServerlessMysql } from "serverless-mysql";
|
||
|
import next from "next";
|
||
|
|
||
|
import serverSocket from "./utils/socket";
|
||
|
import grabDbSSL from "./package-shared/utils/backend/grabDbSSL";
|
||
|
|
||
|
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 ? Number(process.env.WEB_PORT) : 7070;
|
||
|
|
||
|
const app = next({
|
||
|
dev,
|
||
|
hostname,
|
||
|
port,
|
||
|
});
|
||
|
|
||
|
/**
|
||
|
* # Setup Global Variables
|
||
|
*/
|
||
|
declare global {
|
||
|
var DSQL_DB_CONN: ServerlessMysql;
|
||
|
var DSQL_READ_ONLY_DB_CONN: ServerlessMysql;
|
||
|
var DSQL_FULL_ACCESS_DB_CONN: ServerlessMysql;
|
||
|
}
|
||
|
|
||
|
global.DSQL_DB_CONN = mysql({
|
||
|
config: {
|
||
|
host: process.env.DSQL_DB_HOST,
|
||
|
user: process.env.DSQL_DB_USERNAME,
|
||
|
password: process.env.DSQL_DB_PASSWORD,
|
||
|
database: process.env.DSQL_DB_NAME,
|
||
|
port: process.env.DSQL_DB_PORT
|
||
|
? Number(process.env.DSQL_DB_PORT)
|
||
|
: undefined,
|
||
|
charset: "utf8mb4",
|
||
|
// ssl: grabDbSSL(),
|
||
|
},
|
||
|
});
|
||
|
|
||
|
global.DSQL_READ_ONLY_DB_CONN = mysql({
|
||
|
config: {
|
||
|
host: process.env.DSQL_DB_HOST,
|
||
|
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||
|
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||
|
port: process.env.DSQL_DB_PORT
|
||
|
? Number(process.env.DSQL_DB_PORT)
|
||
|
: undefined,
|
||
|
charset: "utf8mb4",
|
||
|
// ssl: grabDbSSL(),
|
||
|
},
|
||
|
});
|
||
|
|
||
|
global.DSQL_FULL_ACCESS_DB_CONN = mysql({
|
||
|
config: {
|
||
|
host: process.env.DSQL_DB_HOST,
|
||
|
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||
|
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
||
|
port: process.env.DSQL_DB_PORT
|
||
|
? Number(process.env.DSQL_DB_PORT)
|
||
|
: undefined,
|
||
|
charset: "utf8mb4",
|
||
|
// ssl: grabDbSSL(),
|
||
|
},
|
||
|
});
|
||
|
|
||
|
/**
|
||
|
* # Fire up Server
|
||
|
*/
|
||
|
const handle = app.getRequestHandler();
|
||
|
|
||
|
app.prepare()
|
||
|
.then(() => {
|
||
|
const server = createServer(async (req, res) => {
|
||
|
try {
|
||
|
const parsedUrl = parse(req.url || "", true);
|
||
|
const { pathname, query } = parsedUrl;
|
||
|
|
||
|
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 });
|
||
|
}
|