perf: optimize program start-up process

This commit is contained in:
Rafi Arrafif
2026-01-29 13:24:52 +07:00
parent 5c7e82cd52
commit 1b039a3d31
3 changed files with 38 additions and 20 deletions

View File

@ -1,18 +1,25 @@
import { middleware } from "./middleware";
import { validateEnv } from "./utils/startups/validateEnv";
validateEnv();
const { Elysia } = await import("elysia");
const { routes } = await import("./routes");
async function bootstrap() {
const { Elysia } = await import("elysia");
const { sentryInit } = await import("./utils/monitoring/sentry/init");
sentryInit();
const { routes } = require("./routes");
const { sentryInit } = require("./utils/monitoring/sentry/init");
const app = new Elysia()
sentryInit();
console.log("\x1b[1m\x1b[33m🚀 Starting backend services...\x1b[0m");
const app = new Elysia()
.use(middleware)
.use(routes)
.listen(process.env.APP_PORT || 3000);
console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
);
console.log(
`\x1b[1m\x1b[32m✅ Backend service started on: ${process.env.APP_URL}\x1b[0m`,
);
}
bootstrap();

View File

@ -1,8 +1,16 @@
import { init } from "@sentry/node";
export const sentryInit = () =>
export const sentryInit = () => {
console.log("🔧 Initializing Sentry...");
try {
init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 1.0,
environment: process.env.APP_ENV,
});
console.log("✅ Sentry initialized.");
} catch (error) {
console.error("❌ Failed to initialize Sentry:", error);
process.exit(1);
}
};

View File

@ -1,18 +1,19 @@
import fs from "fs";
export const validateEnv = () => {
console.log("🔍 Validating environment variables...");
if (!fs.existsSync(".env")) {
if (fs.existsSync(".env.example")) {
console.error("⚠️ .env file not found");
console.warn("📝 Creating .env file from .env.example, please wait...");
fs.copyFileSync(".env.example", ".env");
console.warn(
"🖊️ .env file successfully created please fill in the value in each key needed"
"🖊️ .env file successfully created please fill in the value in each key needed",
);
process.exit(1);
} else {
console.error(
`❌ Can't validate environment variable because can't find .env.example file. seems to be missing files please re-pull with “git pull main”`
`❌ Can't validate environment variable because can't find .env.example file. seems to be missing files please re-pull with “git pull main”`,
);
process.exit(1);
}
@ -25,7 +26,7 @@ export const validateEnv = () => {
.filter((key) => key && !key.startsWith("#"));
const missingKeys = exampleKeys.filter(
(key) => !process.env[key] || process.env[key].trim() === ""
(key) => !process.env[key] || process.env[key].trim() === "",
);
if (missingKeys.length > 0) {
@ -34,4 +35,6 @@ export const validateEnv = () => {
console.error(`check your .env file and make sure all keys are filled in`);
process.exit(1);
}
console.log("✅ Environment variables are valid.");
};