فهرست منبع

添加读者服务

asd123a20 3 سال پیش
والد
کامیت
d018e4e5e5

+ 0 - 46
service-login/.github/workflows/nodejs.yml

@@ -1,46 +0,0 @@
-# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
-# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
-
-name: Node.js CI
-
-on:
-  push:
-    branches:
-      - main
-      - master
-  pull_request:
-    branches:
-      - main
-      - master
-  schedule:
-    - cron: '0 2 * * *'
-
-jobs:
-  build:
-    runs-on: ${{ matrix.os }}
-
-    strategy:
-      fail-fast: false
-      matrix:
-        node-version: [10]
-        os: [ubuntu-latest, windows-latest, macos-latest]
-
-    steps:
-    - name: Checkout Git Source
-      uses: actions/checkout@v2
-
-    - name: Use Node.js ${{ matrix.node-version }}
-      uses: actions/setup-node@v1
-      with:
-        node-version: ${{ matrix.node-version }}
-
-    - name: Install Dependencies
-      run: npm i -g npminstall && npminstall
-
-    - name: Continuous Integration
-      run: npm run ci
-
-    - name: Code Coverage
-      uses: codecov/codecov-action@v1
-      with:
-        token: ${{ secrets.CODECOV_TOKEN }}

+ 0 - 46
service-naf/.github/workflows/nodejs.yml

@@ -1,46 +0,0 @@
-# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
-# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
-
-name: Node.js CI
-
-on:
-  push:
-    branches:
-      - main
-      - master
-  pull_request:
-    branches:
-      - main
-      - master
-  schedule:
-    - cron: '0 2 * * *'
-
-jobs:
-  build:
-    runs-on: ${{ matrix.os }}
-
-    strategy:
-      fail-fast: false
-      matrix:
-        node-version: [10]
-        os: [ubuntu-latest, windows-latest, macos-latest]
-
-    steps:
-    - name: Checkout Git Source
-      uses: actions/checkout@v2
-
-    - name: Use Node.js ${{ matrix.node-version }}
-      uses: actions/setup-node@v1
-      with:
-        node-version: ${{ matrix.node-version }}
-
-    - name: Install Dependencies
-      run: npm i -g npminstall && npminstall
-
-    - name: Continuous Integration
-      run: npm run ci
-
-    - name: Code Coverage
-      uses: codecov/codecov-action@v1
-      with:
-        token: ${{ secrets.CODECOV_TOKEN }}

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

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

+ 3 - 0
service-reader/.eslintrc

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

+ 14 - 0
service-reader/.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
service-reader/.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
service-reader/README.md

@@ -0,0 +1,33 @@
+# service-code
+
+# 点赞/收藏/评论/留言
+
+## 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

+ 23 - 0
service-reader/app/controller/reader.js

@@ -0,0 +1,23 @@
+'use strict';
+const Controller = require('egg').Controller;
+
+class ReaderController extends Controller {
+  async create() {
+    const res = await this.ctx.service.reader.create(this.ctx.request.body);
+    this.ctx.body = res;
+  }
+  async update() {
+    const res = await this.ctx.service.reader.update(this.ctx.request.body);
+    this.ctx.body = res;
+  }
+  async delete() {
+    const res = await this.ctx.service.reader.delete(this.ctx.params);
+    this.ctx.body = res;
+  }
+  async query() {
+    const res = await this.ctx.service.reader.query(this.ctx.query);
+    this.ctx.body = res;
+  }
+}
+
+module.exports = ReaderController;

+ 13 - 0
service-reader/app/model/reader.js

@@ -0,0 +1,13 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const SchemaDefine = {
+  // 文章或杂志ID
+  source: { type: String, required: true },
+  // 用户ID
+  target: { type: String, required: true },
+};
+const schema = new Schema(SchemaDefine);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('reader', schema, 'reader');
+};

+ 13 - 0
service-reader/app/router.js

@@ -0,0 +1,13 @@
+'use strict';
+
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const { router, controller } = app;
+  // 读者
+  router.post('/api/reader/user/create', controller.reader.create);
+  router.post('/api/reader/user/update', controller.reader.update);
+  router.delete('/api/reader/user/delete/:id', controller.reader.delete);
+  router.get('/api/reader/user/query', controller.reader.query);
+};

+ 67 - 0
service-reader/app/service/reader.js

@@ -0,0 +1,67 @@
+'use strict';
+
+const assert = require('assert');
+const Service = require('egg').Service;
+class ReaderService extends Service {
+  constructor(ctx) {
+    super(ctx);
+    this.model = this.ctx.model.Reader;
+  }
+  async create({ source, target }) {
+    assert(source, 'source不存在');
+    assert(target, 'target不存在');
+    try {
+      const total = await this.model.find({ source, target });
+      if (total.length > 0) {
+        return { errcode: -1001, errmsg: '不能重复添加' };
+      }
+      const res = await this.model.create({ source, target });
+      return { errcode: 0, errmsg: 'ok', data: res };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async update({ id, source, target }) {
+    assert(id, 'id不存在');
+    try {
+      await this.model.updateOne({ _id: id }, { source, target });
+      return { errcode: 0, errmsg: 'ok', data: '' };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async delete({ id }) {
+    assert(id, 'id不存在');
+    try {
+      await this.model.deleteOne({ _id: id });
+      return { errcode: 0, errmsg: 'ok', data: '' };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async query({ skip, limit, source, target }) {
+    const filter = {};
+    const arr = { source, target };
+    for (const e in arr) {
+      const data = `{ "${e}": { "$regex": "${arr[e]}" } }`;
+      if (arr[e]) {
+        filter.$or = [];
+        filter.$or.push(JSON.parse(data));
+      }
+    }
+    try {
+      const total = await this.model.find({ ...filter });
+      let res;
+      if (skip && limit) {
+        res = await this.model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
+      } else {
+        res = await this.model.find({ ...filter });
+      }
+      return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
+    } catch (error) {
+      throw error;
+    }
+  }
+}
+
+module.exports = ReaderService;

+ 14 - 0
service-reader/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

+ 54 - 0
service-reader/config/config.default.js

@@ -0,0 +1,54 @@
+/* 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 + '_1635902541751_9477';
+
+  // add your middleware config here
+  config.middleware = [];
+  // add your user config here
+  const userConfig = {
+    // myAppName: 'egg',
+  };
+  // 安全配置
+  config.security = {
+    csrf: {
+      // ignoreJSON: true, // 默认为 false,当设置为 true 时,将会放过所有 content-type 为 `application/json` 的请求
+      enable: false,
+    },
+  };
+  config.cluster = {
+    listen: {
+      port: 9014,
+    },
+  };
+  // 数据库配置
+  config.mongoose = {
+    url: 'mongodb://127.0.0.1/Microservices',
+    options: {
+      // user: 'root',
+      // pass: 'cms@cc-lotus',
+      // authSource: 'admin',
+      // useNewUrlParser: true,
+      // useCreateIndex: true,
+    },
+  };
+  config.logger = {
+    level: 'DEBUG',
+  };
+  return {
+    ...config,
+    ...userConfig,
+  };
+};

+ 9 - 0
service-reader/config/plugin.js

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

+ 17 - 0
service-reader/ecosystem.config.js

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

+ 5 - 0
service-reader/jsconfig.json

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

+ 47 - 0
service-reader/package.json

@@ -0,0 +1,47 @@
+{
+  "name": "service-reader",
+  "version": "1.0.0",
+  "description": "",
+  "private": true,
+  "egg": {
+    "declarations": true
+  },
+  "dependencies": {
+    "egg": "^2.15.1",
+    "egg-mongoose": "^3.3.1",
+    "egg-scripts": "^2.11.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-service-reader",
+    "stop": "egg-scripts stop --title=egg-server-service-reader",
+    "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": "",
+  "license": "MIT"
+}

+ 9 - 0
service-reader/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
service-reader/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);
+  });
+});