diff --git a/server/src/constants.ts b/server/src/constants.ts index f57d99bd68..4cf1a1c232 100644 --- a/server/src/constants.ts +++ b/server/src/constants.ts @@ -115,3 +115,5 @@ export const MB = 1024 * 1024 export const GB = 1024 * MB export const APPLICATION_SECRET_KEY = 'SERVER_SECRET' + +export const MAX_FUNCTION_COUNT = 1000 diff --git a/server/src/function/dto/create-function.dto.ts b/server/src/function/dto/create-function.dto.ts index 3616301b0f..08a396377b 100644 --- a/server/src/function/dto/create-function.dto.ts +++ b/server/src/function/dto/create-function.dto.ts @@ -1,6 +1,7 @@ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger' import { HttpMethod } from '@prisma/client' import { + IsArray, IsBoolean, IsIn, IsNotEmpty, @@ -36,7 +37,17 @@ export class CreateFunctionDto { @MaxLength(1024 * 512) code: string + @ApiPropertyOptional({ type: [String] }) + @IsString({ each: true }) + @IsArray() + @MaxLength(16, { each: true }) + @IsNotEmpty({ each: true }) + tags: string[] + validate() { + if (this.tags?.length >= 8) { + return 'tags length must less than 8' + } return null } } diff --git a/server/src/function/dto/update-function.dto.ts b/server/src/function/dto/update-function.dto.ts index d90e1267d9..ebb850bc1d 100644 --- a/server/src/function/dto/update-function.dto.ts +++ b/server/src/function/dto/update-function.dto.ts @@ -1,6 +1,7 @@ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger' import { HttpMethod } from '@prisma/client' import { + IsArray, IsBoolean, IsIn, IsNotEmpty, @@ -28,6 +29,13 @@ export class UpdateFunctionDto { @MaxLength(1024 * 512) code: string + @ApiPropertyOptional({ type: [String] }) + @IsString({ each: true }) + @IsArray() + @MaxLength(16, { each: true }) + @IsNotEmpty({ each: true }) + tags: string[] + validate() { return null } diff --git a/server/src/function/function.controller.ts b/server/src/function/function.controller.ts index 0354558ad9..a28897c58b 100644 --- a/server/src/function/function.controller.ts +++ b/server/src/function/function.controller.ts @@ -25,6 +25,7 @@ import { ApplicationAuthGuard } from '../auth/application.auth.guard' import { FunctionService } from './function.service' import { IRequest } from '../utils/types' import { CompileFunctionDto } from './dto/compile-function.dto.ts' +import { MAX_FUNCTION_COUNT } from 'src/constants' @ApiTags('Function') @ApiBearerAuth('Authorization') @@ -57,6 +58,12 @@ export class FunctionController { return ResponseUtil.error('function name is already existed') } + // check if meet the count limit + const count = await this.functionsService.count(appid) + if (count > MAX_FUNCTION_COUNT) { + return ResponseUtil.error(`function count limit is ${MAX_FUNCTION_COUNT}`) + } + const res = await this.functionsService.create(appid, req.user.id, dto) if (!res) { return ResponseUtil.error('create function error') diff --git a/server/src/function/function.service.ts b/server/src/function/function.service.ts index 226fe49e55..0ee74a89c1 100644 --- a/server/src/function/function.service.ts +++ b/server/src/function/function.service.ts @@ -32,9 +32,10 @@ export class FunctionService { version: 0, }, desc: dto.description, - websocket: false, + websocket: dto.websocket, createdBy: userid, methods: dto.methods, + tags: dto.tags || [], } const res = await this.prisma.cloudFunction.create({ data }) await this.publish(res) @@ -49,6 +50,11 @@ export class FunctionService { return res } + async count(appid: string) { + const res = await this.prisma.cloudFunction.count({ where: { appid } }) + return res + } + async findOne(appid: string, name: string) { const res = await this.prisma.cloudFunction.findUnique({ where: { appid_name: { appid, name } }, @@ -64,8 +70,9 @@ export class FunctionService { version: func.source.version + 1, }, desc: dto.description, - websocket: false, + websocket: dto.websocket, methods: dto.methods, + tags: dto.tags || [], } const res = await this.prisma.cloudFunction.update({ where: { appid_name: { appid: func.appid, name: func.name } },