Skip to content

Commit

Permalink
EGGRF-01 Initialization code build.
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry.zhou committed Jan 20, 2023
1 parent b223df0 commit bf3f5f7
Show file tree
Hide file tree
Showing 28 changed files with 831 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .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 .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg"
}
46 changes: 46 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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@5 && npminstall

- name: Continuous Integration
run: npm run ci

- name: Code Coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
12 changes: 12 additions & 0 deletions .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@5 -g
install:
- npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
16 changes: 16 additions & 0 deletions app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

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

class HomeController extends Controller {
async index() {
// const { ctx } = this;
// ctx.body = 'hi, egg';

var userinfo = await this.app.model.User.find()
this.ctx.body = userinfo

}
}

module.exports = HomeController;
99 changes: 99 additions & 0 deletions app/controller/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const Controller = require('egg').Controller
class UserController extends Controller {

// 关注频道
async subscribe() {
const subscribeid = this.ctx.params.subscribeid
const userid = this.ctx.user._id
if (subscribeid == userid) {
this.ctx.throw(403, '不能关注自己')
}

const { Subscribe, User } = this.app.model
var subInfo = await Subscribe.findOne({
user: userid,
channel: subscribeid
})
if (subInfo) {
this.ctx.throw(401, '已经关注')
}
var sub = new Subscribe({
user: userid,
channel: subscribeid
})
var subDb = await sub.save()
if (subDb) {
var subscribeUser = await User.findById(subscribeid)
subscribeUser.subscribeCount++
await subscribeUser.save()
this.ctx.body = subscribeUser
} else {
this.ctx.throw(401, '关注失败')
}
}

async userInfo() {
const registerUserid = this.ctx.user ? this.ctx.user._id : null
const userid = this.ctx.params.userid
const { Subscribe, User } = this.app.model
var isSubscribe = false
if (registerUserid) {
const subscribe = await Subscribe.findOne({
user: registerUserid,
channel: userid
})
if (subscribe) {
isSubscribe = true
}
}

var userInfoDb = await User.findById(userid)
var userInfo = userInfoDb._doc
userInfo.isSubscribe = isSubscribe
this.ctx.body = userInfo
}

async login() {
const userBody = this.ctx.request.body
this.ctx.validate({
email: { type: 'string' },
password: { type: 'string' }
}, userBody)

const user = await this.service.user.findEmail(userBody.email)
if (!user) {
this.ctx.throw(422, '用户未注册')
}
if (this.ctx.helper.md5(userBody.password) !== user.password) {
this.ctx.throw(422, '密码不正确')
}

const token = this.service.user.createToken({ user })
var userinfo = user._doc
delete userinfo.password
this.ctx.body = {
...userinfo,
token
}


}

async create() {
const { ctx } = this

ctx.validate({
username: { type: 'string' },
email: { type: 'string' },
password: { type: 'string' },
})

const userBody = ctx.request.body
if (await this.service.user.findEmail(userBody.email)) {
ctx.throw(422, '邮箱已经存在')
}
const user = await this.service.user.createUser(userBody)
ctx.body = user
}
}
module.exports = UserController
44 changes: 44 additions & 0 deletions app/controller/video.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const Controller = require('egg').Controller
class VideoController extends Controller {
async gethots(){
var tops = await this.service.redishot.tophots(10)
this.ctx.body = tops
}

async createComment() {
const body = this.ctx.request.body
const videoid = this.ctx.params.videoid

this.ctx.validate({
content: { type: 'string' }
}, body)
const { Video, Videocomment } = this.app.model
const video = await Video.findById(videoid)
if (!video) {
this.ctx.throw(404, '视频不存在')
}

const comment = await new Videocomment({
content: body.content,
user: this.ctx.user._id,
video: videoid
}).save()

if(comment){
video.commentCount = await Videocomment.countDocuments({
video:videoid
})
await video.save()
// 增加热度
this.service.redishot.hotInc(videoid,2)
this.ctx.body={
msg:"评论成功"
}
}else{
this.ctx.throw(501,'视频评论失败')
}

}
}

module.exports = VideoController
55 changes: 55 additions & 0 deletions app/controller/vod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const Controller = require('egg').Controller
const RPCClient = require('@alicloud/pop-core').RPCClient

class VodController extends Controller {
async getvodinfo(vodId) {
var client = await this.vodClient()
var res = await client.request('GetPlayInfo', {
VideoId: vodId
}, {})
return res
}

async getvideo() {
var videoid = this.ctx.params.videoid
var dbback = await this.app.model.Video.findById(videoid)
if(dbback){
var videoInfo = dbback._doc
var vodid = videoInfo.vodvideoId
var vodInfo = await this.getvodinfo(vodid)
videoInfo.vod = vodInfo
this.ctx.body = videoInfo
}else{
this.throw(404,'视频不存在')
}
}

async vodClient() {
var regionId = 'cn-shanghai'; // 点播服务接入地域
var client = new RPCClient({//填入AccessKey信息
accessKeyId: 'LTAI5t6N7W3BoSYpmtasXzoo',
accessKeySecret: 'GSedSGNfNDvUOGP1Txz5AnwAxfSsa3',
endpoint: 'http://vod.' + regionId + '.aliyuncs.com',
apiVersion: '2017-03-21'
});

return client;
}

async getvod() {
const query = this.ctx.query
this.ctx.validate({
title: { type: 'string' },
filename: { type: 'string' }
}, query)
var client = await this.vodClient()
const vodback = await client.request('CreateUploadVideo', {
Title: query.title,
FileName: query.filename
}, {})
this.ctx.body = vodback
}

}

module.exports = VodController
4 changes: 4 additions & 0 deletions app/extend/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const crypto = require('crypto')
exports.md5 = str => {
return crypto.createHash('md5').update(str).digest('hex')
}
20 changes: 20 additions & 0 deletions app/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = (options = { requried: true }) => {
return async (ctx, next) => {
let token = ctx.headers.authorization
token = token ? token.split('Bearer ')[1] : null

if (token) {
try {
const data = ctx.service.user.verifyToken(token)
ctx.user = data.user
} catch (error) {
ctx.throw(401, 'token验证失败')
}
} else if (!options.requried) {
await next()
} else {
ctx.throw(401, 'token未传入')
}
await next()
}
}
25 changes: 25 additions & 0 deletions app/middleware/error_handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// app/middleware/error_handler.js
module.exports = () => {
return async function errorHandler(ctx, next) {
try {
await next();
} catch (err) {
// 所有的异常都在 app 上触发一个 error 事件,框架会记录一条错误日志
ctx.app.emit('error', err, ctx);

const status = err.status || 500;
// 生产环境时 500 错误的详细错误内容不返回给客户端,因为可能包含敏感信息
const error =
status === 500 && ctx.app.config.env === 'prod'
? 'Internal Server Error'
: err.message;

// 从 error 对象上读出各个属性,设置到响应中
ctx.body = { error };
if (status === 422) {
ctx.body.detail = err.errors;
}
ctx.status = status;
}
};
};
Loading

0 comments on commit bf3f5f7

Please sign in to comment.