Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/tiny-pro-backend-dev-guideline.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ PAGINATION_LIMIT = 10
REFRESH_TOKEN_TTL = 604800000
# 至多有多少个设备可以同时在线
DEVICE_LIMIT=1
# 是否启用演示模式
PREVIEW_MODE=true
```

### 开发前检查清单
Expand Down
2 changes: 2 additions & 0 deletions template/nestJs/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ MOCK_REGEX = '/mock'
REFRESH_TOKEN_TTL = 604800000
# 至多有多少个设备可以同时在线
DEVICE_LIMIT=1
# 是否启用演示模式
PREVIEW_MODE=true
11 changes: 9 additions & 2 deletions template/nestJs/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { HealthCheckController } from './health-check.controller';
import { ApplicationModule } from './application/application.module';
import { ApplicationService } from './application/application.service';
import { applicationData } from './application/init/data';
import { CONFIG_SCHEMA } from './config-schema';
import { CONFIG_SCHEMA, Configure } from './config-schema';

@Module({
imports: [
Expand Down Expand Up @@ -89,14 +89,21 @@ export class AppModule implements OnModuleInit {
private menu: MenuService,
private lang: I18LangService,
private i18: I18Service,
private application: ApplicationService
private application: ApplicationService,
private cfg: ConfigService<Configure>
) {}
async onModuleInit() {
const ROOT = __dirname;
const data = join(ROOT, 'data');
if (!existsSync(data)) {
mkdirSync(data);
}
const IS_PREVIEW_MOD = this.cfg.get('PREVIEW_MODE');
if (IS_PREVIEW_MOD) {
Logger.warn('You are currently in demonstration mode. All additions, deletions, and modifications request will be rejected');
Logger.warn('If you want to disable the demo mode, please set the `PREVIEW_MODE` environment variable to `false`')
Logger.warn('Alternatively, you can create an `.env` file and set `PREVIEV_MODE` to `false`')
}
const LOCK_FILE = join(data, 'lock');
if (existsSync(LOCK_FILE)) {
Logger.warn(
Expand Down
4 changes: 3 additions & 1 deletion template/nestJs/src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type Configure = {
MOCK_REGEX:string;
REFRESH_TOKEN_TTL:number;
DEVICE_LIMIT:number;
PREVIEW_MODE: boolean;
}

export const CONFIG_SCHEMA = Joi.object<Configure>({
Expand All @@ -39,5 +40,6 @@ export const CONFIG_SCHEMA = Joi.object<Configure>({
GLOBAL_PREFIX: Joi.string(),
MOCK_REGEX: Joi.string(),
REFRESH_TOKEN_TTL: Joi.number(),
DEVICE_LIMIT: Joi.number()
DEVICE_LIMIT: Joi.number(),
PREVIEW_MODE: Joi.bool().default(true)
})
11 changes: 10 additions & 1 deletion template/nestJs/src/public/reject.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ import {
import { Reflector } from '@nestjs/core';
import { I18nTranslations } from '../.generate/i18n.generated';
import { I18nContext } from 'nestjs-i18n';
import { ConfigService } from '@nestjs/config';
import { Configure } from '../config-schema';

@Injectable()
export class RejectRequestGuard implements CanActivate {
constructor(private readonly reflector: Reflector) {}
constructor(
private readonly reflector: Reflector,
private readonly cfg: ConfigService<Configure>
) {
}
async canActivate(ctx: ExecutionContext): Promise<boolean> {
if (!this.cfg.get('PREVIEW_MODE')) {
return true;
}
const i18n = I18nContext.current<I18nTranslations>();
const rejectRequest = this.reflector.getAllAndOverride('reject', [
ctx.getHandler(),
Expand Down
Loading