import { initDb } from "./db/index.js"; import { channelRoutes, authRoutes, videoRoutes, meRoutes, blogRoutes, dashboardRoutes } from "./routes/index.js"; import { env, Logger } from "./utils/index.js"; import fastify from "fastify"; import { middleware } from "./modules/middleware.js"; import oauth from '@fastify/oauth2'; import fastifyCookie from "@fastify/cookie"; // import fastifyMultipart from "@fastify/multipart"; const API_VERSION = "v1"; export const main = async () => { const server = fastify({ bodyLimit: 1_000_000, trustProxy: true, // logger: true }); await initDb(); // await Redis.initialize(); // server.register(fastifyMultipart, { // // attachFieldsToBody: true, // }); server.register(fastifyCookie, { secret: "my-secret", // for cookies signature hook: 'preParsing', // set to false to disable cookie autoparsing or set autoparsing on any of the following hooks: 'onRequest', 'preParsing', 'preHandler', 'preValidation'. default: 'onRequest' parseOptions: {}, // options for parsing cookies }); 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: env.GOOGLE_CLIENT_ID, secret: env.GOOGLE_SECRET }, 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', // google redirect here after the user login callbackUri: `${env.PUBLIC_API_URL}/auth/google/callback` }); // Routes server.register(channelRoutes, { prefix: `/channels`, }); server.register(videoRoutes, { prefix: `/videos`, }); server.register(authRoutes, { prefix: `/auth`, }); server.register(meRoutes, { prefix: `/me` }); server.register(blogRoutes, { prefix: `/blog` }); server.register(dashboardRoutes, { prefix: `/dashboard` }); server.get("/hello", (req, res) => { res.send({ message: "world", cookies: req.cookies }); }) 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();