import { PrismaClient } from '../../generated/prisma' import { withAccelerate } from "@prisma/extension-accelerate" import { type FastifyInstance, type FastifyReply, type FastifyRequest } from 'fastify' import { env } from '../config/env' import { constants } from '../config/constants' const prisma = new PrismaClient().$extends(withAccelerate()) export default async function indexRoutes(fastify: FastifyInstance): Promise { fastify.get('/profile', async function (request, reply) { const userId = request.session.get('userId') const user = await prisma.user.findUnique({ where: { id: userId }, include: { roles: true } }) return reply.viewAsync("profile.hbs", { user, site: constants.site }); }) fastify.get('/', async function (request, reply) { const cdnOrigin = env.CDN_ORIGIN; const NODE_ENV = env.NODE_ENV; const userId = request.session.get('userId'); console.log(`we are at the GET root (/) route, with userId=${userId}`); const vods = await prisma.vod.findMany({ take: 3, orderBy: { createdAt: 'desc' } }) console.log('vods as follows') console.log(vods) const vtubers = await prisma.vtuber.findMany({ take: 3, orderBy: { createdAt: 'desc' } }) // Guard: no user in session if (!userId) { const authPath = env.PATREON_AUTHORIZE_PATH console.log(`either patreon_user or patreon_user.id was falsy. userId=${userId}`) return reply.viewAsync("index.hbs", { user: { roles: [{ name: 'anon' }] }, cdnOrigin, obsToken: null, NODE_ENV, vods, vtubers, // streams, // tags, patreonChannels: [], authPath, site: constants.site }); } // Safe to query database const user = await prisma.user.findFirstOrThrow({ where: { id: userId, }, include: { roles: true } }); console.log('user as follows'); console.log(user); const authPath = env.PATREON_AUTHORIZE_PATH return reply.viewAsync("index.hbs", { user, cdnOrigin, NODE_ENV, authPath, vods, vtubers, // streams, // tags, site: constants.site, }); }); fastify.get('/health', function (request, reply) { return reply.send('OK') }) fastify.get('/version', function (request, reply) { return reply.send(constants.site.version) }) }