⚡ perf: optimize program start-up process
This commit is contained in:
13
src/index.ts
13
src/index.ts
@ -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();
|
||||||
|
|
||||||
|
async function bootstrap() {
|
||||||
const { Elysia } = await import("elysia");
|
const { Elysia } = await import("elysia");
|
||||||
const { routes } = await import("./routes");
|
|
||||||
|
|
||||||
const { sentryInit } = await import("./utils/monitoring/sentry/init");
|
const { routes } = require("./routes");
|
||||||
|
const { sentryInit } = require("./utils/monitoring/sentry/init");
|
||||||
|
|
||||||
sentryInit();
|
sentryInit();
|
||||||
|
|
||||||
|
console.log("\x1b[1m\x1b[33m🚀 Starting backend services...\x1b[0m");
|
||||||
const app = new Elysia()
|
const app = new Elysia()
|
||||||
.use(middleware)
|
.use(middleware)
|
||||||
.use(routes)
|
.use(routes)
|
||||||
.listen(process.env.APP_PORT || 3000);
|
.listen(process.env.APP_PORT || 3000);
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
|
`\x1b[1m\x1b[32m✅ Backend service started on: ${process.env.APP_URL}\x1b[0m`,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
bootstrap();
|
||||||
|
|||||||
@ -1,8 +1,16 @@
|
|||||||
import { init } from "@sentry/node";
|
import { init } from "@sentry/node";
|
||||||
|
|
||||||
export const sentryInit = () =>
|
export const sentryInit = () => {
|
||||||
|
console.log("🔧 Initializing Sentry...");
|
||||||
|
try {
|
||||||
init({
|
init({
|
||||||
dsn: process.env.SENTRY_DSN,
|
dsn: process.env.SENTRY_DSN,
|
||||||
tracesSampleRate: 1.0,
|
tracesSampleRate: 1.0,
|
||||||
environment: process.env.APP_ENV,
|
environment: process.env.APP_ENV,
|
||||||
});
|
});
|
||||||
|
console.log("✅ Sentry initialized.");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("❌ Failed to initialize Sentry:", error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@ -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.");
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user