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 { middleware } from "./middleware";
import { validateEnv } from "./utils/startups/validateEnv"; import { validateEnv } from "./utils/startups/validateEnv";
validateEnv(); validateEnv();
const { Elysia } = await import("elysia"); async function bootstrap() {
const { routes } = await import("./routes"); const { Elysia } = await import("elysia");
const { sentryInit } = await import("./utils/monitoring/sentry/init"); const { routes } = require("./routes");
sentryInit(); const { sentryInit } = require("./utils/monitoring/sentry/init");
const app = new Elysia() sentryInit();
.use(middleware)
.use(routes)
.listen(process.env.APP_PORT || 3000);
console.log( console.log("\x1b[1m\x1b[33m🚀 Starting backend services...\x1b[0m");
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`, const app = new Elysia()
); .use(middleware)
.use(routes)
.listen(process.env.APP_PORT || 3000);
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"; import { init } from "@sentry/node";
export const sentryInit = () => export const sentryInit = () => {
init({ console.log("🔧 Initializing Sentry...");
dsn: process.env.SENTRY_DSN, try {
tracesSampleRate: 1.0, init({
environment: process.env.APP_ENV, 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"; import fs from "fs";
export const validateEnv = () => { export const validateEnv = () => {
console.log("🔍 Validating environment variables...");
if (!fs.existsSync(".env")) { if (!fs.existsSync(".env")) {
if (fs.existsSync(".env.example")) { if (fs.existsSync(".env.example")) {
console.error("⚠️ .env file not found"); console.error("⚠️ .env file not found");
console.warn("📝 Creating .env file from .env.example, please wait..."); console.warn("📝 Creating .env file from .env.example, please wait...");
fs.copyFileSync(".env.example", ".env"); fs.copyFileSync(".env.example", ".env");
console.warn( 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); process.exit(1);
} else { } else {
console.error( 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); process.exit(1);
} }
@ -25,7 +26,7 @@ export const validateEnv = () => {
.filter((key) => key && !key.startsWith("#")); .filter((key) => key && !key.startsWith("#"));
const missingKeys = exampleKeys.filter( const missingKeys = exampleKeys.filter(
(key) => !process.env[key] || process.env[key].trim() === "" (key) => !process.env[key] || process.env[key].trim() === "",
); );
if (missingKeys.length > 0) { 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`); console.error(`check your .env file and make sure all keys are filled in`);
process.exit(1); process.exit(1);
} }
console.log("✅ Environment variables are valid.");
}; };