2024-04-17 14:08:16 +00:00
import { initDb } from "./db/index.js" ;
2024-05-29 19:35:18 +00:00
import { channelRoutes , authRoutes , videoRoutes , meRoutes , blogRoutes , dashboardRoutes } 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" ;
2024-04-28 18:58:31 +00:00
// import fastifyMultipart from "@fastify/multipart";
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 ,
2024-04-28 18:58:31 +00:00
// logger: true
2023-06-10 00:37:36 +00:00
} ) ;
await initDb ( ) ;
2024-04-21 22:41:38 +00:00
// await Redis.initialize();
2023-06-10 00:37:36 +00:00
2024-04-28 18:58:31 +00:00
// server.register(fastifyMultipart, {
// // attachFieldsToBody: true,
// });
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
} ) ;
2023-12-13 19:35:55 +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 : {
2024-05-30 07:17:59 +00:00
id : env . GOOGLE _CLIENT _ID ,
secret : env . GOOGLE _SECRET
2024-04-21 22:41:38 +00:00
} ,
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-05-12 20:05:06 +00:00
server . register ( dashboardRoutes , {
prefix : ` /dashboard `
} ) ;
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
} )
2023-12-13 19:35:55 +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 ( ) ;