2
0
liuyu преди 5 години
родител
ревизия
0418abd136
променени са 21 файла, в които са добавени 377 реда и са изтрити 0 реда
  1. 29 0
      .autod.conf.js
  2. 1 0
      .eslintignore
  3. 3 0
      .eslintrc
  4. 14 0
      .gitignore
  5. 12 0
      .travis.yml
  6. 3 0
      .vscode/settings.json
  7. 33 0
      app/controller/.optlog.js
  8. 12 0
      app/controller/home.js
  9. 16 0
      app/controller/optlog.js
  10. 27 0
      app/model/optlog.js
  11. 10 0
      app/router.js
  12. 13 0
      app/service/optlog.js
  13. 14 0
      appveyor.yml
  14. 49 0
      config/config.default.js
  15. 24 0
      config/config.local.js
  16. 12 0
      config/config.prod.js
  17. 9 0
      config/plugin.js
  18. 17 0
      ecosystem.config.js
  19. 50 0
      package.json
  20. 9 0
      server.js
  21. 20 0
      test/app/controller/home.test.js

+ 29 - 0
.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
.eslintignore

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

+ 3 - 0
.eslintrc

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

+ 14 - 0
.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
.travis.yml

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

+ 3 - 0
.vscode/settings.json

@@ -0,0 +1,3 @@
+{
+    "eggHelper.serverPort": 62737
+}

+ 33 - 0
app/controller/.optlog.js

@@ -0,0 +1,33 @@
+module.exports = {
+  create: {
+    requestBody: ['!userid', 'name', '!unitid', 'ip', 'createtime', 'type', 'target', 'content', 'status', 'remark']
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete'
+  },
+  update: {
+    parameters: {
+      params: ['!id']
+    },
+    requestBody: ['userid', 'name', 'unitid', 'ip', 'createtime', 'type', 'target', 'content', 'status', 'remark']
+  },
+  show: {
+    parameters: {
+      params: ['!id']
+    },
+    service: 'fetch'
+  },
+  index: {
+    parameters: {
+      query: ['userid', 'unitid', 'type','createtime', 'status']
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: false,
+      count: true
+    }
+  }
+};

+ 12 - 0
app/controller/home.js

@@ -0,0 +1,12 @@
+'use strict';
+
+const Controller = require('egg').Controller;
+
+class HomeController extends Controller {
+  async index() {
+    const { ctx } = this;
+    ctx.body = 'hi, egg';
+  }
+}
+
+module.exports = HomeController;

+ 16 - 0
app/controller/optlog.js

@@ -0,0 +1,16 @@
+'use strict';
+
+const _ = require('lodash');
+const meta = require('./.optlog.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 待办事项信息
+class OptlogController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.optlog;
+  }
+}
+
+module.exports = CrudController(OptlogController, meta);

+ 27 - 0
app/model/optlog.js

@@ -0,0 +1,27 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+// 文件导入表
+const OptlogSchema = {
+  userid: { type: String, required: true, maxLength: 64 }, // 操作人
+  name: { type: String, required: false, maxLength: 200 }, // 名称
+  unitid: { type: String, required: true, maxLength: 64 }, // 学校/分站id
+  ip: { type: String, required: false, maxLength: 64 }, // ip地址
+  createtime: { type: String, required: false, maxLength: 20 }, // 时间
+  type: { type: String, required: false, maxLength: 64 }, // 操作类型: 1. 添加 2.修改 3. 删除 4.查询
+  target: { type: String, required: false, maxLength: 200 }, // 操作目标,根据页面的menu页区分
+  content: { type: String, required: false, maxLength: 64 }, // 描述
+  status: { type: String, required: false, maxLength: 2 }, // 操作结果
+  remark: { type: String, required: false, maxLength: 200 }, // 备注
+};
+
+
+const schema = new Schema(OptlogSchema, { toJSON: { virtuals: true } });
+schema.index({ userid: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Optlog', schema, 'opt_log');
+};

+ 10 - 0
app/router.js

@@ -0,0 +1,10 @@
+'use strict';
+
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const { router, controller } = app;
+  router.get('/', controller.home.index);
+  router.resources('optlog', '/api/optlog', controller.optlog); // index、create、show、destroy
+};

+ 13 - 0
app/service/optlog.js

@@ -0,0 +1,13 @@
+'use strict';
+
+const _ = require('lodash');
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+
+class OptlogService extends CrudService {
+  constructor(ctx) {
+    super(ctx);
+    this.model = this.ctx.model.Optlog;
+  }
+}
+
+module.exports = OptlogService;

+ 14 - 0
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

+ 49 - 0
config/config.default.js

@@ -0,0 +1,49 @@
+/* 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 + '_1573520974251_6291';
+
+  // add your middleware config here
+  // config.middleware = [ 'optlog' ];
+
+  // add your user config here
+  const userConfig = {
+    // myAppName: 'egg',
+  };
+
+  // add your config here
+  config.cluster = {
+    listen: {
+      port: 8108,
+    },
+  };
+
+  // mongoose config
+  config.mongoose = {
+    url: 'mongodb://127.0.0.1:27017/smart',
+    options: {
+      user: 'root',
+      pass: 'Ziyouyanfa#@!',
+      authSource: 'admin',
+      useNewUrlParser: true,
+      useCreateIndex: true,
+    },
+  };
+
+  return {
+    ...config,
+    ...userConfig,
+  };
+};

+ 24 - 0
config/config.local.js

@@ -0,0 +1,24 @@
+'use strict';
+
+module.exports = () => {
+  const config = (exports = {});
+
+  config.logger = {
+    level: 'DEBUG',
+    consoleLevel: 'DEBUG',
+  };
+
+  config.mongoose = {
+    url: 'mongodb://localhost:27017/smart',
+    options: {
+      user: 'demo',
+      pass: 'demo',
+      authSource: 'admin',
+      useNewUrlParser: true,
+      useCreateIndex: true,
+      useUnifiedTopology: true,
+    },
+  };
+
+  return config;
+};

+ 12 - 0
config/config.prod.js

@@ -0,0 +1,12 @@
+'use strict';
+
+module.exports = () => {
+  const config = exports = {};
+
+  config.logger = {
+    // level: 'DEBUG',
+    // consoleLevel: 'DEBUG',
+  };
+
+  return config;
+};

+ 9 - 0
config/plugin.js

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

+ 17 - 0
ecosystem.config.js

@@ -0,0 +1,17 @@
+'use strict';
+
+const app = 'service-log';
+module.exports = {
+  apps: [{
+    name: app, // 应用名称
+    script: './server.js', // 实际启动脚本
+    out: `./logs/${app}.log`,
+    error: `./logs/${app}.err`,
+    watch: [ // 监控变化的目录,一旦变化,自动重启
+      'app', 'config',
+    ],
+    env: {
+      NODE_ENV: 'production', // 环境参数,当前指定为生产环境
+    },
+  }],
+};

+ 50 - 0
package.json

@@ -0,0 +1,50 @@
+{
+  "name": "service-log",
+  "version": "1.0.0",
+  "description": "",
+  "private": true,
+  "egg": {
+    "framework": "naf-framework-mongoose"
+  },
+  "dependencies": {
+    "egg": "^2.23.0",
+    "egg-naf-amqp": "0.0.13",
+    "egg-scripts": "^2.11.0",
+    "jsonwebtoken": "^8.5.1",
+    "naf-framework-mongoose": "^0.6.11",
+    "silly-datetime": "^0.1.2"
+  },
+  "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-service-log",
+    "stop": "egg-scripts stop --title=egg-server-service-log",
+    "dev": "egg-bin dev",
+    "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": "ly",
+  "license": "MIT"
+}

+ 9 - 0
server.js

@@ -0,0 +1,9 @@
+
+// eslint-disable-next-line strict
+const egg = require('egg');
+
+const workers = Number(process.argv[2] || require('os').cpus().length);
+egg.startCluster({
+  workers,
+  baseDir: __dirname,
+});

+ 20 - 0
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);
+  });
+});