-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(server): redirect to setup page if not initialized (#7875)
- Loading branch information
Showing
20 changed files
with
328 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.env | ||
static/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
@Injectable() | ||
export class ServerService { | ||
private _initialized: boolean | null = null; | ||
constructor(private readonly db: PrismaClient) {} | ||
|
||
async initialized() { | ||
if (!this._initialized) { | ||
const userCount = await this.db.user.count(); | ||
this._initialized = userCount > 0; | ||
} | ||
|
||
return this._initialized; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { join } from 'node:path'; | ||
|
||
import { | ||
Injectable, | ||
Module, | ||
NestMiddleware, | ||
OnModuleInit, | ||
} from '@nestjs/common'; | ||
import { HttpAdapterHost } from '@nestjs/core'; | ||
import type { Application, Request, Response } from 'express'; | ||
import { static as serveStatic } from 'express'; | ||
|
||
import { Config } from '../../fundamentals'; | ||
import { AuthModule } from '../auth'; | ||
import { ServerConfigModule, ServerService } from '../config'; | ||
import { UserModule } from '../user'; | ||
import { CustomSetupController } from './controller'; | ||
|
||
@Injectable() | ||
export class SetupMiddleware implements NestMiddleware { | ||
constructor(private readonly server: ServerService) {} | ||
|
||
use = (req: Request, res: Response, next: (error?: Error | any) => void) => { | ||
// never throw | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
this.server | ||
.initialized() | ||
.then(initialized => { | ||
// Redirect to setup page if not initialized | ||
if (!initialized && req.path !== '/admin/setup') { | ||
res.redirect('/admin/setup'); | ||
return; | ||
} | ||
|
||
// redirect to admin page if initialized | ||
if (initialized && req.path === '/admin/setup') { | ||
res.redirect('/admin'); | ||
return; | ||
} | ||
|
||
next(); | ||
}) | ||
.catch(() => { | ||
next(); | ||
}); | ||
}; | ||
} | ||
|
||
@Module({ | ||
imports: [AuthModule, UserModule, ServerConfigModule], | ||
providers: [SetupMiddleware], | ||
controllers: [CustomSetupController], | ||
}) | ||
export class SelfhostModule implements OnModuleInit { | ||
constructor( | ||
private readonly config: Config, | ||
private readonly adapterHost: HttpAdapterHost, | ||
private readonly check: SetupMiddleware | ||
) {} | ||
|
||
onModuleInit() { | ||
const staticPath = join(this.config.projectRoot, 'static'); | ||
const app = this.adapterHost.httpAdapter.getInstance<Application>(); | ||
const basePath = this.config.server.path; | ||
|
||
app.get(basePath + '/admin/index.html', (_req, res) => { | ||
res.redirect(basePath + '/admin'); | ||
}); | ||
app.use( | ||
basePath + '/admin', | ||
serveStatic(join(staticPath, 'admin'), { | ||
redirect: false, | ||
index: false, | ||
}) | ||
); | ||
|
||
app.get( | ||
[basePath + '/admin', basePath + '/admin/*'], | ||
this.check.use, | ||
(_req, res) => { | ||
res.sendFile(join(staticPath, 'admin', 'index.html')); | ||
} | ||
); | ||
|
||
app.get(basePath + '/index.html', (_req, res) => { | ||
res.redirect(basePath); | ||
}); | ||
app.use( | ||
basePath, | ||
serveStatic(staticPath, { | ||
redirect: false, | ||
index: false, | ||
}) | ||
); | ||
app.get('*', this.check.use, (_req, res) => { | ||
res.sendFile(join(staticPath, 'index.html')); | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.