Skip to content

Commit

Permalink
Merge pull request #1 from UestcCarpediem/dev_backend
Browse files Browse the repository at this point in the history
Dev backend
  • Loading branch information
Kingfish404 authored Feb 23, 2021
2 parents a4d083b + 20c28b4 commit 0e78c7b
Show file tree
Hide file tree
Showing 27 changed files with 635 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# doc

api.md - server接口文档
70 changes: 70 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# api document

接口文档

## 获取视频流

获取播放视频的接口

```javascript
url: /api/video/getlist
methon: GET
request:
{
id=numbser // 默认为1即可,视频的编号,用于区分已经看到的位置,不填服务器默认为0
}
response:
[{
'id': 0,
author: 'w3.org',
url: 'https://media.w3.org/2020/08/ml-workshop/virtual-character-web-meeting.mp4',
desc: 'virtual-character-web-meeting.mp4',
tags: [
'mc', 'course',
],
like: 10000,
comment: 20000,
share: 1000,
}]
```

## 获取直播流

获取直播流数据的接口
```javascript
url: /api/live/getlist
methon: GET
request:
{
id=number // 默认为1即可,直播流的编号,用于区分已经看到的位置,不填服务器默认为0
}
response:
[{
id: 0,
author: 'cctv',
url: 'http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8',
desc: 'cctv - 1',
createdAt: '2021-01-26T19:30:22.420Z',
updatedAt: '2020-01-30T10:24:26.590Z' ,
like: 0,
comment: 0,
share: 0,
}]
```

## Socket.io

```javascript
url: /ws
```

## 报错

```javascript
response:
{
code: 404,
msg: 'url invalid',
request: Object,
}
```
29 changes: 29 additions & 0 deletions server/.autod.conf.js
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions server/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
3 changes: 3 additions & 0 deletions server/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg"
}
42 changes: 42 additions & 0 deletions server/.github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -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 }}
16 changes: 16 additions & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
logs/
npm-debug.log
yarn-error.log
node_modules/
package-lock.json
yarn.lock
coverage/
.idea/
.vscode
temp/
run/
.DS_Store
*.sw*
*.un~
typings/
.nyc_output/
12 changes: 12 additions & 0 deletions server/.travis.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 33 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# tiktok-server

server for tiktok-like webapp

## 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
16 changes: 16 additions & 0 deletions server/app/controller/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const Controller = require('egg').Controller;

class ErrorController extends Controller {
async pageNotFound() {
const { ctx } = this;
ctx.body = {
code: 404,
msg: `url:${ctx.request.url} is invalid`,
request: ctx.request.toJSON(),
};
}
}

module.exports = ErrorController;
12 changes: 12 additions & 0 deletions server/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
async index() {
const { ctx } = this;
ctx.body = 'server runing successful!';
}
}

module.exports = HomeController;
13 changes: 13 additions & 0 deletions server/app/controller/live.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const Controller = require('egg').Controller;

class LiveController extends Controller {
async getLiveList() {
const { ctx } = this;
const ldata = await ctx.service.live.getLiveList(0);
ctx.body = ldata;
}
}

module.exports = LiveController;
13 changes: 13 additions & 0 deletions server/app/controller/video.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const Controller = require('egg').Controller;

class VideoController extends Controller {
async getVideoList() {
const { ctx } = this;
const vdata = await ctx.service.video.getViderList(0);
ctx.body = vdata;
}
}

module.exports = VideoController;
20 changes: 20 additions & 0 deletions server/app/io/controller/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// {app_root}/app/io/controller/default.js
'use strict';

module.exports = app => {
class Controller extends app.Controller {
async rejoin() {
const { ctx } = this;
const message = ctx.args[0];
console.log(ctx.args);
await ctx.socket.emit('res', `Hi! I've got your message: ${message}`);
}
async chat() {
const { ctx } = this;
const message = ctx.args[0];
console.log(ctx.args);
await ctx.socket.emit('res', `Hi! I've got your message: ${message}`);
}
}
return Controller;
};
13 changes: 13 additions & 0 deletions server/app/io/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
// {app_root}/app/io/middleware/connection.js

// eslint-disable-next-line no-unused-vars
module.exports = app => {
return async (ctx, next) => {
// console.log('auth:', ctx.query);
ctx.socket.emit('res', 'connected!');
await next();
// execute when disconnect.
console.log('disconnection!');
};
};
11 changes: 11 additions & 0 deletions server/app/io/middleware/packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
// {app_root}/app/io/middleware/packet.js

// eslint-disable-next-line no-unused-vars
module.exports = app => {
return async (ctx, next) => {
ctx.socket.emit('res', 'packet received!');
console.log('packet:', this);
await next();
};
};
23 changes: 23 additions & 0 deletions server/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

/**
* @param {Egg.Application} app - egg application
*/
module.exports = app => {
const { router, controller, io } = app;
router.get('/', controller.home.index);

// 获取视频列表
router.get('/api/video/getlist', controller.video.getVideoList);

// 获取直播流列表
router.get('/api/live/getlist', controller.live.getLiveList);

// websocket.io
io.of('/ws').route('rejoin', io.controller.default.rejoin);

io.of('/ws').route('chat', io.controller.default.chat);

// 错误url处理
router.get(/^.*$/i, controller.error.pageNotFound);
};
34 changes: 34 additions & 0 deletions server/app/service/live.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const Service = require('egg').Service;

class LiveService extends Service {
// 从数据库获取视频列表
async getLiveList(vid = 0) {
const vdata = [{
id: vid,
author: 'cctv',
url: 'http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8',
desc: 'cctv - 1',
createdAt: '2021-01-26T19:30:22.420Z',
updatedAt: '2020-01-30T10:24:26.590Z',
like: 0,
comment: 0,
share: 0,
}, {
id: vid + 1,
author: 'cctv',
url: 'http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8',
desc: 'cctv - 2',
createdAt: '2021-01-26T19:30:22.420Z',
updatedAt: '2020-01-30T10:24:26.590Z',
like: 0,
comment: 0,
share: 0,
}];
return vdata;
}
}


module.exports = LiveService;
Loading

0 comments on commit 0e78c7b

Please sign in to comment.