youtuber-blog/src/index.js

78 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-04-17 14:08:16 +00:00
import { initDb } from "./db/index.js";
2024-04-21 22:41:38 +00:00
import { channelRoutes, authRoutes } 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';
import { videoRoutes } from "./routes/videos.js";
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
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',
// facebook redirect here after the user login
callbackUri: 'http://localhost:3000/auth/google/callback'
})
2023-06-10 00:37:36 +00:00
// Routes
2024-04-21 22:41:38 +00:00
server.register(channelRoutes, {
prefix: `/`,
});
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`,
});
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);
2023-06-10 00:37:36 +00:00
}
Logger.info("INIT", `Server listening at ${address}`);
});
return server;
};
main();