You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.5 KiB
74 lines
2.5 KiB
import express from "express";
|
|
import dotenv from "dotenv";
|
|
import UserRoute from "./app/routes/user.route.js";
|
|
import ChannelRoute from "./app/routes/channel.route.js";
|
|
import VideoRoute from "./app/routes/video.route.js";
|
|
import CommentRoute from "./app/routes/comment.route.js";
|
|
import RecommendationRoute from "./app/routes/redommendation.route.js";
|
|
import * as http from "node:http";
|
|
import cors from "cors";
|
|
import PlaylistRoute from "./app/routes/playlist.route.js";
|
|
import {initDb} from "./app/utils/database.js";
|
|
import MediaRoutes from "./app/routes/media.routes.js";
|
|
import SearchRoute from "./app/routes/search.route.js";
|
|
|
|
console.clear();
|
|
dotenv.config();
|
|
|
|
|
|
const app = express();
|
|
|
|
// CORS Configuration for production deployment
|
|
const corsOptions = {
|
|
origin: function (origin, callback) {
|
|
// Allow requests with no origin (like mobile apps or curl requests)
|
|
if (!origin) return callback(null, true);
|
|
|
|
// Get allowed origins from environment variable
|
|
const allowedOrigins = process.env.CORS_ORIGIN ?
|
|
process.env.CORS_ORIGIN.split(',').map(url => url.trim()) :
|
|
['http://localhost:3000', 'https://localhost', 'http://localhost'];
|
|
|
|
if (allowedOrigins.indexOf(origin) !== -1) {
|
|
callback(null, true);
|
|
} else {
|
|
console.warn(`CORS blocked origin: ${origin}`);
|
|
callback(new Error('Not allowed by CORS'));
|
|
}
|
|
},
|
|
credentials: true,
|
|
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
|
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
|
|
optionsSuccessStatus: 200 // Some legacy browsers choke on 204
|
|
};
|
|
|
|
// INITIALIZE DATABASE
|
|
|
|
// Increase body size limits for file uploads
|
|
app.use(express.urlencoded({extended: true, limit: '500mb'}));
|
|
app.use(express.json({limit: '500mb'}));
|
|
app.use(cors(corsOptions))
|
|
|
|
// ROUTES
|
|
app.use("/api/users/", UserRoute);
|
|
app.use("/api/channels/", ChannelRoute);
|
|
app.use("/api/videos/", VideoRoute);
|
|
app.use("/api/comments/", CommentRoute);
|
|
app.use("/api/playlists", PlaylistRoute);
|
|
app.use("/api/recommendations", RecommendationRoute);
|
|
app.use("/api/media", MediaRoutes);
|
|
app.use("/api/search", SearchRoute);
|
|
|
|
const port = process.env.PORT;
|
|
|
|
if (process.env.NODE_ENV !== "test") {
|
|
const server = http.createServer(app);
|
|
server.listen(port, '0.0.0.0', async () => {
|
|
console.log("Server's listening on port " + port);
|
|
console.log("Initializing database...");
|
|
await initDb();
|
|
console.log("Database initialized successfully.");
|
|
});
|
|
}
|
|
|
|
export default app;
|