初始化项目
This commit is contained in:
commit
28f327c903
19 files changed
+434
No files matched your search
@@ -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',
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
coverage
|
||||||
@@ -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 }}
|
||||||
+14
@@ -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,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
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
# api-service
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 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
|
||||||
@@ -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;
|
||||||
@@ -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;
|
||||||
@@ -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);
|
||||||
|
};
|
||||||
@@ -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);
|
||||||
|
};
|
||||||
@@ -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;
|
||||||
@@ -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;
|
||||||
@@ -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
|
||||||
@@ -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,
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/** @type Egg.EggPlugin */
|
||||||
|
module.exports = {
|
||||||
|
// had enabled by egg
|
||||||
|
// static: {
|
||||||
|
// enable: true,
|
||||||
|
// }
|
||||||
|
};
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"include": [
|
||||||
|
"**/*"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -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"
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user