Skip to content

Commit 4220e70

Browse files
authored
feat: add reject request flag (#146)
* feat(nest.js): add preview mode flag * style: refine the preview mode warning tip * fix: use relative path
1 parent 39d3d70 commit 4220e70

File tree

6 files changed

+25
-6
lines changed

6 files changed

+25
-6
lines changed

template/nestJs/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ MOCK_REGEX = '/mock'
1717
REFRESH_TOKEN_TTL = 604800000
1818
# 至多有多少个设备可以同时在线
1919
DEVICE_LIMIT=1
20+
# 是否启用演示模式
21+
PREVIEW_MODE=true

template/nestJs/dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ ENV DATABASE_NAME=""
2424

2525
EXPOSE 3000
2626
VOLUME [ "/builder/dist/data" ]
27-
CMD ["sh", "endpoint.sh"]
27+
CMD ["sh", "./endpoint.sh"]
2828

template/nestJs/endpoint.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/bin/sh
2-
32
wait4x mysql "${DATABASE_USERNAME}:${DATABASE_PASSWORD}@tcp(mysql:3306)/${DATABASE_NAME}" --timeout=60s --interval=2s -- node ./migrate.js && node ./dist/main.js

template/nestJs/src/app.module.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { HealthCheckController } from './health-check.controller';
3737
import { ApplicationModule } from './application/application.module';
3838
import { ApplicationService } from './application/application.service';
3939
import { applicationData } from './application/init/data';
40-
import { CONFIG_SCHEMA } from './config-schema';
40+
import { CONFIG_SCHEMA, Configure } from './config-schema';
4141

4242
@Module({
4343
imports: [
@@ -89,14 +89,21 @@ export class AppModule implements OnModuleInit {
8989
private menu: MenuService,
9090
private lang: I18LangService,
9191
private i18: I18Service,
92-
private application: ApplicationService
92+
private application: ApplicationService,
93+
private cfg: ConfigService<Configure>
9394
) {}
9495
async onModuleInit() {
9596
const ROOT = __dirname;
9697
const data = join(ROOT, 'data');
9798
if (!existsSync(data)) {
9899
mkdirSync(data);
99100
}
101+
const IS_PREVIEW_MOD = this.cfg.get('PREVIEW_MODE');
102+
if (IS_PREVIEW_MOD) {
103+
Logger.warn('You are currently in demonstration mode. All additions, deletions, and modifications request will be rejected');
104+
Logger.warn('If you want to disable the demo mode, please set the `PREVIEW_MODE` environment variable to `false`')
105+
Logger.warn('Alternatively, you can create an `.env` file and set `PREVIEV_MODE` to `false`')
106+
}
100107
const LOCK_FILE = join(data, 'lock');
101108
if (existsSync(LOCK_FILE)) {
102109
Logger.warn(

template/nestJs/src/config-schema.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type Configure = {
1919
MOCK_REGEX:string;
2020
REFRESH_TOKEN_TTL:number;
2121
DEVICE_LIMIT:number;
22+
PREVIEW_MODE: boolean;
2223
}
2324

2425
export const CONFIG_SCHEMA = Joi.object<Configure>({
@@ -39,5 +40,6 @@ export const CONFIG_SCHEMA = Joi.object<Configure>({
3940
GLOBAL_PREFIX: Joi.string(),
4041
MOCK_REGEX: Joi.string(),
4142
REFRESH_TOKEN_TTL: Joi.number(),
42-
DEVICE_LIMIT: Joi.number()
43+
DEVICE_LIMIT: Joi.number(),
44+
PREVIEW_MODE: Joi.bool().default(true)
4345
})

template/nestJs/src/public/reject.guard.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@ import {
88
import { Reflector } from '@nestjs/core';
99
import { I18nTranslations } from '../.generate/i18n.generated';
1010
import { I18nContext } from 'nestjs-i18n';
11+
import { ConfigService } from '@nestjs/config';
12+
import { Configure } from '../config-schema';
1113

1214
@Injectable()
1315
export class RejectRequestGuard implements CanActivate {
14-
constructor(private readonly reflector: Reflector) {}
16+
constructor(
17+
private readonly reflector: Reflector,
18+
private readonly cfg: ConfigService<Configure>
19+
) {
20+
}
1521
async canActivate(ctx: ExecutionContext): Promise<boolean> {
22+
if (!this.cfg.get('PREVIEW_MODE')) {
23+
return true;
24+
}
1625
const i18n = I18nContext.current<I18nTranslations>();
1726
const rejectRequest = this.reflector.getAllAndOverride('reject', [
1827
ctx.getHandler(),

0 commit comments

Comments
 (0)