fix(server): node imports order (#5583)

This commit is contained in:
liuyi
2024-01-14 05:47:56 +00:00
parent 18907ebe57
commit 4c49b62ab7
16 changed files with 109 additions and 101 deletions

View File

@@ -14,8 +14,8 @@
"test": "ava --concurrency 1 --serial",
"test:coverage": "c8 ava --concurrency 1 --serial",
"postinstall": "prisma generate",
"data-migration": "node --loader ts-node/esm/transpile-only.mjs --es-module-specifier-resolution node ./src/data/app.ts",
"predeploy": "yarn prisma migrate deploy && node --es-module-specifier-resolution node ./dist/data/app.js run"
"data-migration": "node --loader ts-node/esm/transpile-only.mjs --es-module-specifier-resolution node ./src/data/index.ts",
"predeploy": "yarn prisma migrate deploy && node --es-module-specifier-resolution node ./dist/data/index.js run"
},
"dependencies": {
"@apollo/server": "^4.9.5",

View File

@@ -0,0 +1,38 @@
import { DynamicModule, Module, Type } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { AppController } from './app.controller';
import { CacheInterceptor, CacheModule } from './fundamentals/cache';
import { ConfigModule } from './fundamentals/config';
import { EventModule } from './fundamentals/event';
import { MailModule } from './fundamentals/mailer';
import { MetricsModule } from './fundamentals/metrics';
import { PrismaModule } from './fundamentals/prisma';
import { SessionModule } from './fundamentals/session';
import { RateLimiterModule } from './fundamentals/throttler';
import { BusinessModules } from './modules';
import { AuthModule } from './modules/auth';
export const FunctionalityModules: Array<Type | DynamicModule> = [
ConfigModule.forRoot(),
CacheModule,
PrismaModule,
MetricsModule,
EventModule,
SessionModule,
RateLimiterModule,
AuthModule,
MailModule,
];
@Module({
providers: [
{
provide: APP_INTERCEPTOR,
useClass: CacheInterceptor,
},
],
imports: [...FunctionalityModules, ...BusinessModules],
controllers: [AppController],
})
export class AppModule {}

View File

@@ -1,38 +1,42 @@
import { DynamicModule, Module, Type } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { NestFactory } from '@nestjs/core';
import type { NestExpressApplication } from '@nestjs/platform-express';
import cookieParser from 'cookie-parser';
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import { AppController } from './app.controller';
import { CacheInterceptor, CacheModule } from './fundamentals/cache';
import { ConfigModule } from './fundamentals/config';
import { EventModule } from './fundamentals/event';
import { MailModule } from './fundamentals/mailer';
import { MetricsModule } from './fundamentals/metrics';
import { PrismaModule } from './fundamentals/prisma';
import { SessionModule } from './fundamentals/session';
import { RateLimiterModule } from './fundamentals/throttler';
import { BusinessModules } from './modules';
import { AuthModule } from './modules/auth';
import { CacheRedis } from './fundamentals/cache/redis';
import { RedisIoAdapter } from './fundamentals/websocket';
import { ExceptionLogger } from './middleware/exception-logger';
import { serverTimingAndCache } from './middleware/timing';
export const FunctionalityModules: Array<Type | DynamicModule> = [
ConfigModule.forRoot(),
CacheModule,
PrismaModule,
MetricsModule,
EventModule,
SessionModule,
RateLimiterModule,
AuthModule,
MailModule,
];
export async function createApp() {
const { AppModule } = await import('./app.module');
@Module({
providers: [
{
provide: APP_INTERCEPTOR,
useClass: CacheInterceptor,
},
],
imports: [...FunctionalityModules, ...BusinessModules],
controllers: [AppController],
})
export class AppModule {}
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
cors: true,
rawBody: true,
bodyParser: true,
logger: AFFiNE.affine.stable ? ['log'] : ['verbose'],
});
app.use(serverTimingAndCache);
app.use(
graphqlUploadExpress({
// TODO: dynamic limit by quota
maxFileSize: 100 * 1024 * 1024,
maxFiles: 5,
})
);
app.useGlobalFilters(new ExceptionLogger());
app.use(cookieParser());
if (AFFiNE.redis.enabled) {
const redis = app.get(CacheRedis, { strict: false });
const redisIoAdapter = new RedisIoAdapter(app);
await redisIoAdapter.connectToRedis(redis);
app.useWebSocketAdapter(redisIoAdapter);
}
return app;
}

View File

@@ -1,9 +1,6 @@
import '../prelude';
import { Module } from '@nestjs/common';
import { Logger, Module } from '@nestjs/common';
import { CommandFactory } from 'nest-commander';
import { AppModule as BusinessAppModule } from '../app';
import { AppModule as BusinessAppModule } from '../app.module';
import { ConfigModule } from '../fundamentals/config';
import { CreateCommand, NameQuestion } from './commands/create';
import { RevertCommand, RunCommand } from './commands/run';
@@ -21,14 +18,4 @@ import { RevertCommand, RunCommand } from './commands/run';
],
providers: [NameQuestion, CreateCommand, RunCommand, RevertCommand],
})
class AppModule {}
async function bootstrap() {
await CommandFactory.run(AppModule, new Logger()).catch(e => {
console.error(e);
process.exit(1);
});
process.exit(0);
}
await bootstrap();
export class CliAppModule {}

View File

@@ -0,0 +1,16 @@
import '../prelude';
import { Logger } from '@nestjs/common';
import { CommandFactory } from 'nest-commander';
import { CliAppModule } from './app';
async function bootstrap() {
await CommandFactory.run(CliAppModule, new Logger()).catch(e => {
console.error(e);
process.exit(1);
});
process.exit(0);
}
await bootstrap();

View File

@@ -1,5 +1,3 @@
// eslint-disable-next-line simple-import-sort/imports
export * from './def';
export * from './default';
export { applyEnvToConfig, parseEnvValue } from './env';

View File

@@ -2,45 +2,9 @@
// keep the config import at the top
// eslint-disable-next-line simple-import-sort/imports
import './prelude';
import { createApp } from './app';
import { NestFactory } from '@nestjs/core';
import type { NestExpressApplication } from '@nestjs/platform-express';
import cookieParser from 'cookie-parser';
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import { AppModule } from './app';
import { RedisIoAdapter } from './fundamentals';
import { CacheRedis } from './fundamentals/cache/redis';
import { ExceptionLogger } from './middleware/exception-logger';
import { serverTimingAndCache } from './middleware/timing';
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
cors: true,
rawBody: true,
bodyParser: true,
logger: AFFiNE.affine.stable ? ['log'] : ['verbose'],
});
app.use(serverTimingAndCache);
app.use(
graphqlUploadExpress({
// TODO: dynamic limit by quota
maxFileSize: 100 * 1024 * 1024,
maxFiles: 5,
})
);
app.useGlobalFilters(new ExceptionLogger());
app.use(cookieParser());
if (AFFiNE.redis.enabled) {
const redis = app.get(CacheRedis, { strict: false });
const redisIoAdapter = new RedisIoAdapter(app);
await redisIoAdapter.connectToRedis(redis);
app.useWebSocketAdapter(redisIoAdapter);
}
const app = await createApp();
await app.listen(AFFiNE.port, AFFiNE.host);
console.log(

View File

@@ -9,7 +9,7 @@ import ava, { type TestFn } from 'ava';
import type { Express } from 'express';
import request from 'supertest';
import { AppModule } from '../src/app';
import { AppModule } from '../src/app.module';
import { FeatureManagementService } from '../src/modules/features';
import { createTestingApp } from './utils';

View File

@@ -6,7 +6,7 @@ import { type User } from '@prisma/client';
import { PrismaClient } from '@prisma/client';
import ava, { type TestFn } from 'ava';
import { AppModule } from '../src/app';
import { AppModule } from '../src/app.module';
import { MailService } from '../src/fundamentals/mailer';
import { FeatureKind, FeatureManagementService } from '../src/modules/features';
import { Quotas } from '../src/modules/quota';

View File

@@ -4,7 +4,7 @@ import test from 'ava';
test('should be able to bootstrap sync server', async t => {
// set env before import
process.env.SERVER_FLAVOR = 'sync';
const { AppModule } = await import('../src/app');
const { AppModule } = await import('../src/app.module');
await t.notThrowsAsync(async () => {
const module = await Test.createTestingModule({
imports: [AppModule],

View File

@@ -2,7 +2,7 @@ import type { INestApplication } from '@nestjs/common';
import test from 'ava';
import request from 'supertest';
import { AppModule } from '../src/app';
import { AppModule } from '../src/app.module';
import { createTestingApp, currentUser, signUp } from './utils';
let app: INestApplication;

View File

@@ -4,7 +4,7 @@ import { Test, TestingModuleBuilder } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client';
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import { AppModule, FunctionalityModules } from '../../src/app';
import { AppModule, FunctionalityModules } from '../../src/app.module';
import { UserFeaturesInit1698652531198 } from '../../src/data/migrations/1698652531198-user-features-init';
import { GqlModule } from '../../src/fundamentals/graphql';

View File

@@ -2,7 +2,7 @@ import type { INestApplication } from '@nestjs/common';
import test from 'ava';
import request from 'supertest';
import { AppModule } from '../src/app';
import { AppModule } from '../src/app.module';
import { FeatureManagementService, FeatureType } from '../src/modules/features';
import { QuotaService, QuotaType } from '../src/modules/quota';
import {

View File

@@ -6,7 +6,7 @@ import type { INestApplication } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import ava, { type TestFn } from 'ava';
import { AppModule } from '../src/app';
import { AppModule } from '../src/app.module';
import { MailService } from '../src/fundamentals/mailer';
import { AuthService } from '../src/modules/auth/service';
import {

View File

@@ -3,7 +3,7 @@ import { PrismaClient } from '@prisma/client';
import ava, { type TestFn } from 'ava';
import request from 'supertest';
import { AppModule } from '../src/app';
import { AppModule } from '../src/app.module';
import {
acceptInviteById,
createTestingApp,