Skip to content

Commit

Permalink
feat(server): impl add app api
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Nov 15, 2022
1 parent e707e5e commit 5877439
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 19 deletions.
5 changes: 5 additions & 0 deletions server/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"casdoor"
]
}
5 changes: 4 additions & 1 deletion server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { FunctionsModule } from './functions/functions.module'
import { ConfigModule } from '@nestjs/config'
import { HttpModule } from '@nestjs/axios'
import { CoreModule } from './core/core.module'
import { DemoModule } from './demo/demo.module';
import { TestService } from './test/test.service';

@Module({
imports: [
Expand All @@ -25,8 +27,9 @@ import { CoreModule } from './core/core.module'
CollectionsModule,
HttpModule,
CoreModule,
DemoModule,
],
controllers: [AppController],
providers: [AppService],
providers: [AppService, TestService],
})
export class AppModule {}
4 changes: 2 additions & 2 deletions server/src/apps/apps.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { AppsService } from './apps.service'
import { CreateAppDto } from './dto/create-app.dto'
import { UpdateAppDto } from './dto/update-app.dto'
import { ResponseStruct } from '../utils/response'
import { ResponseUtil } from '../utils/response'

@Controller('apps')
export class AppsController {
Expand All @@ -24,7 +24,7 @@ export class AppsController {
create(@Body() dto: CreateAppDto) {
const error = dto.validate()
if (error) {
return ResponseStruct.error(error)
return ResponseUtil.error(error)
}
return this.appsService.create(dto)
}
Expand Down
49 changes: 49 additions & 0 deletions server/src/apps/apps.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Test, TestingModule } from '@nestjs/testing'
import { CoreModule } from '../core/core.module'
import { AppsService } from './apps.service'
import { CreateAppDto } from './dto/create-app.dto'
import { ApplicationState } from './entities/app.entity'

describe('AppsService', () => {
let service: AppsService
Expand All @@ -18,3 +20,50 @@ describe('AppsService', () => {
expect(service).toBeDefined()
})
})

describe.only('AppService create app', () => {
const timeout = 60 * 1000
let service: AppsService
const name = 'testing-create-app'
async function cleanup() {
if (await service.k8sClient.existsNamespace(name)) {
await service.k8sClient.deleteNamespace(name)
}
// wait for namespace deleted
await new Promise((resolve) => setTimeout(resolve, 3000))
}

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [CoreModule],
providers: [AppsService],
}).compile()

service = module.get<AppsService>(AppsService)

await cleanup()
}, timeout)

jest.setTimeout(timeout)

it('should create app', async () => {
const dto = new CreateAppDto()
dto.name = name
dto.state = ApplicationState.ApplicationStateRunning
dto.region = 'default'
dto.bundleName = 'mini'
dto.runtimeName = 'node-laf'
try {
const res = await service.create(dto)
expect(res.error).toBeNull()
expect(res.data).toBe('create app success')
} catch (err) {
console.log(err.response)
throw err
}
})

afterAll(async () => {
// await cleanup()
}, 20000)
})
26 changes: 19 additions & 7 deletions server/src/apps/apps.service.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
import { Injectable } from '@nestjs/common'
import { ResponseUtil } from '../utils/response'
import { KubernetesService } from '../core/kubernetes.service'
import { CreateAppDto } from './dto/create-app.dto'
import { UpdateAppDto } from './dto/update-app.dto'
import { Application, ApplicationSpec } from './entities/app.entity'

@Injectable()
export class AppsService {
constructor(private k8sClient: KubernetesService) {}
constructor(public k8sClient: KubernetesService) {}

async create(dto: CreateAppDto) {
// create app namespace
await this.k8sClient.createNamespace(dto.name)

try {
await this.k8sClient.createNamespace(dto.name)
} catch (error) {
console.error(error)
return ResponseUtil.error('create app namespace error')
}

// create app resources
const app = new Application(dto.name)
const app = new Application()
app.metadata.name = dto.name
app.metadata.namespace = dto.name
app.spec = new ApplicationSpec({
appid: dto.name,
state: dto.state,
region: dto.region,
bundleName: dto.bundleName,
runtimeName: dto.runtimeName,
})
app.metadata.namespace = dto.name

console.log(app.toJSON())
const res = await this.k8sClient.objectApi.create(app.toJSON())
try {
await this.k8sClient.objectApi.create(app.toJSON())
} catch (error) {
console.error(error)
return ResponseUtil.error('create app resources error')
}

return res.body
return ResponseUtil.ok('create app success')
}

findAll() {
Expand Down
9 changes: 5 additions & 4 deletions server/src/apps/entities/app.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ export class Application implements KubernetesObject {
metadata?: V1ObjectMeta
spec: ApplicationSpec

constructor(name: string) {
this.metadata = {
name,
}
constructor() {
this.metadata = {}
}

toJSON() {
if (!this.metadata.name) {
throw new Error('name cannot be empty')
}
return {
apiVersion: this.apiVersion,
kind: this.kind,
Expand Down
2 changes: 1 addition & 1 deletion server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function bootstrap() {
.setVersion('1.0')
.build()
const document = SwaggerModule.createDocument(app, config)
SwaggerModule.setup('open-api', app, document)
SwaggerModule.setup('api', app, document)

await app.listen(3000)
}
Expand Down
2 changes: 2 additions & 0 deletions server/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


12 changes: 8 additions & 4 deletions server/src/utils/response.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
export class ResponseStruct {
export class ResponseUtil {
static ok(data: any) {
return new ResponseStruct(data, null)
return new ResponseUtil(data, null)
}

static error(error: string) {
return new ResponseStruct(null, error)
return new ResponseUtil(null, error)
}

static build(data: any, error: string) {
return new ResponseStruct(data, error)
return new ResponseUtil(data, error)
}

constructor(public data: any, public error: string) {}
valueOf() {
return this.toJSON()
}

toJSON() {
return {
error: this.error,
Expand Down

0 comments on commit 5877439

Please sign in to comment.