Skip to content

Commit cd3924b

Browse files
committed
Revert "fix(server): redirect to setup page if not initialized (#7871)"
This reverts commit 42b5ef7.
1 parent 42b5ef7 commit cd3924b

File tree

2 files changed

+8
-54
lines changed

2 files changed

+8
-54
lines changed

packages/backend/server/src/app.module.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import { join, resolve } from 'node:path';
2-
import { fileURLToPath } from 'node:url';
1+
import { join } from 'node:path';
32

43
import {
54
DynamicModule,
65
ForwardReference,
76
Logger,
8-
MiddlewareConsumer,
97
Module,
10-
NestModule,
118
} from '@nestjs/common';
129
import { ScheduleModule } from '@nestjs/schedule';
1310
import { ServeStaticModule } from '@nestjs/serve-static';
@@ -19,7 +16,7 @@ import { ADD_ENABLED_FEATURES, ServerConfigModule } from './core/config';
1916
import { DocModule } from './core/doc';
2017
import { FeatureModule } from './core/features';
2118
import { QuotaModule } from './core/quota';
22-
import { CustomSetupModule, SetupMiddleware } from './core/setup';
19+
import { CustomSetupModule } from './core/setup';
2320
import { StorageModule } from './core/storage';
2421
import { SyncModule } from './core/sync';
2522
import { UserModule } from './core/user';
@@ -138,26 +135,16 @@ export class AppModuleBuilder {
138135
}
139136

140137
compile() {
141-
const configure = (consumer: MiddlewareConsumer) => {
142-
if (this.config.isSelfhosted) {
143-
consumer.apply(SetupMiddleware).forRoutes('*');
144-
}
145-
};
146-
147138
@Module({
148139
imports: this.modules,
149140
controllers: this.config.isSelfhosted ? [] : [AppController],
150141
})
151-
class AppModule implements NestModule {
152-
configure = configure;
153-
}
142+
class AppModule {}
154143

155144
return AppModule;
156145
}
157146
}
158147

159-
const pwd = resolve(fileURLToPath(import.meta.url), '../../');
160-
161148
function buildAppModule() {
162149
AFFiNE = mergeConfigOverride(AFFiNE);
163150
const factor = new AppModuleBuilder(AFFiNE);
@@ -192,12 +179,12 @@ function buildAppModule() {
192179
config => config.isSelfhosted,
193180
CustomSetupModule,
194181
ServeStaticModule.forRoot({
195-
rootPath: join(pwd, 'static', 'admin'),
196-
renderPath: /^\/admin\/?/,
182+
rootPath: join('/app', 'static'),
183+
exclude: ['/admin*'],
197184
}),
198185
ServeStaticModule.forRoot({
199-
rootPath: join(pwd, 'static'),
200-
renderPath: '*',
186+
rootPath: join('/app', 'static', 'admin'),
187+
serveRoot: '/admin',
201188
})
202189
);
203190

packages/backend/server/src/core/setup/index.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,9 @@
1-
import { Injectable, Module, NestMiddleware } from '@nestjs/common';
2-
import { PrismaClient } from '@prisma/client';
3-
import type { Request, Response } from 'express';
1+
import { Module } from '@nestjs/common';
42

53
import { AuthModule } from '../auth';
64
import { UserModule } from '../user';
75
import { CustomSetupController } from './controller';
86

9-
@Injectable()
10-
export class SetupMiddleware implements NestMiddleware {
11-
private initialized: boolean | null = null;
12-
constructor(private readonly db: PrismaClient) {}
13-
14-
use(req: Request, res: Response, next: (error?: Error | any) => void) {
15-
// never throw
16-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
17-
this.allow().then(allowed => {
18-
if (allowed) {
19-
next();
20-
} else if (!req.path.startsWith('/admin/setup')) {
21-
res.redirect('/admin/setup');
22-
}
23-
});
24-
}
25-
26-
async allow() {
27-
try {
28-
if (this.initialized === null) {
29-
this.initialized = (await this.db.user.count()) > 0;
30-
}
31-
} catch (e) {
32-
// avoid block the whole app
33-
return true;
34-
}
35-
36-
return this.initialized;
37-
}
38-
}
39-
407
@Module({
418
imports: [AuthModule, UserModule],
429
controllers: [CustomSetupController],

0 commit comments

Comments
 (0)