|
1 | | -import "reflect-metadata"; |
| 1 | +// Load environment and Firebase configuration first |
2 | 2 | import "dotenv/config"; |
| 3 | +import "./firebase"; |
| 4 | +import "reflect-metadata"; |
3 | 5 |
|
4 | | -import dotenv from "dotenv"; |
5 | 6 | import { |
6 | 7 | createExpressServer, |
7 | | - ForbiddenError, |
8 | | - UnauthorizedError, |
9 | 8 | useContainer as routingUseContainer, |
10 | | - HttpError, |
11 | 9 | } from "routing-controllers"; |
12 | | - |
13 | 10 | import { getManager, useContainer } from "typeorm"; |
14 | 11 | import { Container } from "typeorm-typedi-extensions"; |
15 | | -import { Express } from "express"; |
| 12 | +import { Express, Request, Response } from "express"; |
16 | 13 | import * as swaggerUi from "swagger-ui-express"; |
17 | | -import * as path from "path"; |
18 | | -import * as admin from "firebase-admin"; |
19 | | - |
20 | | -dotenv.config(); |
21 | | - |
22 | | -const serviceAccountPath = process.env.FIREBASE_SERVICE_ACCOUNT_PATH || ""; |
23 | | -const serviceAccount = require(serviceAccountPath); |
24 | | - |
25 | | -if (!serviceAccountPath) { |
26 | | - throw new Error( |
27 | | - "FIREBASE_SERVICE_ACCOUNT_PATH environment variable is not set.", |
28 | | - ); |
29 | | -} |
30 | | - |
31 | | -if (!admin.apps.length) { |
32 | | - admin.initializeApp({ |
33 | | - credential: admin.credential.cert(serviceAccount), |
34 | | - }); |
35 | | -} |
36 | | - |
37 | | -export { admin }; // Export the admin instance |
| 14 | +import swaggerDocument from "../swagger.json"; |
38 | 15 |
|
39 | 16 | import { controllers } from "./api/controllers"; |
40 | 17 | import { middlewares } from "./api/middlewares"; |
41 | | -import { UserModel } from "./models/UserModel"; |
42 | | -import { ReportController } from "./api/controllers/ReportController"; |
| 18 | +import { FirebaseCurrentUserChecker } from "./api/middlewares/FirebaseAuth"; |
43 | 19 | import resellConnection from "./utils/DB"; |
44 | | -import { ReportService } from "./services/ReportService"; |
45 | | -import { reportToString } from "./utils/Requests"; |
46 | | - |
47 | | -dotenv.config(); |
48 | 20 |
|
| 21 | +const port = process.env.PORT ?? 3000; |
| 22 | +const app: Express = createExpressServer({ |
| 23 | + cors: true, |
| 24 | + routePrefix: "/api/", |
| 25 | + controllers: controllers, |
| 26 | + middlewares: middlewares, |
| 27 | + defaults: { |
| 28 | + paramOptions: { |
| 29 | + required: true, // Make all params required by default |
| 30 | + }, |
| 31 | + }, |
| 32 | + validation: true, |
| 33 | + development: process.env.NODE_ENV !== "production", |
| 34 | + defaultErrorHandler: false, |
| 35 | + currentUserChecker: FirebaseCurrentUserChecker, |
| 36 | +}); |
| 37 | + |
| 38 | +/** |
| 39 | + * Setup Swagger docs |
| 40 | + */ |
| 41 | +app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); |
| 42 | +console.log( |
| 43 | + `Swagger documentation available at http://localhost:${port}/api-docs`, |
| 44 | +); |
| 45 | + |
| 46 | +/** |
| 47 | + * Health check endpoint |
| 48 | + */ |
| 49 | +app.get("/health", async (_req: Request, res: Response) => { |
| 50 | + const manager = getManager(); |
| 51 | + try { |
| 52 | + await manager.query("SELECT 1"); |
| 53 | + res.status(200).json({ status: "healthy", database: "Connected" }); |
| 54 | + } catch (error) { |
| 55 | + res |
| 56 | + .status(500) |
| 57 | + .json({ status: "Error", database: "Not connected", error: error }); |
| 58 | + } |
| 59 | +}); |
| 60 | + |
| 61 | +// Setup dependency injection containers |
| 62 | +routingUseContainer(Container); |
| 63 | +useContainer(Container); |
| 64 | + |
| 65 | +// Initialize and start application |
49 | 66 | async function main() { |
50 | | - routingUseContainer(Container); |
51 | | - useContainer(Container); |
52 | | - |
53 | | - await resellConnection().catch((error: any) => { |
| 67 | + // Initialize database connection |
| 68 | + await resellConnection().catch((error: unknown) => { |
54 | 69 | console.log(error); |
55 | 70 | throw new Error("Connection to DB failed. Check console output"); |
56 | 71 | }); |
|
0 commit comments