Skip to content

Commit

Permalink
feat(system-server): add quota to limit app creating
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 6, 2021
1 parent f55f7ec commit 77e98db
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
18 changes: 18 additions & 0 deletions packages/system-server/src/api/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

import { Constants } from "../constants"
import { DatabaseAgent } from "../lib/db-agent"
import * as assert from 'assert'

/**
* Get an account by account_id
*/
export async function getAccountByAppid(uid: string) {
assert.ok(uid, 'empty uid got')

const db = DatabaseAgent.sys_db
const ret = await db.collection(Constants.cn.accounts)
.where({ _id: uid })
.getOne()

return ret.data
}
8 changes: 6 additions & 2 deletions packages/system-server/src/api/application.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-28 22:00:45
* @LastEditTime: 2021-09-03 18:32:35
* @LastEditTime: 2021-09-06 22:04:37
* @Description: Application APIs
*/

Expand Down Expand Up @@ -116,7 +116,11 @@ export async function getApplicationDbAccessor(app: ApplicationStruct) {
return accessor
}


/**
* Get the db uri of an application
* @param app
* @returns
*/
export function getApplicationDbUri(app: ApplicationStruct) {
const { db_name, db_password, db_user } = app.config

Expand Down
15 changes: 15 additions & 0 deletions packages/system-server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,19 @@ export default class Config {
static get APP_SERVICE_IMAGE(): string {
return process.env.APP_SERVICE_IMAGE
}

/**
* The application count that an account can create by default
*/
static get ACCOUNT_DEFAULT_APP_QUOTA(): number {
return (process.env.ACCOUNT_DEFAULT_APP_QUOTA ?? 2) as number
}

/**
* The deploy url used to access the app service
* example: http://www.example.com:8080 , don't end with '/'
*/
static get APP_DEPLOY_URL(): string {
return process.env.APP_DEPLOY_URL ?? ''
}
}
7 changes: 7 additions & 0 deletions packages/system-server/src/constants/error-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

export class ErrorCodes {
static MEET_APPLICATION_QUOTA_LIMIT = {
code: 'MEET_APPLICATION_QUOTA_LIMIT',
error: 'you have not more quota to create application'
}
}
6 changes: 5 additions & 1 deletion packages/system-server/src/router/account/signup.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-08-31 14:37:26
* @LastEditTime: 2021-09-06 22:19:49
* @Description:
*/

import { Request, Response } from 'express'
import { hashPassword } from '../../utils/hash'
import { DatabaseAgent } from '../../lib/db-agent'
import { Constants } from '../../constants'
import Config from '../../config'


/**
Expand All @@ -33,6 +34,9 @@ export async function handleSignUp(req: Request, res: Response) {
const r = await db.collection(Constants.cn.accounts)
.add({
username,
quota: {
app_count: Config.ACCOUNT_DEFAULT_APP_QUOTA
},
password: hashPassword(password),
created_at: Date.now(),
updated_at: Date.now()
Expand Down
22 changes: 18 additions & 4 deletions packages/system-server/src/router/application/create.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-31 15:00:04
* @LastEditTime: 2021-09-03 19:07:51
* @LastEditTime: 2021-09-06 22:22:21
* @Description:
*/

import * as assert from 'assert'
import { Request, Response } from 'express'
import { ApplicationStruct, createApplicationDb, generateAppid } from '../../api/application'
import { getAccountByAppid } from '../../api/account'
import { ApplicationStruct, createApplicationDb, generateAppid, getMyApplications } from '../../api/application'
import { Constants } from '../../constants'
import { ErrorCodes } from '../../constants/error-code'
import { DatabaseAgent } from '../../lib/db-agent'
import { logger } from '../../lib/logger'
import { generatePassword } from '../../utils/rand'
Expand All @@ -20,11 +23,20 @@ export async function handleCreateApplication(req: Request, res: Response) {
if (!uid)
return res.status(401).send()

const app_name = req.body?.name ?? 'default'
const db = DatabaseAgent.sys_db

const appid = generateAppid()
// check the application quota in account
const account = await getAccountByAppid(uid)
assert.ok(account, 'empty account got')
const app_quota = account.quota?.app_count ?? 0
const my_apps = await getMyApplications(uid)
if (my_apps.length >= app_quota) {
return res.send(ErrorCodes.MEET_APPLICATION_QUOTA_LIMIT)
}

// build the application config
const app_name = req.body?.name ?? 'default'
const appid = generateAppid()
const _salt = generatePassword(6, true, false)
const db_name = `app_${appid}_${_salt}`
const db_user = db_name
Expand All @@ -47,13 +59,15 @@ export async function handleCreateApplication(req: Request, res: Response) {
updated_at: now
}

// save it
const ret = await db.collection(Constants.cn.applications)
.add(data)

if (!ret.id) {
return res.status(400).send('failed to create application')
}

// create app db
const result = await createApplicationDb(data)
logger.debug(`create application db ${db_name}`, result)

Expand Down

0 comments on commit 77e98db

Please sign in to comment.