-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstarter.js
More file actions
128 lines (107 loc) · 4.01 KB
/
Copy pathstarter.js
File metadata and controls
128 lines (107 loc) · 4.01 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Core modules
import Express from "express";
import cors from "cors";
import dotenv from "dotenv";
import swaggerUi from "swagger-ui-express";
import swaggerJsDoc from "swagger-jsdoc";
// Application helpers and utilities
import { configureEnvironmentVariable } from "./src/helpers/enviroment.js";
import fileUpload from "./src/helpers/multer.js";
import connectDB from "./src/db/connection.js";
import { socketConnection } from "./src/helpers/sockets.js";
// Middleware
import errorHandler from "./src/middlewares/errors/errorHandler.js";
import notFoundHandler from "./src/middlewares/errors/notFoundHandler.js";
// Routes
import authRoutes from "./src/routes/auth.js";
import profileRoutes from "./src/routes/profile.js";
import friendRoutes from "./src/routes/friend.js";
import chatRoutes from "./src/routes/chat.js";
import messageRoutes from './src/routes/message.js';
// Load environment variables from .env
dotenv.config();
// Create app instance
const app = new Express();
// CORS Configuration
const DEPLOYMENT_API_URL = process.env.DEPLOYMENT_API_URL;
const CLIENT_URL = process.env.CLIENT_URL;
const allowedOrigins = [CLIENT_URL, DEPLOYMENT_API_URL, "http://localhost:3000", `http://localhost:${process.env.PORT || 3000}`];
const corsOptions = {
origin: (origin, callback) => {
if (allowedOrigins.includes(origin) || !origin) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
};
app.use(cors(corsOptions));
app.use(Express.json());
app.use(fileUpload.single("imageMessage"));
// Swagger setup
const NODE_ENV = configureEnvironmentVariable();
const swaggerOptions = {
swaggerDefinition: {
openapi: "3.0.0",
info: {
title: "ChatSphere API Documentation",
version: "1.0.0",
description: "API documentation for ChatSphere, a chat application.",
},
servers: NODE_ENV === "dev" || NODE_ENV === "test"
? [
{
url: `http://localhost:${process.env.PORT || 3000}${process.env.BASE_URL}`,
description: `${NODE_ENV === "test" ? "Test" : "Development"} server`,
},
]
: [
{
url: `${DEPLOYMENT_API_URL}${process.env.BASE_URL}`,
description: "Production server",
},
],
components: {
securitySchemes: {
bearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
},
},
},
security: [{ bearerAuth: [] }],
},
apis: ["./src/routes/*.js"],
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs));
// Route registration
app.use(`${process.env.BASE_URL}/profile`, profileRoutes);
app.use(`${process.env.BASE_URL}/auth`, authRoutes);
app.use(`${process.env.BASE_URL}/users`, friendRoutes);
app.use(`${process.env.BASE_URL}/chats`, chatRoutes);
app.use(`${process.env.BASE_URL}/messages`, messageRoutes);
// Error handling
app.use(errorHandler);
app.use(notFoundHandler);
// Initialization logic
export const initializeServer = async (port, server) => {
if (!NODE_ENV) {
throw new Error("Environment variable not set. Cannot start the server.");
}
console.log(`Connecting to the database for ${NODE_ENV} environment...`);
await connectDB(NODE_ENV);
if (NODE_ENV === "dev" || NODE_ENV === "test") {
console.log(`Server is running in ${NODE_ENV} mode.`);
console.log(`→ http://localhost:${port}${process.env.BASE_URL}`);
console.log(`→ Swagger Docs: http://localhost:${port}/docs`);
} else {
console.log("Server is running in production mode.");
console.log(`→ ${DEPLOYMENT_API_URL}${process.env.BASE_URL}`);
console.log(`→ Swagger Docs: ${DEPLOYMENT_API_URL}/docs`);
}
// WebSocket
socketConnection(server);
};
export default app;