Skip to content

Commit

Permalink
feat(server): add enviroments apis (#605)
Browse files Browse the repository at this point in the history
* feat(server): add enviroments apis

* chore: fix changelog links
  • Loading branch information
maslow authored Jan 7, 2023
1 parent f429c2a commit acf5d34
Show file tree
Hide file tree
Showing 6 changed files with 875 additions and 698 deletions.
1,390 changes: 695 additions & 695 deletions CHANGELOG.md

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion server/src/application/application.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { InstanceService } from '../instance/instance.service'
import { JwtService } from '@nestjs/jwt'
import { FunctionService } from '../function/function.service'
import { StorageService } from '../storage/storage.service'
import { EnvironmentVariableService } from './environment.service'
import { EnvironmentVariableController } from './environment.controller'

@Module({
imports: [CoreModule],
controllers: [ApplicationController],
controllers: [ApplicationController, EnvironmentVariableController],
providers: [
ApplicationService,
PrismaService,
Expand All @@ -20,6 +22,7 @@ import { StorageService } from '../storage/storage.service'
JwtService,
FunctionService,
StorageService,
EnvironmentVariableService,
],
exports: [ApplicationService],
})
Expand Down
23 changes: 21 additions & 2 deletions server/src/application/application.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class ApplicationService {
name: APPLICATION_SECRET_KEY,
value: GenerateAlphaNumericPassword(64),
}
const appid = this.generateAppID(ServerConfig.APPID_LENGTH)
const appid = await this.tryGenerateUniqueAppid()

const data: Prisma.ApplicationCreateInput = {
name: dto.name,
Expand Down Expand Up @@ -145,7 +145,7 @@ export class ApplicationService {
}
}

generateAppID(len: number) {
private generateAppID(len: number) {
len = len || 6

// ensure prefixed with letter
Expand All @@ -155,4 +155,23 @@ export class ApplicationService {
const nano = nanoid.customAlphabet(alphanumeric, len - 1)
return prefix + nano()
}

/**
* Generate unique application id
* @returns
*/
async tryGenerateUniqueAppid() {
for (let i = 0; i < 10; i++) {
const appid = this.generateAppID(ServerConfig.APPID_LENGTH)
const existed = await this.prisma.application.findUnique({
where: { appid },
select: { appid: true },
})
if (!existed) {
return appid
}
}

throw new Error('Generate appid failed')
}
}
15 changes: 15 additions & 0 deletions server/src/application/dto/create-env.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsNotEmpty, IsString, Length } from 'class-validator'

export class CreateEnvironmentDto {
@ApiProperty()
@IsNotEmpty()
@IsString()
@Length(1, 64)
name: string

@ApiProperty()
@Length(0, 4096)
@IsString()
value: string
}
74 changes: 74 additions & 0 deletions server/src/application/environment.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
Body,
Controller,
Delete,
Get,
Logger,
Param,
Post,
UseGuards,
} from '@nestjs/common'
import {
ApiBearerAuth,
ApiOperation,
ApiResponse,
ApiTags,
} from '@nestjs/swagger'
import { ApplicationAuthGuard } from 'src/auth/application.auth.guard'
import { JwtAuthGuard } from 'src/auth/jwt.auth.guard'
import { ResponseUtil } from 'src/utils/response'
import { EnvironmentVariableService } from './environment.service'
import { CreateEnvironmentDto } from './dto/create-env.dto'

@ApiTags('Application')
@ApiBearerAuth('Authorization')
@Controller('apps/:appid/environments')
export class EnvironmentVariableController {
private readonly logger = new Logger(EnvironmentVariableController.name)

constructor(private readonly confService: EnvironmentVariableService) {}

/**
* Set a environment variable
* @param appid
* @param dto
* @returns
*/
@ApiResponse({ type: ResponseUtil })
@ApiOperation({ summary: 'Set a environment variable (create/update)' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Post()
async add(@Param('appid') appid: string, @Body() dto: CreateEnvironmentDto) {
const res = await this.confService.set(appid, dto)
return ResponseUtil.ok(res)
}

/**
* Get environment variables
* @param appid
* @returns
*/
@ApiResponse({ type: ResponseUtil })
@ApiOperation({ summary: 'Get environment variables' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Get()
async get(@Param('appid') appid: string) {
const res = await this.confService.find(appid)
return ResponseUtil.ok(res)
}

/**
* Delete an environment variable by name
* @param appid
* @param name
* @returns
*/
@ApiResponse({ type: ResponseUtil })
@ApiOperation({ summary: 'Delete an environment variable by name' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Delete(':name')
async delete(@Param('appid') appid: string, @Param('name') name: string) {
const res = await this.confService.delete(appid, name)
return ResponseUtil.ok(res)
}
}
66 changes: 66 additions & 0 deletions server/src/application/environment.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Injectable, Logger } from '@nestjs/common'
import { PrismaService } from 'src/prisma.service'
import { CreateEnvironmentDto } from './dto/create-env.dto'

@Injectable()
export class EnvironmentVariableService {
private readonly logger = new Logger(EnvironmentVariableService.name)

constructor(private readonly prisma: PrismaService) {}

/**
* if exists, update, else create
* @param appid
* @param dto
*/
async set(appid: string, dto: CreateEnvironmentDto) {
const origin = await this.find(appid)
// check if exists
const exists = origin.find((item) => item.name === dto.name)
if (exists) {
exists.value = dto.value
} else {
origin.push(dto)
}

const res = await this.prisma.applicationConfiguration.update({
where: { appid },
data: {
environments: {
set: origin,
},
},
})

return res.environments
}

async find(appid: string) {
const res = await this.prisma.applicationConfiguration.findUnique({
where: {
appid,
},
})

return res.environments
}

async delete(appid: string, name: string) {
const res = await this.prisma.applicationConfiguration.update({
where: {
appid,
},
data: {
environments: {
deleteMany: {
where: {
name,
},
},
},
},
})

return res
}
}

0 comments on commit acf5d34

Please sign in to comment.