Skip to content

Commit

Permalink
fix(server): fix function websocket error; add tags for function (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow authored Jan 9, 2023
1 parent 7d9cd82 commit 6abee36
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions server/src/function/dto/create-function.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import { HttpMethod } from '@prisma/client'
import {
IsArray,
IsBoolean,
IsIn,
IsNotEmpty,
Expand Down Expand Up @@ -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
}
}
8 changes: 8 additions & 0 deletions server/src/function/dto/update-function.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import { HttpMethod } from '@prisma/client'
import {
IsArray,
IsBoolean,
IsIn,
IsNotEmpty,
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions server/src/function/function.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
11 changes: 9 additions & 2 deletions server/src/function/function.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 } },
Expand All @@ -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 } },
Expand Down

0 comments on commit 6abee36

Please sign in to comment.