feat: add self checking

This commit is contained in:
Aidan 2025-04-17 15:54:34 -04:00
parent 3d08b14e0f
commit abe9c6994d
2 changed files with 63 additions and 1 deletions

View File

@ -0,0 +1,52 @@
import fs from "fs";
const requiredEnvVars = ['MIGRATE_TXT', 'DB_FILE_NAME', 'MAILCONNECT_ROOT_DIR'];
function checkRatelimit() {
const ratelimitExists = fs.existsSync("ratelimit.json");
if (!ratelimitExists) {
console.log("[!] Ratelimit config not found");
return false;
} else {
console.log("[✓] Found ratelimit config");
return true;
};
};
function checkEnv() {
const envExists = fs.existsSync(".env");
if (!envExists) {
console.log("[!] .env file not found");
return false;
} else {
console.log("[✓] Found .env file");
let allVarsPresent = true;
for (const envVar of requiredEnvVars) {
if (!process.env[envVar]) {
console.log(` [!] ${envVar} not found`);
allVarsPresent = false;
} else {
console.log(` [✓] Found ${envVar}`);
}
}
if (!allVarsPresent) {
return false;
} else {
return true;
}
};
};
// the return value of this function determines if server should exit early
export default function initialChecks() {
const ratelimitCheck = checkRatelimit();
const envCheck = checkEnv();
if (!ratelimitCheck || !envCheck) {
return false;
} else {
return true;
}
};

View File

@ -5,10 +5,20 @@ import { listAccounts } from "./actions/accounts/listAccounts";
import { getUserAccount } from "./actions/accounts/getUserAccount";
import { updatePassword } from "./actions/accounts/updatePassword";
import { addAccount } from "./actions/accounts/addAccount";
import initialChecks from "./actions/initialChecks";
const app = express();
app.use(express.json());
console.log("==== SELF CHECK STARTING ====\n");
const rCResult = initialChecks();
if (!rCResult) {
console.log("\n==== SELF CHECK FAIL ====");
process.exit(1);
} else {
console.log("\n==== SELF CHECK PASS ====\n");
}
interface RateLimitOptions {
windowMs: number;
limit: number;
@ -35,7 +45,7 @@ app.listen(PORT, () => {
console.log('mail-connect');
console.log('Version: 0.1.1');
console.log(`API listening on port ${PORT}\n`);
console.dir("[!] " + err);
console.log("[!] " + err);
} else {
console.log(data);
console.log('Version: 0.1.1');