Skip to content

Commit 77e98db

Browse files
committed
feat(system-server): add quota to limit app creating
1 parent f55f7ec commit 77e98db

File tree

6 files changed

+69
-7
lines changed

6 files changed

+69
-7
lines changed
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
import { Constants } from "../constants"
3+
import { DatabaseAgent } from "../lib/db-agent"
4+
import * as assert from 'assert'
5+
6+
/**
7+
* Get an account by account_id
8+
*/
9+
export async function getAccountByAppid(uid: string) {
10+
assert.ok(uid, 'empty uid got')
11+
12+
const db = DatabaseAgent.sys_db
13+
const ret = await db.collection(Constants.cn.accounts)
14+
.where({ _id: uid })
15+
.getOne()
16+
17+
return ret.data
18+
}

packages/system-server/src/api/application.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-08-28 22:00:45
4-
* @LastEditTime: 2021-09-03 18:32:35
4+
* @LastEditTime: 2021-09-06 22:04:37
55
* @Description: Application APIs
66
*/
77

@@ -116,7 +116,11 @@ export async function getApplicationDbAccessor(app: ApplicationStruct) {
116116
return accessor
117117
}
118118

119-
119+
/**
120+
* Get the db uri of an application
121+
* @param app
122+
* @returns
123+
*/
120124
export function getApplicationDbUri(app: ApplicationStruct) {
121125
const { db_name, db_password, db_user } = app.config
122126

packages/system-server/src/config.ts

+15
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,19 @@ export default class Config {
7878
static get APP_SERVICE_IMAGE(): string {
7979
return process.env.APP_SERVICE_IMAGE
8080
}
81+
82+
/**
83+
* The application count that an account can create by default
84+
*/
85+
static get ACCOUNT_DEFAULT_APP_QUOTA(): number {
86+
return (process.env.ACCOUNT_DEFAULT_APP_QUOTA ?? 2) as number
87+
}
88+
89+
/**
90+
* The deploy url used to access the app service
91+
* example: http://www.example.com:8080 , don't end with '/'
92+
*/
93+
static get APP_DEPLOY_URL(): string {
94+
return process.env.APP_DEPLOY_URL ?? ''
95+
}
8196
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
export class ErrorCodes {
3+
static MEET_APPLICATION_QUOTA_LIMIT = {
4+
code: 'MEET_APPLICATION_QUOTA_LIMIT',
5+
error: 'you have not more quota to create application'
6+
}
7+
}

packages/system-server/src/router/account/signup.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-07-30 10:30:29
4-
* @LastEditTime: 2021-08-31 14:37:26
4+
* @LastEditTime: 2021-09-06 22:19:49
55
* @Description:
66
*/
77

88
import { Request, Response } from 'express'
99
import { hashPassword } from '../../utils/hash'
1010
import { DatabaseAgent } from '../../lib/db-agent'
1111
import { Constants } from '../../constants'
12+
import Config from '../../config'
1213

1314

1415
/**
@@ -33,6 +34,9 @@ export async function handleSignUp(req: Request, res: Response) {
3334
const r = await db.collection(Constants.cn.accounts)
3435
.add({
3536
username,
37+
quota: {
38+
app_count: Config.ACCOUNT_DEFAULT_APP_QUOTA
39+
},
3640
password: hashPassword(password),
3741
created_at: Date.now(),
3842
updated_at: Date.now()

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

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-08-31 15:00:04
4-
* @LastEditTime: 2021-09-03 19:07:51
4+
* @LastEditTime: 2021-09-06 22:22:21
55
* @Description:
66
*/
77

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

23-
const app_name = req.body?.name ?? 'default'
2426
const db = DatabaseAgent.sys_db
2527

26-
const appid = generateAppid()
28+
// check the application quota in account
29+
const account = await getAccountByAppid(uid)
30+
assert.ok(account, 'empty account got')
31+
const app_quota = account.quota?.app_count ?? 0
32+
const my_apps = await getMyApplications(uid)
33+
if (my_apps.length >= app_quota) {
34+
return res.send(ErrorCodes.MEET_APPLICATION_QUOTA_LIMIT)
35+
}
2736

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

62+
// save it
5063
const ret = await db.collection(Constants.cn.applications)
5164
.add(data)
5265

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

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

0 commit comments

Comments
 (0)