Skip to content

Commit

Permalink
feat(sys-server): add remove app service api;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 8, 2021
1 parent d845356 commit 6942fa5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
20 changes: 20 additions & 0 deletions packages/system-server/src/lib/service-driver/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ export class DockerContainerServiceDriver {
return container.id
}

/**
* Stop application service
* @param app
*/
async stopService(app: ApplicationStruct) {
const info = await this.info(app)
if (!info) {
return
}

const container = this.getContainer(app)
if (info.State.Running || info.State.Restarting) {
await container.stop()
}

logger.debug(`stop container ${container.id} of app ${app.appid}`)

return container.id
}

/**
* Remove application service
* @param app
Expand Down
17 changes: 11 additions & 6 deletions packages/system-server/src/router/application/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-29 11:35:11
* @LastEditTime: 2021-09-08 22:57:59
* @LastEditTime: 2021-09-09 00:43:39
* @Description:
*/

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

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

/**
* Start an application by appid
* Start an application service by appid
*/
ApplicationRouter.post('/:appid/start', handleStartApplicationService)
ApplicationRouter.post('/:appid/service/start', handleStartApplicationService)

/**
* Stop an application by appid
* Stop an application service by appid
*/
ApplicationRouter.post('/:appid/stop', handleStopApplicationService)
ApplicationRouter.post('/:appid/service/stop', handleStopApplicationService)

/**
* Remove an application service by appid
*/
ApplicationRouter.post('/:appid/service/remove', handleRemoveApplicationService)

/**
* Remove an application by appid
Expand Down
38 changes: 36 additions & 2 deletions packages/system-server/src/router/application/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-31 15:00:04
* @LastEditTime: 2021-09-03 19:04:17
* @LastEditTime: 2021-09-08 23:52:11
* @Description:
*/

Expand Down Expand Up @@ -67,12 +67,46 @@ export async function handleStopApplicationService(req: Request, res: Response)
}

const dockerService = new DockerContainerServiceDriver()
const container_id = await dockerService.removeService(app)
const container_id = await dockerService.stopService(app)

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

return res.send({
data: {
service_id: container_id,
appid: app.appid
}
})
}


/**
* The handler of removing application
*/
export async function handleRemoveApplicationService(req: Request, res: Response) {
const uid = req['auth']?.uid
const db = DatabaseAgent.sys_db
const appid = req.params.appid
const app = await getApplicationByAppid(appid)

if (!app)
return res.status(422).send('app not found')

// check permission
const code = await checkPermission(uid, APPLICATION_UPDATE.name, app)
if (code) {
return res.status(code).send()
}

const dockerService = new DockerContainerServiceDriver()
const container_id = await dockerService.removeService(app)

await db.collection(Constants.cn.applications)
.where({ appid: app.appid })
.update({ status: 'removed' })

return res.send({
data: {
service_id: container_id,
Expand Down

0 comments on commit 6942fa5

Please sign in to comment.