Files
chad/server/server.ts
Ivan Grachyov ca773a56c6 chat WIP
2025-12-26 23:36:21 +03:00

55 lines
1.2 KiB
TypeScript

import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import FastifyAutoLoad from '@fastify/autoload'
import FastifyCookie from '@fastify/cookie'
import FastifyCors from '@fastify/cors'
import Fastify from 'fastify'
import { chatInit } from './modules/chat/index.ts'
import prisma from './prisma/client.ts'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const fastify = Fastify({
logger: true,
})
fastify.register(FastifyCors, {
origin: [
'http://localhost:3000',
'http://tauri.localhost',
'https://koptilnya.xyz',
'https://chad.koptilnya.xyz',
],
methods: ['PATCH', 'PUT', 'OPTIONS', 'GET', 'POST', 'DELETE'],
credentials: true,
})
fastify.register(FastifyCookie)
fastify.register(FastifyAutoLoad, {
dir: join(__dirname, 'plugins'),
})
fastify.register(FastifyAutoLoad, {
dir: join(__dirname, 'routes'),
options: { prefix: 'chad' },
})
;(async () => {
const port = process.env.PORT ? Number(process.env.PORT) : 4000
try {
await fastify.listen({ port, host: '0.0.0.0' })
await prisma.$connect()
fastify.log.info('Testing DB Connection. OK')
await chatInit()
}
catch (err) {
fastify.log.error(err)
process.exit(1)
}
})()