Skip to content

Commit

Permalink
Revert "fix(server): redirect to setup page if not initialized (#7871)"
Browse files Browse the repository at this point in the history
This reverts commit 42b5ef7.
  • Loading branch information
forehalo committed Aug 14, 2024
1 parent 42b5ef7 commit cd3924b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 54 deletions.
27 changes: 7 additions & 20 deletions packages/backend/server/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { join } from 'node:path';

import {
DynamicModule,
ForwardReference,
Logger,
MiddlewareConsumer,
Module,
NestModule,
} from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
import { ServeStaticModule } from '@nestjs/serve-static';
Expand All @@ -19,7 +16,7 @@ import { ADD_ENABLED_FEATURES, ServerConfigModule } from './core/config';
import { DocModule } from './core/doc';
import { FeatureModule } from './core/features';
import { QuotaModule } from './core/quota';
import { CustomSetupModule, SetupMiddleware } from './core/setup';
import { CustomSetupModule } from './core/setup';
import { StorageModule } from './core/storage';
import { SyncModule } from './core/sync';
import { UserModule } from './core/user';
Expand Down Expand Up @@ -138,26 +135,16 @@ export class AppModuleBuilder {
}

compile() {
const configure = (consumer: MiddlewareConsumer) => {
if (this.config.isSelfhosted) {
consumer.apply(SetupMiddleware).forRoutes('*');
}
};

@Module({
imports: this.modules,
controllers: this.config.isSelfhosted ? [] : [AppController],
})
class AppModule implements NestModule {
configure = configure;
}
class AppModule {}

return AppModule;
}
}

const pwd = resolve(fileURLToPath(import.meta.url), '../../');

function buildAppModule() {
AFFiNE = mergeConfigOverride(AFFiNE);
const factor = new AppModuleBuilder(AFFiNE);
Expand Down Expand Up @@ -192,12 +179,12 @@ function buildAppModule() {
config => config.isSelfhosted,
CustomSetupModule,
ServeStaticModule.forRoot({
rootPath: join(pwd, 'static', 'admin'),
renderPath: /^\/admin\/?/,
rootPath: join('/app', 'static'),
exclude: ['/admin*'],
}),
ServeStaticModule.forRoot({
rootPath: join(pwd, 'static'),
renderPath: '*',
rootPath: join('/app', 'static', 'admin'),
serveRoot: '/admin',
})
);

Expand Down
35 changes: 1 addition & 34 deletions packages/backend/server/src/core/setup/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
import { Injectable, Module, NestMiddleware } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import type { Request, Response } from 'express';
import { Module } from '@nestjs/common';

import { AuthModule } from '../auth';
import { UserModule } from '../user';
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({
imports: [AuthModule, UserModule],
controllers: [CustomSetupController],
Expand Down

0 comments on commit cd3924b

Please sign in to comment.