Skip to content

Commit

Permalink
feat(system-server): add app update apis;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 3, 2021
1 parent 0a5cd99 commit 5c674f3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 21 deletions.
61 changes: 40 additions & 21 deletions packages/system-server/src/api/application.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-28 22:00:45
* @LastEditTime: 2021-09-02 16:12:47
* @LastEditTime: 2021-09-03 18:32:35
* @Description: Application APIs
*/

import { Constants } from "../constants"
import { DatabaseAgent } from "../lib/db-agent"
import * as assert from 'assert'
import { MongoAccessor } from "less-api/dist"
import * as crypto from 'crypto'
import { generateUUID } from "../utils/rand"
import { MongoClient } from 'mongodb'
import Config from "../config"
import * as mongodb_uri from 'mongodb-uri'

/**
* The application structure in db
Expand All @@ -19,12 +22,11 @@ export interface ApplicationStruct {
name: string
created_by: string
appid: string
app_secret: string
status: 'created' | 'starting' | 'running' | 'stopped'
status: 'created' | 'running' | 'stopped'
config: {
db_name: string
db_uri: string
db_max_pool_size: number
db_user: string
db_password: string
server_secret_salt: string
file_system_driver?: string
file_system_enable_unauthorized_upload?: string
Expand All @@ -50,10 +52,6 @@ export async function getApplicationByAppid(appid: string) {
const db = DatabaseAgent.sys_db
const ret = await db.collection(Constants.cn.applications)
.where({ appid: appid })
.field({
app_secret: false,
config: false
})
.getOne<ApplicationStruct>()

assert.ok(ret.ok, `getMyApplicationById() got error: ${appid}`)
Expand Down Expand Up @@ -106,30 +104,51 @@ export async function getMyJoinedApplications(account_id: string) {
* @returns
*/
export async function getApplicationDbAccessor(app: ApplicationStruct) {
const db_name = app.config.db_name
const db_uri = app.config.db_uri
const { db_name, db_password, db_user } = app.config
assert.ok(db_name, 'empty db_name got')
assert.ok(db_password, 'empty db_password got')
assert.ok(db_user, 'empty db_user got')

assert.ok(db_name)
assert.ok(db_uri)
const db_uri = getApplicationDbUri(app)
const accessor = new MongoAccessor(db_name, db_uri, { directConnection: true })
await accessor.init()

return accessor
}


export function getApplicationDbUri(app: ApplicationStruct) {
const { db_name, db_password, db_user } = app.config

// build app db connection uri from config
const parsed = mongodb_uri.parse(Config.app_db_uri)
parsed.database = db_name
parsed.username = db_user
parsed.password = db_password
parsed.options['authSource'] = db_name

return mongodb_uri.format(parsed)
}

/**
* Generate application secret string
* @returns
* Create
* @param app
*/
export async function generateApplicationSecret() {
const buf = crypto.randomBytes(64)
return buf.toString('base64')
export async function createApplicationDb(app: ApplicationStruct) {
const client = new MongoClient(Config.app_db_uri)
await client.connect()
const { db_name, db_user, db_password } = app.config
const db = client.db(db_name)
const result = await db.addUser(db_user, db_password, { roles: [{ role: "readWrite", db: db_name }] })

await client.close()
return result
}

/**
* Generate application id
* @returns
*/
export function generateApplicationId() {
return crypto.randomUUID()
export function generateAppid() {
return generateUUID()
}
45 changes: 45 additions & 0 deletions packages/system-server/src/router/application/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-31 15:00:04
* @LastEditTime: 2021-09-03 17:29:23
* @Description:
*/

import { Request, Response } from 'express'
import { getApplicationByAppid } from '../../api/application'
import { checkPermission } from '../../api/permission'
import { Constants } from '../../constants'
import { DatabaseAgent } from '../../lib/db-agent'
import { permissions } from '../../constants/permissions'

const { APPLICATION_UPDATE } = permissions
/**
* The handler of updating application
*/
export async function handleUpdateApplication(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()
}

// check params
const body = req.body
if (!body.name) return res.status(422).send('name cannot be empty')

const ret = await db.collection(Constants.cn.applications)
.where({ appid: app.appid })
.update({ name: body.name })

return res.send({
data: ret
})
}

0 comments on commit 5c674f3

Please sign in to comment.