소스 검색

add mock server

star7th 5 년 전
부모
커밋
1895f9d3a4
14개의 변경된 파일275개의 추가작업 그리고 0개의 파일을 삭제
  1. 29 0
      mock/.autod.conf.js
  2. 1 0
      mock/.eslintignore
  3. 3 0
      mock/.eslintrc
  4. 14 0
      mock/.gitignore
  5. 12 0
      mock/.travis.yml
  6. 33 0
      mock/README.md
  7. 41 0
      mock/app/controller/home.js
  8. 10 0
      mock/app/router.js
  9. 14 0
      mock/appveyor.yml
  10. 37 0
      mock/config/config.default.js
  11. 9 0
      mock/config/plugin.js
  12. 5 0
      mock/jsconfig.json
  13. 47 0
      mock/package.json
  14. 20 0
      mock/test/app/controller/home.test.js

+ 29 - 0
mock/.autod.conf.js

@@ -0,0 +1,29 @@
+'use strict';
+
+module.exports = {
+  write: true,
+  prefix: '^',
+  plugin: 'autod-egg',
+  test: [
+    'test',
+    'benchmark',
+  ],
+  dep: [
+    'egg',
+    'egg-scripts',
+  ],
+  devdep: [
+    'egg-ci',
+    'egg-bin',
+    'egg-mock',
+    'autod',
+    'autod-egg',
+    'eslint',
+    'eslint-config-egg',
+  ],
+  exclude: [
+    './test/fixtures',
+    './dist',
+  ],
+};
+

+ 1 - 0
mock/.eslintignore

@@ -0,0 +1 @@
+coverage

+ 3 - 0
mock/.eslintrc

@@ -0,0 +1,3 @@
+{
+  "extends": "eslint-config-egg"
+}

+ 14 - 0
mock/.gitignore

@@ -0,0 +1,14 @@
+logs/
+npm-debug.log
+yarn-error.log
+node_modules/
+package-lock.json
+yarn.lock
+coverage/
+.idea/
+run/
+.DS_Store
+*.sw*
+*.un~
+typings/
+.nyc_output/

+ 12 - 0
mock/.travis.yml

@@ -0,0 +1,12 @@
+
+language: node_js
+node_js:
+  - '10'
+before_install:
+  - npm i npminstall -g
+install:
+  - npminstall
+script:
+  - npm run ci
+after_script:
+  - npminstall codecov && codecov

+ 33 - 0
mock/README.md

@@ -0,0 +1,33 @@
+# mockServer
+
+
+
+## QuickStart
+
+<!-- add docs here for user -->
+
+see [egg docs][egg] for more detail.
+
+### Development
+
+```bash
+$ npm i
+$ npm run dev
+$ open http://localhost:7001/
+```
+
+### Deploy
+
+```bash
+$ npm start
+$ npm stop
+```
+
+### npm scripts
+
+- Use `npm run lint` to check code style.
+- Use `npm test` to run unit test.
+- Use `npm run autod` to auto detect dependencies upgrade, see [autod](https://www.npmjs.com/package/autod) for more detail.
+
+
+[egg]: https://eggjs.org

+ 41 - 0
mock/app/controller/home.js

@@ -0,0 +1,41 @@
+'use strict';
+var Mock = require('mockjs');
+
+const Controller = require('egg').Controller;
+
+class HomeController extends Controller {
+  async index() {
+    const { ctx } = this;
+    ctx.body = 'hi, egg';
+  }
+
+  async mock() {
+    const { ctx } = this;
+    var data = '';
+    var template = ctx.request.body.template ;
+    /* 
+    template = {
+	  'list|20':[{
+	        'id|+1':1,
+	        'serial_number|1-100':1,
+	        'warn_number|1-100':1,
+	        'warn_name|1':['流水线编排服务异常','磁盘占用超过阈值'],
+	        'warn_level|1':['紧急','重要'],
+	        'warn_detail':'环境IP:127.0.0.1,服务名称:XX',
+	        'create_time':'@date("yyyy-MM-dd")',
+	        'finish_time':'@date("yyyy-MM-dd")',
+	        'contact|4':'abc'
+	    }] 
+	 };
+	 */
+    try{
+    	data =Mock.mock(JSON.parse(template));
+    }catch(e){
+    	data = '为了服务器安全,只允许符合json语法的字符串'
+    }
+    ctx.body = data;
+  }
+
+}
+
+module.exports = HomeController;

+ 10 - 0
mock/app/router.js

@@ -0,0 +1,10 @@
+'use strict';
+
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const { router, controller } = app;
+  router.all('/', controller.home.index);
+  router.all('/mock', controller.home.mock);
+};

+ 14 - 0
mock/appveyor.yml

@@ -0,0 +1,14 @@
+environment:
+  matrix:
+    - nodejs_version: '10'
+
+install:
+  - ps: Install-Product node $env:nodejs_version
+  - npm i npminstall && node_modules\.bin\npminstall
+
+test_script:
+  - node --version
+  - npm --version
+  - npm run test
+
+build: off

+ 37 - 0
mock/config/config.default.js

@@ -0,0 +1,37 @@
+/* eslint valid-jsdoc: "off" */
+
+'use strict';
+
+/**
+ * @param {Egg.EggAppInfo} appInfo app info
+ */
+module.exports = appInfo => {
+  /**
+   * built-in config
+   * @type {Egg.EggAppConfig}
+   **/
+  const config = exports = {};
+
+  // use for cookie sign key, should change to your own and keep security
+  config.keys = appInfo.name + '_1656735598785_406';
+
+  // add your middleware config here
+  config.middleware = [];
+
+  // add your user config here
+  const userConfig = {
+    // myAppName: 'egg',
+  };
+
+  //安全设置
+  config.security= {
+     csrf: {
+       enable: false
+     }
+  }
+
+  return {
+    ...config,
+    ...userConfig,
+  };
+};

+ 9 - 0
mock/config/plugin.js

@@ -0,0 +1,9 @@
+'use strict';
+
+/** @type Egg.EggPlugin */
+module.exports = {
+  // had enabled by egg
+  // static: {
+  //   enable: true,
+  // }
+};

+ 5 - 0
mock/jsconfig.json

@@ -0,0 +1,5 @@
+{
+  "include": [
+    "**/*"
+  ]
+}

+ 47 - 0
mock/package.json

@@ -0,0 +1,47 @@
+{
+  "name": "mockServer",
+  "version": "1.0.0",
+  "description": "",
+  "private": true,
+  "egg": {
+    "declarations": true
+  },
+  "dependencies": {
+    "egg": "^2.15.1",
+    "egg-scripts": "^2.11.0",
+    "mockjs": "^1.1.0"
+  },
+  "devDependencies": {
+    "autod": "^3.0.1",
+    "autod-egg": "^1.1.0",
+    "egg-bin": "^4.11.0",
+    "egg-ci": "^1.11.0",
+    "egg-mock": "^3.21.0",
+    "eslint": "^5.13.0",
+    "eslint-config-egg": "^7.1.0"
+  },
+  "engines": {
+    "node": ">=10.0.0"
+  },
+  "scripts": {
+    "start": "egg-scripts start --daemon --title=egg-server-mockServer --port 7123",
+    "stop": "egg-scripts stop --title=egg-server-mockServer",
+    "dev": "egg-bin dev --port 7123 --workers=1 ",
+    "debug": "egg-bin debug",
+    "test": "npm run lint -- --fix && npm run test-local",
+    "test-local": "egg-bin test",
+    "cov": "egg-bin cov",
+    "lint": "eslint .",
+    "ci": "npm run lint && npm run cov",
+    "autod": "autod"
+  },
+  "ci": {
+    "version": "10"
+  },
+  "repository": {
+    "type": "git",
+    "url": ""
+  },
+  "author": "star7th",
+  "license": "MIT"
+}

+ 20 - 0
mock/test/app/controller/home.test.js

@@ -0,0 +1,20 @@
+'use strict';
+
+const { app, assert } = require('egg-mock/bootstrap');
+
+describe('test/app/controller/home.test.js', () => {
+  it('should assert', () => {
+    const pkg = require('../../../package.json');
+    assert(app.config.keys.startsWith(pkg.name));
+
+    // const ctx = app.mockContext({});
+    // yield ctx.service.xx();
+  });
+
+  it('should GET /', () => {
+    return app.httpRequest()
+      .get('/')
+      .expect('hi, egg')
+      .expect(200);
+  });
+});