import { initDb } from "./db/index.js"; import { channelRoutes, authRoutes } from "./routes/index.js"; import { env, Logger, Redis } from "./utils/index.js"; import fastify from "fastify"; import { middleware } from "./modules/middleware.js"; import oauth from '@fastify/oauth2'; import { videoRoutes } from "./routes/videos.js"; const API_VERSION = "v1"; export const main = async () => { const server = fastify({ bodyLimit: 1_000_000, trustProxy: true, }); await initDb(); // await Redis.initialize(); server.register(middleware); server.register(import("@fastify/cors"), { maxAge: 600, origin: true, credentials: true, }); server.register(oauth, { name: 'googleOAuth2', scope: ['https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/youtube.force-ssl', "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"], credentials: { client: { id: '103003963446-6m80lqbe4bkgjv1nq5ihmju0bb6agkkg.apps.googleusercontent.com', secret: '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' }, auth: oauth.GOOGLE_CONFIGURATION }, callbackUriParams: { // custom query param that will be passed to callbackUri access_type: 'offline', // will tell Google to send a refreshToken too }, pkce: 'S256', // register a fastify url to start the redirect flow startRedirectPath: '/auth/google', // facebook redirect here after the user login callbackUri: 'http://localhost:3000/auth/google/callback' }) // Routes server.register(channelRoutes, { prefix: `/`, }); server.register(videoRoutes, { prefix: `/videos`, }); server.register(authRoutes, { prefix: `/auth`, }); server.get("/hello", (req, res) => { res.send("world"); }) server.listen({ host: env.HOST, port: env.PORT }, (error, address) => { if (error) { Logger.error("INIT", error.message); throw new Error(error.message); } Logger.info("INIT", `Server listening at ${address}`); }); return server; }; main();