fix(server): redirect to setup page if not initialized (#7871)
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
import { join } from 'node:path';
|
import { join, resolve } from 'node:path';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
DynamicModule,
|
DynamicModule,
|
||||||
ForwardReference,
|
ForwardReference,
|
||||||
Logger,
|
Logger,
|
||||||
|
MiddlewareConsumer,
|
||||||
Module,
|
Module,
|
||||||
|
NestModule,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ScheduleModule } from '@nestjs/schedule';
|
import { ScheduleModule } from '@nestjs/schedule';
|
||||||
import { ServeStaticModule } from '@nestjs/serve-static';
|
import { ServeStaticModule } from '@nestjs/serve-static';
|
||||||
@@ -16,7 +19,7 @@ import { ADD_ENABLED_FEATURES, ServerConfigModule } from './core/config';
|
|||||||
import { DocModule } from './core/doc';
|
import { DocModule } from './core/doc';
|
||||||
import { FeatureModule } from './core/features';
|
import { FeatureModule } from './core/features';
|
||||||
import { QuotaModule } from './core/quota';
|
import { QuotaModule } from './core/quota';
|
||||||
import { CustomSetupModule } from './core/setup';
|
import { CustomSetupModule, SetupMiddleware } from './core/setup';
|
||||||
import { StorageModule } from './core/storage';
|
import { StorageModule } from './core/storage';
|
||||||
import { SyncModule } from './core/sync';
|
import { SyncModule } from './core/sync';
|
||||||
import { UserModule } from './core/user';
|
import { UserModule } from './core/user';
|
||||||
@@ -135,16 +138,26 @@ export class AppModuleBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
compile() {
|
compile() {
|
||||||
|
const configure = (consumer: MiddlewareConsumer) => {
|
||||||
|
if (this.config.isSelfhosted) {
|
||||||
|
consumer.apply(SetupMiddleware).forRoutes('*');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: this.modules,
|
imports: this.modules,
|
||||||
controllers: this.config.isSelfhosted ? [] : [AppController],
|
controllers: this.config.isSelfhosted ? [] : [AppController],
|
||||||
})
|
})
|
||||||
class AppModule {}
|
class AppModule implements NestModule {
|
||||||
|
configure = configure;
|
||||||
|
}
|
||||||
|
|
||||||
return AppModule;
|
return AppModule;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const pwd = resolve(fileURLToPath(import.meta.url), '../../');
|
||||||
|
|
||||||
function buildAppModule() {
|
function buildAppModule() {
|
||||||
AFFiNE = mergeConfigOverride(AFFiNE);
|
AFFiNE = mergeConfigOverride(AFFiNE);
|
||||||
const factor = new AppModuleBuilder(AFFiNE);
|
const factor = new AppModuleBuilder(AFFiNE);
|
||||||
@@ -179,12 +192,12 @@ function buildAppModule() {
|
|||||||
config => config.isSelfhosted,
|
config => config.isSelfhosted,
|
||||||
CustomSetupModule,
|
CustomSetupModule,
|
||||||
ServeStaticModule.forRoot({
|
ServeStaticModule.forRoot({
|
||||||
rootPath: join('/app', 'static'),
|
rootPath: join(pwd, 'static', 'admin'),
|
||||||
exclude: ['/admin*'],
|
renderPath: /^\/admin\/?/,
|
||||||
}),
|
}),
|
||||||
ServeStaticModule.forRoot({
|
ServeStaticModule.forRoot({
|
||||||
rootPath: join('/app', 'static', 'admin'),
|
rootPath: join(pwd, 'static'),
|
||||||
serveRoot: '/admin',
|
renderPath: '*',
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,42 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Injectable, Module, NestMiddleware } from '@nestjs/common';
|
||||||
|
import { PrismaClient } from '@prisma/client';
|
||||||
|
import type { Request, Response } from 'express';
|
||||||
|
|
||||||
import { AuthModule } from '../auth';
|
import { AuthModule } from '../auth';
|
||||||
import { UserModule } from '../user';
|
import { UserModule } from '../user';
|
||||||
import { CustomSetupController } from './controller';
|
import { CustomSetupController } from './controller';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class SetupMiddleware implements NestMiddleware {
|
||||||
|
private initialized: boolean | null = null;
|
||||||
|
constructor(private readonly db: PrismaClient) {}
|
||||||
|
|
||||||
|
use(req: Request, res: Response, next: (error?: Error | any) => void) {
|
||||||
|
// never throw
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||||
|
this.allow().then(allowed => {
|
||||||
|
if (allowed) {
|
||||||
|
next();
|
||||||
|
} else if (!req.path.startsWith('/admin/setup')) {
|
||||||
|
res.redirect('/admin/setup');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async allow() {
|
||||||
|
try {
|
||||||
|
if (this.initialized === null) {
|
||||||
|
this.initialized = (await this.db.user.count()) > 0;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// avoid block the whole app
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.initialized;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [AuthModule, UserModule],
|
imports: [AuthModule, UserModule],
|
||||||
controllers: [CustomSetupController],
|
controllers: [CustomSetupController],
|
||||||
|
|||||||
Reference in New Issue
Block a user