ryu-js / startup /lifestyle.js
randydev's picture
Update startup/lifestyle.js
e298570 verified
// startup like fastapi similar
import { Database } from '../database/database.js';
import {
UsernameDB,
PasswordDB
} from '../config.js';
import mongoose from 'mongoose';
import { ApiKey } from "../models.js";
const port = 7860
const startup = async () => {
try {
await mongoose.connect(`mongodb+srv://${UsernameDB}:${PasswordDB}@cluster0.1asx6h1.mongodb.net/AkenoXJs?retryWrites=true&w=majority`);
console.log("Mongoose connected successfully.");
const db = mongoose.connection.db;
const collections = await db.listCollections().toArray();
const collectionNames = collections.map(col => col.name);
if (collectionNames.includes("ApiKey")) {
const collection = db.collection("ApiKey");
try {
await collection.dropIndex("expiresAt_1");
console.log("Dropped TTL index on expiresAt.");
} catch (err) {
if (err.codeName === "IndexNotFound") {
console.log("No TTL index found, skipping dropIndex.");
} else {
console.error("Error dropping index:", err.message);
}
}
} else {
console.log("Collection 'ApiKey' does not exist yet. Skipping dropIndex.");
}
const dbClient = new Database("AkenoXJs");
console.log("Starting application...");
await dbClient.connect();
console.log("MongoDB connected successfully.");
} catch (error) {
console.error("Error during startup:", error.message);
process.exit(1);
}
};
const startServer = async (app) => {
await startup();
app.listen(port, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${port}`);
});
};
export { startServer };