109 lines
2.9 KiB
TypeScript
109 lines
2.9 KiB
TypeScript
import fastify from 'fastify';
|
|
import fastifyAutoload from '@fastify/autoload';
|
|
import { PrismaClient } from '@prisma/client';
|
|
// import { Client as MinioClient } from 'minio';
|
|
import { S3Client } from '@aws-sdk/client-s3';
|
|
import fastifyCookie from '@fastify/cookie';
|
|
// import { readFile } from 'fs/promises';
|
|
// import { createClient, RedisClientType } from 'redis';
|
|
// import fastifySchedule from '@fastify/schedule';
|
|
// import fastifyCors from '@fastify/cors';
|
|
|
|
import path from 'path';
|
|
import { env } from 'process';
|
|
// import { FromSchema } from 'json-schema-to-ts';
|
|
|
|
declare module 'fastify' {
|
|
interface FastifyInstance {
|
|
db: PrismaClient;
|
|
storage: S3Client;
|
|
// redis: RedisClientType;
|
|
}
|
|
interface FastifyReply {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
sendError: (error: any, code?: number, explanation?: string) => void;
|
|
}
|
|
}
|
|
|
|
declare global {
|
|
namespace NodeJS {
|
|
interface ProcessEnv {
|
|
NODE_ENV: 'development' | 'production';
|
|
PORT: number;
|
|
JWT_REFRESH_SECRET: string;
|
|
JWT_ACCESS_SECRET: string;
|
|
AUTH_CLIENT_ID: string;
|
|
AUTH_CLIENT_SECRET: string;
|
|
REDIS_URL: string;
|
|
URL_TOKEN: string;
|
|
S3_ENDPOINT: string;
|
|
S3_ID: string;
|
|
S3_KEY: string;
|
|
AUTH_SECRET: string;
|
|
AUTH_SALT: string;
|
|
}
|
|
}
|
|
}
|
|
|
|
fastify({
|
|
logger: true,
|
|
ajv: { customOptions: { allErrors: true } }
|
|
})
|
|
.decorate('db', new PrismaClient())
|
|
.decorate(
|
|
'storage',
|
|
new S3Client({
|
|
endpoint: process.env.S3_ENDPOINT,
|
|
region: 'us-east',
|
|
credentials: {
|
|
accessKeyId: process.env.S3_ID,
|
|
secretAccessKey: process.env.S3_KEY
|
|
}
|
|
})
|
|
)
|
|
.register(fastifyCookie)
|
|
// .decorate('redis', async () => {
|
|
// const client = createClient({
|
|
// url: process.env.REDIS_URL
|
|
// });
|
|
|
|
// client.on('error', err => console.log('Redis Client Error', err));
|
|
|
|
// await client.connect();
|
|
// return client;
|
|
// })
|
|
.addHook('onRoute', route => console.log(route.url))
|
|
// .register(fastifyCors, {
|
|
// origin: ['http://127.0.0.1:3000'],
|
|
// credentials: true
|
|
// })
|
|
.decorateReply(
|
|
'sendError',
|
|
function (
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
error: any,
|
|
code = 500,
|
|
explanation = 'Unexpected error occured. Issue will be reported to staff.'
|
|
) {
|
|
this.log.error(error);
|
|
this.code(code).send(new Error(explanation));
|
|
}
|
|
)
|
|
.register(fastifyAutoload, {
|
|
dir: path.join(__dirname, 'routes'),
|
|
routeParams: true,
|
|
autoHooks: true,
|
|
options: { prefix: '/api' }
|
|
})
|
|
// .register(fastifyAutoload, {
|
|
// dir: path.join(__dirname, 'link'),
|
|
// routeParams: true,
|
|
// autoHooks: true,
|
|
// options: { prefix: '/a' }
|
|
// })
|
|
.listen({ port: process.env.PORT || 8080, host: '0.0.0.0' })
|
|
// eslint-disable-next-line no-console
|
|
.then(() => console.log('Server Started'))
|
|
// eslint-disable-next-line no-console
|
|
.catch(console.error);
|