Skip to content

Commit 233ba5f

Browse files
committed
[后端] 优化生产环境插件
1 parent e46073d commit 233ba5f

File tree

9 files changed

+15
-39
lines changed

9 files changed

+15
-39
lines changed

server/build/blogpack.prod.config.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,21 @@ const CheckAuthPlugin = require('../plugins/beforeRestful/checkAuth')
2525

2626
config.plugins.push(
2727
// beforeUseRoutes
28-
new RatelimitPlugin(),
28+
new RatelimitPlugin({
29+
duration: 1000,
30+
errorMessage: 'Slow Down Your Request.',
31+
id: ctx => ctx.ip,
32+
max: 10
33+
}),
2934
new BodyParserPlugin(),
3035
new LogTimePlugin(),
3136
new RestcPlugin(),
37+
3238
// beforeServerStart
3339
new InitUserPlugin(),
3440
new InstallThemePlugin(),
3541
new InitOptionPlugin(),
42+
3643
// beforeRestful
3744
new CheckAuthPlugin()
3845
)

server/plugins/beforeServerStart/initOption.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ const options = require('../../conf/option')
33
const models = require('../../model/mongo')
44

55
module.exports = class {
6-
constructor() {}
7-
86
async beforeServerStart() {
97
for (const option of options) {
108
let key = option.key

server/plugins/beforeServerStart/initUser.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ const config = require('../../conf/config')
33
const models = require('../../model/mongo')
44

55
module.exports = class {
6-
constructor() {}
7-
86
async beforeServerStart() {
97
const count = await models.user.find().count().exec()
108
if (count !== 0) return

server/plugins/beforeServerStart/installTheme.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const resolve = file => path.resolve(__dirname, file)
55
const models = require('../../model/mongo')
66

77
module.exports = class {
8-
constructor() {}
9-
108
async beforeServerStart() {
119
const prefix = '../../theme'
1210
let fileArr = fs.readdirSync(resolve(prefix))

server/plugins/beforeUseRoutes/bodyParser.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const bodyParser = require('koa-bodyparser')
22

33
module.exports = class {
4-
constructor() {}
5-
64
async beforeUseRoutes({ app }) {
75
app.use(bodyParser())
86
}

server/plugins/beforeUseRoutes/logTime.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
const log = require('../../utils/log')
22

33
module.exports = module.exports = class {
4-
constructor(options) {
5-
this.options = options
6-
}
7-
84
async beforeUseRoutes({ app, redis }) {
95
app.use(async (ctx, next) => {
106
const start = new Date()
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
const ratelimit = require('koa-ratelimit')
22

33
module.exports = class {
4-
constructor() {}
4+
constructor(options) {
5+
this.options = options
6+
}
57

68
async beforeUseRoutes({ app, redis }) {
7-
app.use(ratelimit({
8-
db: redis,
9-
duration: 1000,
10-
errorMessage: 'Slow Down Your Request.',
11-
id: ctx => ctx.ip,
12-
max: 10
13-
}))
9+
const config = Object.assign({}, this.options, {
10+
db: redis
11+
})
12+
app.use(ratelimit(config))
1413
}
1514
}

server/plugins/beforeUseRoutes/restc.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const restc = require('restc')
22

33
module.exports = class {
4-
constructor() {}
5-
64
async beforeUseRoutes({ app }) {
75
app.use(restc.koa2())
86
}

server/plugins/index.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)