From 28f327c9035b807949f104fe24a6b53bd51aa06f Mon Sep 17 00:00:00 2001 From: dengtongyu Date: Fri, 20 Nov 2020 16:07:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .autod.conf.js | 29 ++++++++++++++++ .eslintignore | 1 + .eslintrc | 3 ++ .github/workflows/nodejs.yml | 42 +++++++++++++++++++++++ .gitignore | 14 ++++++++ .travis.yml | 12 +++++++ README.md | 33 ++++++++++++++++++ app/controller/home.js | 12 +++++++ app/controller/kugou.js | 48 ++++++++++++++++++++++++++ app/router.js | 11 ++++++ app/router/music.js | 14 ++++++++ app/service/kugou.js | 59 ++++++++++++++++++++++++++++++++ app/util/respFormat.js | 25 ++++++++++++++ appveyor.yml | 14 ++++++++ config/config.default.js | 37 ++++++++++++++++++++ config/plugin.js | 9 +++++ jsconfig.json | 5 +++ package.json | 46 +++++++++++++++++++++++++ test/app/controller/home.test.js | 20 +++++++++++ 19 files changed, 434 insertions(+) create mode 100644 .autod.conf.js create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 .github/workflows/nodejs.yml create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 README.md create mode 100644 app/controller/home.js create mode 100644 app/controller/kugou.js create mode 100644 app/router.js create mode 100644 app/router/music.js create mode 100644 app/service/kugou.js create mode 100644 app/util/respFormat.js create mode 100644 appveyor.yml create mode 100644 config/config.default.js create mode 100644 config/plugin.js create mode 100644 jsconfig.json create mode 100644 package.json create mode 100644 test/app/controller/home.test.js diff --git a/.autod.conf.js b/.autod.conf.js new file mode 100644 index 0000000..8c53939 --- /dev/null +++ b/.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', + ], +}; + diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..4ebc8ae --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +coverage diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..c799fe5 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": "eslint-config-egg" +} diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..04ffea7 --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,42 @@ +# 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: [ master ] + pull_request: + branches: [ 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 }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c487f23 --- /dev/null +++ b/.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/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ec260bb --- /dev/null +++ b/.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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..652ddeb --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# api-service + + + +## QuickStart + + + +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 \ No newline at end of file diff --git a/app/controller/home.js b/app/controller/home.js new file mode 100644 index 0000000..b8e82fe --- /dev/null +++ b/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; diff --git a/app/controller/kugou.js b/app/controller/kugou.js new file mode 100644 index 0000000..4791172 --- /dev/null +++ b/app/controller/kugou.js @@ -0,0 +1,48 @@ +'use strict'; + +const Controller = require('egg').Controller; +const respFormat = require('../util/respFormat'); + +class KuGouController extends Controller { + async songSearch() { + const { ctx } = this; + try { + const res = await ctx.service.kugou.getSearchSongList(ctx.query); + if (res.status === 1) { + ctx.body = respFormat({ data: res.data }); + } else { + ctx.body = respFormat({ desc: '请求出错,请稍后再试!', errcode: res.err_code }); + } + } catch (err) { + ctx.body = respFormat({ desc: '出现未知错误!', errcode: 1000 }); + } + } + async newSong() { + const { ctx } = this; + try { + const res = await ctx.service.kugou.getNewSongList(ctx.query); + if (res.data && res.data.length && res.data.length > 0) { + ctx.body = respFormat({ data: res.data }); + } else { + ctx.body = respFormat({ desc: '请求出错,请稍后再试!', errcode: res.err_code }); + } + } catch (err) { + ctx.body = respFormat({ desc: '出现未知错误!', errcode: 1000 }); + } + } + async songMsg() { + const { ctx } = this; + try { + const res = await ctx.service.kugou.getSongMsg(ctx.query); + if (res.status == 1) { + ctx.body = respFormat({ data: res.data }); + } else { + ctx.body = respFormat({ desc: '请求出错,请稍后再试!', errcode: res.err_code }); + } + } catch (err) { + ctx.body = respFormat({ desc: '出现未知错误!', errcode: 1000 }); + } + } +} + +module.exports = KuGouController; diff --git a/app/router.js b/app/router.js new file mode 100644 index 0000000..6834a46 --- /dev/null +++ b/app/router.js @@ -0,0 +1,11 @@ +'use strict'; + +/** + * @param {Egg.Application} app - egg application + */ +module.exports = app => { + // const { router, controller } = app; + // router.get('/', controller.home.index); + + require('./router/music')(app); +}; diff --git a/app/router/music.js b/app/router/music.js new file mode 100644 index 0000000..b18ef4b --- /dev/null +++ b/app/router/music.js @@ -0,0 +1,14 @@ +'use strict'; + +// const neteasecloud = '/music/neteasecloud'; + +const kugou = '/music/kugou'; + +module.exports = app => { + const { router, controller } = app; + // router.get(neteasecloud, controller.music.neteasecloud); + + router.get(kugou + '/songSearch', controller.kugou.songSearch); + router.get(kugou + '/newSong', controller.kugou.newSong); + router.get(kugou + '/songMsg', controller.kugou.songMsg); +}; diff --git a/app/service/kugou.js b/app/service/kugou.js new file mode 100644 index 0000000..099d720 --- /dev/null +++ b/app/service/kugou.js @@ -0,0 +1,59 @@ +'use strict'; + +const Service = require('egg').Service; + +class KuGouService extends Service { + async getSearchSongList(param) { + try { + const header = { + method: 'GET', + dataType: 'json', + data: { + ...param, + _: Date.now(), + }, + }; + const url = 'http://songsearch.kugou.com/song_search_v2'; + const res = await this.ctx.curl(url, header); + return res.data; + } catch (err) { + throw new Error(err); + } + } + async getNewSongList(param) { + try { + const header = { + method: 'GET', + dataType: 'json', + data: { + ...param, + _: Date.now(), + }, + }; + const url = 'http://m.kugou.com/'; + const res = await this.ctx.curl(url, header); + return res.data; + } catch (err) { + throw new Error(err); + } + } + async getSongMsg(hash) { + try { + const header = { + method: 'GET', + dataType: 'json', + data: { + hash, + r: 'play/getdata', + }, + }; + const url = 'http://www.kugou.com/yy/index.php'; + const res = await this.ctx.curl(url, header); + return res.data; + } catch (err) { + throw new Error(err); + } + } +} + +module.exports = KuGouService; diff --git a/app/util/respFormat.js b/app/util/respFormat.js new file mode 100644 index 0000000..43d352a --- /dev/null +++ b/app/util/respFormat.js @@ -0,0 +1,25 @@ +'use strict'; + +const getErrorMessge = code => { + let message = ''; + switch (code) { + case 0: + message = '请求成功'; + break; + default: + message = '请求成功'; + break; + } + return message; +}; + +const respFormat = option => { + const res = {}; + res.data = option.hasOwnProperty('data') ? option.data : ''; + res.errcode = option.hasOwnProperty('errcode') ? option.errcode : 0; + res.result = option.hasOwnProperty('result') ? option.result : true; + res.desc = option.hasOwnProperty('desc') ? option.desc : getErrorMessge(res.errcode); + return res; +}; + +module.exports = respFormat; diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..5830a38 --- /dev/null +++ b/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 diff --git a/config/config.default.js b/config/config.default.js new file mode 100644 index 0000000..e8b2f47 --- /dev/null +++ b/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 + '_1605853804883_9478'; + + // add your middleware config here + config.middleware = []; + + // add your user config here + const userConfig = { + // myAppName: 'egg', + }; + + config.onerror = { + all(error, ctx) { + ctx.body = { errcode: 500, desc: error, result: false, data: null }; + ctx.status = 500; + }, + }; + + return { + ...config, + ...userConfig, + }; +}; diff --git a/config/plugin.js b/config/plugin.js new file mode 100644 index 0000000..5711c6f --- /dev/null +++ b/config/plugin.js @@ -0,0 +1,9 @@ +'use strict'; + +/** @type Egg.EggPlugin */ +module.exports = { + // had enabled by egg + // static: { + // enable: true, + // } +}; diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..1bbed3f --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,5 @@ +{ + "include": [ + "**/*" + ] +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..bb8b290 --- /dev/null +++ b/package.json @@ -0,0 +1,46 @@ +{ + "name": "api-service", + "version": "1.0.0", + "description": "", + "private": true, + "egg": { + "declarations": true + }, + "dependencies": { + "egg": "^2.15.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-api-service", + "stop": "egg-scripts stop --title=egg-server-api-service", + "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": "dengtongyu", + "license": "MIT" +} diff --git a/test/app/controller/home.test.js b/test/app/controller/home.test.js new file mode 100644 index 0000000..d5be671 --- /dev/null +++ b/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); + }); +});