feat: 优化和修复

1.定时清除锁定
2.优化仓库查询
3.升级包
pull/542/head
chibing.fy 6 years ago
parent 1c05df8537
commit 2c8c3a09e5

@ -43,13 +43,14 @@
"node-fetch": "^2.6.0",
"node-print": "0.0.4",
"nodemailer": "^6.2.1",
"node-schedule": "^1.3.2",
"notevil": "^1.3.1",
"path-to-regexp": "^3.0.0",
"redis": "^2.8.0",
"reflect-metadata": "^0.1.13",
"request": "^2.88.0",
"request-promise": "^4.2.4",
"sequelize": "^5.8.7",
"sequelize": "^5.10.1",
"sequelize-typescript": "^1.0.0-beta.3",
"svg-captcha": "^1.4.0",
"underscore": "^1.9.1",
@ -70,9 +71,11 @@
"@types/mockjs": "^1.0.2",
"@types/node": "^12.0.3",
"@types/nodemailer": "^6.2.0",
"@types/node-schedule": "^1.2.3",
"@types/redis": "^2.8.13",
"@types/request": "^2.48.1",
"@types/request-promise": "^4.1.44",
"@types/sequelize": "^4.28.4",
"@types/underscore": "^1.8.18",
"babel-eslint": "^10.0.1",
"chai": "^4.2.0",

@ -1,10 +1,10 @@
import { IConfigOptions } from "../types"
let config: IConfigOptions = {
version: '2.3',
version: 'v2.1.0',
serve: {
port: 8080,
path: "", // 服务context path
path: '',
},
keys: ['some secret hurr'],
session: {

@ -4,7 +4,7 @@ let config: IConfigOptions = {
version: '2.3',
serve: {
port: 8080,
path: "", // 服务context path
path: '',
},
keys: ['some secret hurr'],
session: {

@ -1,11 +1,11 @@
import { IConfigOptions } from "../types"
// 先从环境变量取配置
let config: IConfigOptions = {
let config: IConfigOptions = {
version: '2.3',
serve: {
port: (process.env.EXPOSE_PORT && parseInt(process.env.EXPOSE_PORT)) || 8080,
path: process.env.EXPOSE_PATH || "" , // 服务context path
path: '',
},
keys: ['some secret hurr'],
session: {

@ -100,7 +100,6 @@ router.get('/app/plugin/:repositories', async (ctx) => {
result.push(generatePlugin(protocol, ctx.host, repository))
}
ctx.enco
ctx.type = 'application/x-javascript; charset=utf-8'
ctx.body = result.join('\n')
})
@ -246,7 +245,7 @@ router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => {
}
}
} else if (listMatched.length === 0) {
ctx.body = {isOk: false, errMsg: '未匹配到任何接口 No matched interface'}
ctx.body = { isOk: false, errMsg: '未匹配到任何接口 No matched interface' }
return
} else {
loadDataId = listMatched[0].id
@ -302,6 +301,11 @@ router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => {
ctx.type = 'json'
ctx.status = itf.status
ctx.body = JSON.stringify(data, undefined, 2)
const Location = data.Location
if (Location && itf.status === 301) {
ctx.redirect(Location)
return
}
if (itf && itf.url.indexOf('[callback]=') > -1) {
const query = querystring.parse(itf.url.substring(itf.url.indexOf('?') + 1))
const cbName = query['[callback]']

@ -176,26 +176,47 @@ router.get('/repository/get', async (ctx) => {
return
}
const tryCache = await RedisService.getCache(CACHE_KEY.REPOSITORY_GET, ctx.query.id)
let repository: Repository
let repository: Partial<Repository>
if (tryCache) {
repository = JSON.parse(tryCache)
} else {
repository = await Repository.findByPk(ctx.query.id, {
attributes: { exclude: [] },
include: [
QueryInclude.Creator,
QueryInclude.Owner,
QueryInclude.Locker,
QueryInclude.Members,
QueryInclude.Organization,
QueryInclude.RepositoryHierarchy,
QueryInclude.Collaborators
],
order: [
[{ model: Module, as: 'modules' }, 'priority', 'asc'],
[{ model: Module, as: 'modules' }, { model: Interface, as: 'interfaces' }, 'priority', 'asc']
] as any
})
// 分开查询减少
let [repositoryOmitModules, repositoryModules] = await Promise.all([
Repository.findByPk(ctx.query.id, {
attributes: { exclude: [] },
include: [
QueryInclude.Creator,
QueryInclude.Owner,
QueryInclude.Locker,
QueryInclude.Members,
QueryInclude.Organization,
QueryInclude.Collaborators,
QueryInclude.Domains
]
}),
Repository.findByPk(ctx.query.id, {
attributes: { exclude: [] },
include: [QueryInclude.RepositoryHierarchy],
order: [
[{ model: Module, as: 'modules' }, 'priority', 'asc'],
[
{ model: Module, as: 'modules' },
{ model: Interface, as: 'interfaces' },
'priority',
'asc'
]
]
})
])
repository = {
...repositoryOmitModules.toJSON(),
...repositoryModules.toJSON()
}
await RedisService.setCache(
CACHE_KEY.REPOSITORY_GET,
JSON.stringify(repository),
ctx.query.id
)
await RedisService.setCache(CACHE_KEY.REPOSITORY_GET, JSON.stringify(repository), ctx.query.id)
}
ctx.body = {

@ -1,7 +1,7 @@
import * as Router from 'koa-router'
import config from '../config'
let router = new Router({prefix: config.serve.path})
let router = new Router({prefix: config.serve.path})
// index
router.get('/', (ctx) => {

@ -6,4 +6,13 @@ export const COMMON_ERROR_RES = {
ERROR_PARAMS: { isOk: false, errMsg: '参数错误' },
ACCESS_DENY: { isOk: false, errMsg: '您没有访问权限' },
NOT_LOGIN: { isOk: false, errMsg: '您未登陆,或登陆状态过期。请登陆后重试' },
}
}
export enum DATE_CONST {
SECOND = 1000,
MINUTE = 1000 * 60,
HOUR = 1000 * 60 * 60,
DAY = 1000 * 60 * 60 * 24,
MONTH = 1000 * 60 * 60 * 24 * 30,
YEAR = 1000 * 60 * 60 * 24 * 365,
}

@ -29,6 +29,7 @@ export default class UrlUtils {
let re = pathToRegexp(pattern)
return re.test(url)
}
public static getUrlPattern = (pattern: string) => {
pattern = UrlUtils.getRelative(pattern)
return pathToRegexp(pattern)

@ -8,6 +8,7 @@ import * as cors from 'kcors'
import * as bodyParser from 'koa-body'
import router from '../routes'
import config from '../config'
import { startTask } from '../service/task'
const app = new Koa()
let appAny: any = app
@ -54,4 +55,6 @@ app.use(bodyParser({ multipart: true }))
app.use(router.routes())
startTask()
export default app

@ -0,0 +1,28 @@
import * as schedule from 'node-schedule'
import { Interface } from '../models'
import { Op } from 'sequelize'
import { DATE_CONST } from '../routes/utils/const'
export async function startTask() {
console.log(`Starting task: locker check`)
/**
* 5lock
*/
schedule.scheduleJob('*/5 * * * *', async () => {
// tslint:disable-next-line: no-null-keyword
const [num] = await Interface.update({ lockerId: null }, {
where: {
lockerId: {
[Op.gt]: 0,
},
updatedAt: {
[Op.lt]: new Date(Date.now() - DATE_CONST.DAY),
},
},
})
num > 0 && console.log(`cleared ${num} locks`)
})
}

@ -14,8 +14,8 @@ declare interface RedisAndClusterOptions extends RedisOptions {
declare interface IConfigOptions {
version: string
serve: {
port: number,
path: string
port: number
path: string // Context Path
},
keys: string[]
session: {

Loading…
Cancel
Save