oa_based/src/common/adapters/fastify.adapter.ts

46 lines
1.2 KiB
TypeScript

import FastifyCookie from '@fastify/cookie';
import FastifyMultipart from '@fastify/multipart';
import { FastifyAdapter } from '@nestjs/platform-fastify';
const app: FastifyAdapter = new FastifyAdapter({
trustProxy: true,
logger: false
// forceCloseConnections: true,
});
export { app as fastifyApp };
app.register(FastifyMultipart, {
attachFieldsToBody:true,
limits: {
fields: 10, // Max number of non-file fields
fileSize: 1024 * 1024 * 50, // limit size 50M
files: 5 // Max number of file fields
}
});
app.register(FastifyCookie, {
secret: 'cookie-secret' // 这个 secret 不太重要,不存鉴权相关,无关紧要
});
app.getInstance().addHook('onRequest', (request, reply, done) => {
// set undefined origin
const { origin } = request.headers;
if (!origin) request.headers.origin = request.headers.host;
// forbidden php
const { url } = request;
if (url.endsWith('.php')) {
reply.raw.statusMessage =
'Eh. PHP is not support on this machine. Yep, I also think PHP is bestest programming language. But for me it is beyond my reach.';
return reply.code(418).send();
}
// skip favicon request
if (url.match(/favicon.ico$/) || url.match(/manifest.json$/)) return reply.code(204).send();
done();
});