Skip to content

Commit 6942fa5

Browse files
committed
feat(sys-server): add remove app service api;
1 parent d845356 commit 6942fa5

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

packages/system-server/src/lib/service-driver/container.ts

+20
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ export class DockerContainerServiceDriver {
6868
return container.id
6969
}
7070

71+
/**
72+
* Stop application service
73+
* @param app
74+
*/
75+
async stopService(app: ApplicationStruct) {
76+
const info = await this.info(app)
77+
if (!info) {
78+
return
79+
}
80+
81+
const container = this.getContainer(app)
82+
if (info.State.Running || info.State.Restarting) {
83+
await container.stop()
84+
}
85+
86+
logger.debug(`stop container ${container.id} of app ${app.appid}`)
87+
88+
return container.id
89+
}
90+
7191
/**
7292
* Remove application service
7393
* @param app

packages/system-server/src/router/application/index.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-08-29 11:35:11
4-
* @LastEditTime: 2021-09-08 22:57:59
4+
* @LastEditTime: 2021-09-09 00:43:39
55
* @Description:
66
*/
77

@@ -10,7 +10,7 @@ import { handleNotImplemented } from '../common'
1010
import { handleGetCollaborators, handleGetRoles, handleInviteCollaborator, handleRemoveCollaborator, handleSearchCollaborator } from './collaborator'
1111
import { handleCreateApplication } from './create'
1212
import { handleGetApplicationByAppid, handleMyApplications } from './get'
13-
import { handleStopApplicationService, handleStartApplicationService } from './service'
13+
import { handleStopApplicationService, handleStartApplicationService, handleRemoveApplicationService } from './service'
1414
import { handleUpdateApplication } from './update'
1515

1616
export const ApplicationRouter = Router()
@@ -36,14 +36,19 @@ ApplicationRouter.post('/create', handleCreateApplication)
3636
ApplicationRouter.post('/:appid', handleUpdateApplication)
3737

3838
/**
39-
* Start an application by appid
39+
* Start an application service by appid
4040
*/
41-
ApplicationRouter.post('/:appid/start', handleStartApplicationService)
41+
ApplicationRouter.post('/:appid/service/start', handleStartApplicationService)
4242

4343
/**
44-
* Stop an application by appid
44+
* Stop an application service by appid
4545
*/
46-
ApplicationRouter.post('/:appid/stop', handleStopApplicationService)
46+
ApplicationRouter.post('/:appid/service/stop', handleStopApplicationService)
47+
48+
/**
49+
* Remove an application service by appid
50+
*/
51+
ApplicationRouter.post('/:appid/service/remove', handleRemoveApplicationService)
4752

4853
/**
4954
* Remove an application by appid

packages/system-server/src/router/application/service.ts

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-08-31 15:00:04
4-
* @LastEditTime: 2021-09-03 19:04:17
4+
* @LastEditTime: 2021-09-08 23:52:11
55
* @Description:
66
*/
77

@@ -67,12 +67,46 @@ export async function handleStopApplicationService(req: Request, res: Response)
6767
}
6868

6969
const dockerService = new DockerContainerServiceDriver()
70-
const container_id = await dockerService.removeService(app)
70+
const container_id = await dockerService.stopService(app)
7171

7272
await db.collection(Constants.cn.applications)
7373
.where({ appid: app.appid })
7474
.update({ status: 'stopped' })
7575

76+
return res.send({
77+
data: {
78+
service_id: container_id,
79+
appid: app.appid
80+
}
81+
})
82+
}
83+
84+
85+
/**
86+
* The handler of removing application
87+
*/
88+
export async function handleRemoveApplicationService(req: Request, res: Response) {
89+
const uid = req['auth']?.uid
90+
const db = DatabaseAgent.sys_db
91+
const appid = req.params.appid
92+
const app = await getApplicationByAppid(appid)
93+
94+
if (!app)
95+
return res.status(422).send('app not found')
96+
97+
// check permission
98+
const code = await checkPermission(uid, APPLICATION_UPDATE.name, app)
99+
if (code) {
100+
return res.status(code).send()
101+
}
102+
103+
const dockerService = new DockerContainerServiceDriver()
104+
const container_id = await dockerService.removeService(app)
105+
106+
await db.collection(Constants.cn.applications)
107+
.where({ appid: app.appid })
108+
.update({ status: 'removed' })
109+
76110
return res.send({
77111
data: {
78112
service_id: container_id,

0 commit comments

Comments
 (0)