-
-
Notifications
You must be signed in to change notification settings - Fork 685
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sys-server): impl im/export apis;
- Loading branch information
Showing
11 changed files
with
408 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
> @Deprecated 此目录中的脚本原为初始化应用,现在多租户版中已废弃,待清理。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* @Author: Maslow<[email protected]> | ||
* @Date: 2021-07-30 10:30:29 | ||
* @LastEditTime: 2021-09-06 18:16:25 | ||
* @LastEditTime: 2021-09-09 17:59:42 | ||
* @Description: | ||
*/ | ||
|
||
|
@@ -14,38 +14,45 @@ import * as assert from 'assert' | |
import { logger } from "../lib/logger" | ||
import { ApplicationStruct, getApplicationDbAccessor } from "./application" | ||
|
||
const db = DatabaseAgent.sys_db | ||
export enum FunctionStatus { | ||
DISABLED = 0, | ||
ENABLED = 1 | ||
} | ||
|
||
/** | ||
* Extended function struct | ||
*/ | ||
export interface FunctionStruct extends CloudFunctionStruct { | ||
description: string | ||
tags: string[] | ||
label: string | ||
triggers: any[] | ||
status: FunctionStatus | ||
version: number | ||
hash: string | ||
created_at: number | ||
updated_at: number | ||
created_by: any | ||
appid: string | ||
debugParams: string | ||
} | ||
|
||
|
||
/** | ||
* Load function data by its name | ||
* @param func_name | ||
* @returns | ||
*/ | ||
export async function getFunctionByName(appid: string, func_name: string) { | ||
const db = DatabaseAgent.sys_db | ||
const r = await db.collection(Constants.cn.functions) | ||
.where({ name: func_name, appid }) | ||
.getOne<CloudFunctionStruct>() | ||
|
||
assert.ok(r.ok, `getCloudFunction() failed to get function [${func_name}]: ${r.error.toString()}`) | ||
return r.data | ||
} | ||
.getOne<FunctionStruct>() | ||
|
||
/** | ||
* Load function data by its id | ||
* @param func_name | ||
* @returns | ||
*/ | ||
export async function getFunctionById(appid: string, func_id: string) { | ||
const r = await db.collection(Constants.cn.functions) | ||
.where({ _id: func_id, appid }) | ||
.getOne() | ||
|
||
assert.ok(r.ok, `getCloudFunctionById() failed to get function [${func_id}]: ${r.error.toString()}`) | ||
return r.data | ||
} | ||
|
||
|
||
|
||
/** | ||
* Publish functions | ||
* Means that copying sys db functions to app db | ||
|
@@ -93,7 +100,7 @@ function compileFunction(func: any) { | |
/** | ||
* Deploy functions which pushed from remote environment | ||
*/ | ||
export async function deployFunctions(functions: CloudFunctionStruct[]) { | ||
export async function deployFunctions(functions: FunctionStruct[]) { | ||
assert.ok(functions) | ||
assert.ok(functions instanceof Array) | ||
|
||
|
@@ -121,7 +128,7 @@ export async function deployFunctions(functions: CloudFunctionStruct[]) { | |
* @see deployFunctions() | ||
* @returns | ||
*/ | ||
async function _deployOneFunction(func: CloudFunctionStruct, session: ClientSession) { | ||
async function _deployOneFunction(func: FunctionStruct, session: ClientSession) { | ||
|
||
await _processFunctionWithSameNameButNotId(func, session) | ||
|
||
|
@@ -155,7 +162,7 @@ async function _deployOneFunction(func: CloudFunctionStruct, session: ClientSess | |
* @param func the function to be processing | ||
* @param session the mongodb session for transaction operations | ||
*/ | ||
async function _processFunctionWithSameNameButNotId(func: CloudFunctionStruct, session: ClientSession) { | ||
async function _processFunctionWithSameNameButNotId(func: FunctionStruct, session: ClientSession) { | ||
const db = DatabaseAgent.sys_accessor.db | ||
|
||
// remove functions | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* @Author: Maslow<[email protected]> | ||
* @Date: 2021-07-30 10:30:29 | ||
* @LastEditTime: 2021-09-02 16:34:22 | ||
* @LastEditTime: 2021-09-09 17:59:47 | ||
* @Description: | ||
*/ | ||
|
||
|
@@ -11,6 +11,36 @@ import { DatabaseAgent } from "../lib/db-agent" | |
import { ClientSession, ObjectId } from 'mongodb' | ||
import { ApplicationStruct, getApplicationDbAccessor } from './application' | ||
|
||
export enum PolicyStatus { | ||
DISABLED = 0, | ||
ENABLED = 1 | ||
} | ||
export interface PolicyStruct { | ||
_id: string | ||
name: string | ||
description: string | ||
status: PolicyStatus | ||
rules: any | ||
injector: string | ||
hash: string | ||
created_at: number | ||
updated_at: number | ||
created_by: string | ||
appid: string | ||
} | ||
/** | ||
* Load policy by its name | ||
* @param func_name | ||
* @returns | ||
*/ | ||
export async function getPolicyByName(appid: string, policy_name: string) { | ||
const db = DatabaseAgent.sys_db | ||
const r = await db.collection(Constants.cn.policies) | ||
.where({ name: policy_name, appid }) | ||
.getOne<PolicyStruct>() | ||
|
||
return r.data | ||
} | ||
|
||
/** | ||
* Publish access policies | ||
|
@@ -45,7 +75,7 @@ export async function publishAccessPolicy(app: ApplicationStruct) { | |
/** | ||
* Deploy policies which pushed from remote environment | ||
*/ | ||
export async function deployPolicies(policies: any[]) { | ||
export async function deployPolicies(policies: PolicyStruct[]) { | ||
assert.ok(policies) | ||
assert.ok(policies instanceof Array) | ||
|
||
|
@@ -73,7 +103,7 @@ export async function deployPolicies(policies: any[]) { | |
* @private | ||
* @returns | ||
*/ | ||
async function _deployOnePolicy(policy: any, session: ClientSession) { | ||
async function _deployOnePolicy(policy: PolicyStruct, session: ClientSession) { | ||
|
||
await _deletePolicyWithSameNameButNotId(policy, session) | ||
|
||
|
@@ -109,7 +139,7 @@ async function _deployOnePolicy(policy: any, session: ClientSession) { | |
* @see _deployOnePolicy() | ||
* @private | ||
*/ | ||
async function _deletePolicyWithSameNameButNotId(policy: any, session: ClientSession) { | ||
async function _deletePolicyWithSameNameButNotId(policy: PolicyStruct, session: ClientSession) { | ||
const db = DatabaseAgent.sys_accessor.db | ||
await db.collection(Constants.cn.policies).findOneAndDelete({ | ||
_id: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { ApplicationStruct } from "../api/application" | ||
import { FunctionStruct } from "../api/function" | ||
import { Constants } from "../constants" | ||
import { DatabaseAgent } from "./db-agent" | ||
|
||
|
||
/** | ||
* Export application definition to json object: | ||
* - cloud functions | ||
* - policies | ||
*/ | ||
export class ApplicationExporter { | ||
readonly app: ApplicationStruct | ||
|
||
constructor(app: ApplicationStruct) { | ||
this.app = app | ||
} | ||
|
||
async build() { | ||
const meta = this.buildMeta() | ||
const functions = await this.buildFunctions() | ||
const policies = await this.buildPolicies() | ||
|
||
return { | ||
meta, | ||
functions, | ||
policies | ||
} | ||
} | ||
|
||
private buildMeta() { | ||
return { | ||
name: this.app.name, | ||
created_at: Date.now() | ||
} | ||
} | ||
|
||
private async buildFunctions() { | ||
const db = DatabaseAgent.sys_db | ||
const r = await db.collection(Constants.cn.functions) | ||
.where({ appid: this.app.appid }) | ||
.limit(999) | ||
.get<FunctionStruct>() | ||
|
||
const functions = r.data | ||
const ret = functions.map(func => { | ||
return { | ||
name: func.name, | ||
label: func.label, | ||
code: func.code, | ||
hash: func.hash, | ||
tags: func.tags, | ||
description: func.description, | ||
enableHTTP: func.enableHTTP, | ||
status: func.status, | ||
triggers: func.triggers, | ||
debugParams: func.debugParams, | ||
version: func.version | ||
} | ||
}) | ||
|
||
return ret | ||
} | ||
|
||
private async buildPolicies() { | ||
const db = DatabaseAgent.sys_db | ||
const r = await db.collection(Constants.cn.policies) | ||
.where({ appid: this.app.appid }) | ||
.get() | ||
|
||
const policies = r.data | ||
const ret = policies.map(po => { | ||
return { | ||
name: po.name, | ||
description: po.description, | ||
status: po.status, | ||
rules: po.rules, | ||
injector: po.injector ?? null, | ||
hash: po.hash, | ||
} | ||
}) | ||
|
||
return ret | ||
} | ||
} |
Oops, something went wrong.