Fastify (#9106)
* wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * fix * Update SignupApiService.ts * wip * wip * Update ClientServerService.ts * wip * wip * wip * Update WellKnownServerService.ts * wip * wip * update des * wip * Update ApiServerService.ts * wip * update deps * Update WellKnownServerService.ts * wip * update deps * Update ApiCallService.ts * Update ApiCallService.ts * Update ApiServerService.ts
This commit is contained in:
parent
2db9f6efe7
commit
3a7182bfb5
40 changed files with 1651 additions and 1977 deletions
|
@ -2,11 +2,7 @@ import cluster from 'node:cluster';
|
|||
import * as fs from 'node:fs';
|
||||
import * as http from 'node:http';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import Koa from 'koa';
|
||||
import Router from '@koa/router';
|
||||
import mount from 'koa-mount';
|
||||
import koaLogger from 'koa-logger';
|
||||
import * as slow from 'koa-slow';
|
||||
import Fastify from 'fastify';
|
||||
import { IsNull } from 'typeorm';
|
||||
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
||||
import type { Config } from '@/config.js';
|
||||
|
@ -58,47 +54,29 @@ export class ServerService {
|
|||
}
|
||||
|
||||
public launch() {
|
||||
// Init app
|
||||
const koa = new Koa();
|
||||
koa.proxy = true;
|
||||
|
||||
if (!['production', 'test'].includes(process.env.NODE_ENV ?? '')) {
|
||||
// Logger
|
||||
koa.use(koaLogger(str => {
|
||||
this.logger.info(str);
|
||||
}));
|
||||
|
||||
// Delay
|
||||
if (envOption.slow) {
|
||||
koa.use(slow({
|
||||
delay: 3000,
|
||||
}));
|
||||
}
|
||||
}
|
||||
const fastify = Fastify({
|
||||
trustProxy: true,
|
||||
logger: !['production', 'test'].includes(process.env.NODE_ENV ?? ''),
|
||||
});
|
||||
|
||||
// HSTS
|
||||
// 6months (15552000sec)
|
||||
if (this.config.url.startsWith('https') && !this.config.disableHsts) {
|
||||
koa.use(async (ctx, next) => {
|
||||
ctx.set('strict-transport-security', 'max-age=15552000; preload');
|
||||
await next();
|
||||
fastify.addHook('onRequest', (request, reply, done) => {
|
||||
reply.header('strict-transport-security', 'max-age=15552000; preload');
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
koa.use(mount('/api', this.apiServerService.createApiServer(koa)));
|
||||
koa.use(mount('/files', this.fileServerService.createServer()));
|
||||
koa.use(mount('/proxy', this.mediaProxyServerService.createServer()));
|
||||
fastify.register(this.apiServerService.createServer, { prefix: '/api' });
|
||||
fastify.register(this.fileServerService.createServer, { prefix: '/files' });
|
||||
fastify.register(this.mediaProxyServerService.createServer, { prefix: '/proxy' });
|
||||
fastify.register(this.activityPubServerService.createServer);
|
||||
fastify.register(this.nodeinfoServerService.createServer);
|
||||
fastify.register(this.wellKnownServerService.createServer);
|
||||
|
||||
// Init router
|
||||
const router = new Router();
|
||||
|
||||
// Routing
|
||||
router.use(this.activityPubServerService.createRouter().routes());
|
||||
router.use(this.nodeinfoServerService.createRouter().routes());
|
||||
router.use(this.wellKnownServerService.createRouter().routes());
|
||||
|
||||
router.get('/avatar/@:acct', async ctx => {
|
||||
const { username, host } = Acct.parse(ctx.params.acct);
|
||||
fastify.get<{ Params: { acct: string } }>('/avatar/@:acct', async (request, reply) => {
|
||||
const { username, host } = Acct.parse(request.params.acct);
|
||||
const user = await this.usersRepository.findOne({
|
||||
where: {
|
||||
usernameLower: username.toLowerCase(),
|
||||
|
@ -109,28 +87,25 @@ export class ServerService {
|
|||
});
|
||||
|
||||
if (user) {
|
||||
ctx.redirect(this.userEntityService.getAvatarUrlSync(user));
|
||||
reply.redirect(this.userEntityService.getAvatarUrlSync(user));
|
||||
} else {
|
||||
ctx.redirect('/static-assets/user-unknown.png');
|
||||
reply.redirect('/static-assets/user-unknown.png');
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/identicon/:x', async ctx => {
|
||||
fastify.get<{ Params: { x: string } }>('/identicon/:x', async (request, reply) => {
|
||||
const [temp, cleanup] = await createTemp();
|
||||
await genIdenticon(ctx.params.x, fs.createWriteStream(temp));
|
||||
ctx.set('Content-Type', 'image/png');
|
||||
ctx.body = fs.createReadStream(temp).on('close', () => cleanup());
|
||||
await genIdenticon(request.params.x, fs.createWriteStream(temp));
|
||||
reply.header('Content-Type', 'image/png');
|
||||
return fs.createReadStream(temp).on('close', () => cleanup());
|
||||
});
|
||||
|
||||
router.get('/verify-email/:code', async ctx => {
|
||||
fastify.get<{ Params: { code: string } }>('/verify-email/:code', async (request, reply) => {
|
||||
const profile = await this.userProfilesRepository.findOneBy({
|
||||
emailVerifyCode: ctx.params.code,
|
||||
emailVerifyCode: request.params.code,
|
||||
});
|
||||
|
||||
if (profile != null) {
|
||||
ctx.body = 'Verify succeeded!';
|
||||
ctx.status = 200;
|
||||
|
||||
await this.userProfilesRepository.update({ userId: profile.userId }, {
|
||||
emailVerified: true,
|
||||
emailVerifyCode: null,
|
||||
|
@ -140,21 +115,19 @@ export class ServerService {
|
|||
detail: true,
|
||||
includeSecrets: true,
|
||||
}));
|
||||
|
||||
reply.code(200);
|
||||
return 'Verify succeeded!';
|
||||
} else {
|
||||
ctx.status = 404;
|
||||
reply.code(404);
|
||||
}
|
||||
});
|
||||
|
||||
// Register router
|
||||
koa.use(router.routes());
|
||||
fastify.register(this.clientServerService.createServer);
|
||||
|
||||
koa.use(mount(this.clientServerService.createApp()));
|
||||
this.streamingApiServerService.attachStreamingApi(fastify.server);
|
||||
|
||||
const server = http.createServer(koa.callback());
|
||||
|
||||
this.streamingApiServerService.attachStreamingApi(server);
|
||||
|
||||
server.on('error', err => {
|
||||
fastify.server.on('error', err => {
|
||||
switch ((err as any).code) {
|
||||
case 'EACCES':
|
||||
this.logger.error(`You do not have permission to listen on port ${this.config.port}.`);
|
||||
|
@ -168,13 +141,13 @@ export class ServerService {
|
|||
}
|
||||
|
||||
if (cluster.isWorker) {
|
||||
process.send!('listenFailed');
|
||||
process.send!('listenFailed');
|
||||
} else {
|
||||
// disableClustering
|
||||
// disableClustering
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(this.config.port);
|
||||
fastify.listen({ port: this.config.port });
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue