File size: 1,816 Bytes
56ddde0
 
bb4fd67
6413440
 
 
 
 
e298570
54c3ff7
64c6036
 
aa680e5
 
b45d8e6
 
 
54c3ff7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b45d8e6
54c3ff7
 
b45d8e6
 
bb815e1
aa680e5
 
 
 
 
 
 
 
 
b45d8e6
aa680e5
 
94ccbc7
aa680e5
 
 
 
e69b6a1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 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 };