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.
107 lines
2.9 KiB
107 lines
2.9 KiB
import express from "express";
|
|
import swaggerui from "swagger-ui-express";
|
|
import fs from "fs";
|
|
import YAML from "yaml";
|
|
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";
|
|
import OAuthRoute from "./app/routes/oauth.route.js";
|
|
import session from "express-session";
|
|
import passport from "passport";
|
|
import { Strategy as GitHubStrategy } from "passport-github2";
|
|
|
|
console.clear();
|
|
dotenv.config();
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
// Increase body size limits for file uploads
|
|
app.use(express.urlencoded({extended: true, limit: '500mb'}));
|
|
app.use(express.json({limit: '500mb'}));
|
|
|
|
app.use(session({
|
|
secret: "your-secret",
|
|
resave: false,
|
|
saveUninitialized: false,
|
|
}));
|
|
|
|
// Swagger setup
|
|
const file = fs.readFileSync('./swagger.yaml', 'utf8');
|
|
const swaggerDocument = YAML.parse(file);
|
|
|
|
// Swagger UI options
|
|
const swaggerOptions = {
|
|
explorer: true,
|
|
swaggerOptions: {
|
|
requestInterceptor: (req) => {
|
|
req.headers['Content-Type'] = 'application/json';
|
|
return req;
|
|
}
|
|
}
|
|
};
|
|
|
|
app.use('/api/api-docs', swaggerui.serve, swaggerui.setup(swaggerDocument, swaggerOptions));
|
|
|
|
// --- Passport setup ---
|
|
app.use(passport.initialize());
|
|
app.use(passport.session());
|
|
|
|
// Serialize user -> session
|
|
passport.serializeUser((user, done) => {
|
|
done(null, user);
|
|
});
|
|
|
|
// Deserialize session -> user
|
|
passport.deserializeUser((obj, done) => {
|
|
done(null, obj);
|
|
});
|
|
|
|
// --- GitHub Strategy ---
|
|
passport.use(new GitHubStrategy({
|
|
clientID: process.env.GITHUB_ID,
|
|
clientSecret: process.env.GITHUB_SECRET,
|
|
callbackURL: "https://localhost/api/oauth/callback",
|
|
scope: ["user:email"]
|
|
},
|
|
(accessToken, refreshToken, profile, done) => {
|
|
|
|
return done(null, profile);
|
|
}
|
|
));
|
|
|
|
// 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);
|
|
app.use("/api/oauth", OAuthRoute);
|
|
|
|
const port = process.env.BACKEND_PORT;
|
|
|
|
if (process.env.NODE_ENV !== "test") {
|
|
const server = http.createServer(app);
|
|
server.listen(port, '0.0.0.0', async () => {
|
|
|
|
console.log(`Server started on port ${port}`);
|
|
await initDb();
|
|
console.log("Database initialized successfully.");
|
|
|
|
});
|
|
}
|
|
|
|
export default app;
|