Skip to content

Commit

Permalink
feat(system-server): create system server package;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Aug 30, 2021
1 parent d8794b9 commit 508c76e
Show file tree
Hide file tree
Showing 56 changed files with 4,432 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/system-server/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# node_modules
.env
ecosystem.config.js
# dist
data
src
tests
5 changes: 5 additions & 0 deletions packages/system-server/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

SYS_DB=laf-sys
SYS_DB_URI=mongodb://localhost:27017
SYS_SERVER_SECRET_SALT=abcdefg1234567!@#$%^&sadfqwef&*^*#!@^
PORT=9000
5 changes: 5 additions & 0 deletions packages/system-server/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

SYS_DB=laf-sys
SYS_DB_URI=mongodb://localhost:27017
SYS_SERVER_SECRET_SALT=abcdefg1234567!@#$%^&sadfqwef&*^*#!@^
PORT=9000
11 changes: 11 additions & 0 deletions packages/system-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules
dist

upload
data/*
tmp

.env

ecosystem.config.js
.DS_Store
15 changes: 15 additions & 0 deletions packages/system-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:14-alpine
RUN apk add --no-cache openssl
ENV DOCKERIZE_VERSION v0.6.1
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& rm dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz

EXPOSE 9000
WORKDIR /app
ENV LOG_LEVEL=debug
COPY . /app
# RUN npm i
# RUN npm run build
USER node
CMD [ "npm", "run", "init-start" ]
53 changes: 53 additions & 0 deletions packages/system-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# less api framework - devops server

## 介绍

`laf-devops-server` 是 laf 中负责在线开发和运维应用的服务:

- 云函数管理
- 数据访问策略管理
- 数据库管理:集合与数据管理,备份与恢复
- 应用状态:启停、指标统计、伸缩

另配套有 `laf-devops-client` - LAF 开发运维控制台,提供 Web IDE,在线编写、调试云函数,操作数据库等。


## 安装部署

### 安装依赖

```sh
cd packages/devops-server

# 安装依赖(建议使用 node 14+)
npm install

# 编译
npm run build
```


### 启动 MongoDb

```sh
# 因 laf-app-server 服务使用到了 mongodb watch() 功能,要求 mongo 必须用 replica 或 cluster 模式
# 若已有 mongo 实例,则跳过此步
docker run -p 27017:27017 --name laf_mongo -e ALLOW_EMPTY_PASSWORD=yes -e MONGODB_REPLICA_SET_MODE=primary -e MONGODB_INITIAL_PRIMARY_HOST=localhost -d bitnami/mongodb
```

### 配置数据库,并初始化应用

```sh
# 创建配置文件 .env
cp .env.development .env

# 初始化管理员、访问策略、云函数等数据
npm run init
```

### 运行

```sh
#运行
npm start
```
8 changes: 8 additions & 0 deletions packages/system-server/ecosystem.config.js.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
apps: {
script: 'dist/index.js',
name: 'laf-devops',
interpreter_args: '--max_old_space_size=256',
watch: './dist'
}
};
45 changes: 45 additions & 0 deletions packages/system-server/http/admin.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

@token={{login.response.body.$.data.access_token}}

### 管理员登陆
# @name login

POST {{base_url}}/admin/login HTTP/1.1
Content-Type: application/json

{
"username": "{{sys_admin}}",
"password": "{{sys_password}}"
}

### 管理员信息

GET {{base_url}}/admin/info
Authorization: Bearer {{token}}

### 添加管理员

POST {{base_url}}/admin/add HTTP/1.1
Content-Type: application/json
Authorization: Bearer {{token}}

{
"username": "less-test",
"password": "less123",
"avatar": "https://work.zhuo-zhuo.com/file/data/23ttprpxmavkkuf6nttc/PHID-FILE-vzv6dyqo3ev2tmngx7mu/logoL)",
"name": "Maslow"
}

### 编辑管理员

POST {{base_url}}/admin/edit HTTP/1.1
Content-Type: application/json
Authorization: Bearer {{token}}

{
"uid": "6055c058c342ed67a0cdf856",
"username": "less-test",
"password": "less",
"name": "Maslow",
"avatar": "https://work.zhuo-zhuo.com/file/data/23ttprpxmavkkuf6nttc/PHID-FILE-vzv6dyqo3ev2tmngx7mu/logoL)"
}
46 changes: 46 additions & 0 deletions packages/system-server/http/dbm.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

@token={{login.response.body.$.data.access_token}}

### 管理员登陆
# @name login

POST {{base_url}}/admin/login HTTP/1.1
Content-Type: application/json

{
"username": "{{sys_admin}}",
"password": "{{sys_password}}"
}

### 获取集合列表

GET {{base_url}}/admin/collections
Content-Type: application/json
Authorization: Bearer {{token}}

### 获取集合的索引信息

GET {{base_url}}/admin/collection/indexes?collection=functions
Content-Type: application/json
Authorization: Bearer {{token}}


### 创建集合索引

POST {{base_url}}/admin/collection/indexes?collection=functions
Content-Type: application/json;charset=UTF-8
Authorization: Bearer {{token}}

{
"spec": {
"created_at": 1
},
"unique": true
}


### 删除集合索引

DELETE {{base_url}}/admin/collection/indexes?collection=functions&index=created_at_1
Content-Type: application/json;charset=UTF-8
Authorization: Bearer {{token}}
26 changes: 26 additions & 0 deletions packages/system-server/http/deploy.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

@token={{login.response.body.$.data.access_token}}

### 管理员登陆
# @name login

POST {{base_url}}/admin/login HTTP/1.1
Content-Type: application/json

{
"username": "{{sys_admin}}",
"password": "{{sys_password}}"
}


### 创建部署令牌

POST {{base_url}}/deploy/create-token
Content-Type: application/json;charset=UTF-8
Authorization: Bearer {{token}}

{
"permissions": ["policy", "function"],
"expire": 1,
"source": "test"
}
47 changes: 47 additions & 0 deletions packages/system-server/http/file.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

@token={{login.response.body.$.data.access_token}}

### admin login
# @name login

POST {{base_url}}/admin/login HTTP/1.1
Content-Type: application/json

{
"username": "{{sys_admin}}",
"password": "{{sys_password}}"
}

### Get bucket list

GET {{base_url}}/file/buckets
Content-Type: application/json
Authorization: Bearer {{token}}


### Get file list

GET {{base_url}}/file/public/files?limit=3
Content-Type: application/json
Authorization: Bearer {{token}}

### Delete a file

DELETE {{base_url}}/file/public/61124d425a821711e7b6b95d
Authorization: Bearer {{token}}

### Create a bucket

POST {{base_url}}/file/buckets
Content-Type: application/json
Authorization: Bearer {{token}}

{
"bucket": "t9"
}


### Delete a bucket

DELETE {{base_url}}/file/buckets/t9
Authorization: Bearer {{token}}
20 changes: 20 additions & 0 deletions packages/system-server/http/publish.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

@token={{login.response.body.$.data.access_token}}

### 管理员登陆
# @name login

POST {{base_url}}/admin/login HTTP/1.1
Content-Type: application/json

{
"username": "{{sys_admin}}",
"password": "{{sys_password}}"
}


### 发布访问策略

POST {{base_url}}/publish/policy
Content-Type: application/json;charset=UTF-8
Authorization: Bearer {{token}}
77 changes: 77 additions & 0 deletions packages/system-server/init/func-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-08-17 17:34:58
* @Description:
*/

const fs = require('fs/promises')
const path = require('path')

class FunctionLoader {
rootPath = path.join(__dirname, 'functions')

/**
* Get directory list of functions
* @returns {Promise<string[]>}
*/
async getFunctionDirectoryList() {
const dirs = await fs.readdir(this.rootPath)
return dirs ?? []
}

/**
* Load functions
* @returns {Promise<any[]>}
*/
async getFunctions() {
const dirs = await this.getFunctionDirectoryList()
const funcPaths = dirs.map(dir => path.join(this.rootPath, dir))
const results = []
for (const fp of funcPaths) {
const r = await this.loadFunction(fp)
results.push(r)
}
return results
}

/**
* Load a function
* @param {string}} func_path
* @returns
*/
async loadFunction(func_path) {
const codePath = path.join(func_path, 'index.ts')
const code = await this.loadFunctionCode(codePath)

const metaPath = path.join(func_path, 'meta.json')
const meta = await this.loadFunctionMeta(metaPath)

if (!meta['status']) {
meta['status'] = 0
}
return { ...meta, code }
}

/**
* Load function's code
* @param {Promise<string>} file_path
*/
async loadFunctionCode(file_path) {
const data = await fs.readFile(file_path, 'utf-8')
return data
}

/**
* Load meta of function
* @param {string} file_path
*/
async loadFunctionMeta(file_path) {
const data = await fs.readFile(file_path, 'utf-8')
return JSON.parse(data)
}
}

module.exports = {
FunctionLoader
}
Loading

0 comments on commit 508c76e

Please sign in to comment.