youtuber-blog/src/index.js

91 lines
2.6 KiB
JavaScript
Raw Normal View History

2024-04-17 14:08:16 +00:00
import { initDb } from "./db/index.js";
2024-04-26 16:40:30 +00:00
import { channelRoutes, authRoutes, videoRoutes, meRoutes, blogRoutes } from "./routes/index.js";
2024-04-17 14:08:16 +00:00
import { env, Logger, Redis } from "./utils/index.js";
2023-06-10 00:37:36 +00:00
import fastify from "fastify";
2024-04-17 14:08:16 +00:00
import { middleware } from "./modules/middleware.js";
2024-04-21 22:41:38 +00:00
import oauth from '@fastify/oauth2';
2024-04-24 20:49:59 +00:00
import fastifyCookie from "@fastify/cookie";
2023-06-10 00:37:36 +00:00
const API_VERSION = "v1";
export const main = async () => {
const server = fastify({
bodyLimit: 1_000_000,
trustProxy: true,
});
await initDb();
2024-04-21 22:41:38 +00:00
// await Redis.initialize();
2023-06-10 00:37:36 +00:00
2024-04-24 20:49:59 +00:00
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);
2023-06-10 00:37:36 +00:00
server.register(import("@fastify/cors"), {
maxAge: 600,
origin: true,
credentials: true,
});
2024-04-21 22:41:38 +00:00
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',
2024-04-24 20:49:59 +00:00
// google redirect here after the user login
callbackUri: `${env.PUBLIC_API_URL}/auth/google/callback`
});
2023-06-10 00:37:36 +00:00
// Routes
2024-04-21 22:41:38 +00:00
server.register(channelRoutes, {
2024-04-24 20:49:59 +00:00
prefix: `/channels`,
2024-04-21 22:41:38 +00:00
});
server.register(videoRoutes, {
prefix: `/videos`,
2023-06-10 00:37:36 +00:00
});
2024-04-21 22:41:38 +00:00
server.register(authRoutes, {
prefix: `/auth`,
});
2024-04-24 20:49:59 +00:00
server.register(meRoutes, {
prefix: `/me`
});
2024-04-26 16:40:30 +00:00
server.register(blogRoutes, {
prefix: `/blog`
});
2024-04-21 22:41:38 +00:00
server.get("/hello", (req, res) => {
2024-04-24 20:49:59 +00:00
res.send({message: "world", cookies: req.cookies});
2024-04-21 22:41:38 +00:00
})
server.listen({ host: env.HOST, port: env.PORT }, (error, address) => {
if (error) {
Logger.error("INIT", error.message);
throw new Error(error.message);
2023-06-10 00:37:36 +00:00
}
Logger.info("INIT", `Server listening at ${address}`);
});
return server;
};
main();