tachybase_todo/packages/samples/ratelimit/src/server/index.ts
chenos 883f1e6fd1
fix: eslint (#1759)
* fix: eslint

* fix: eslint --fix

* fix: changelog
2023-04-25 13:12:14 +08:00

39 lines
893 B
TypeScript

import { InstallOptions, Plugin } from '@nocobase/server';
import ratelimit from 'koa-ratelimit';
export class CustomPagePlugin extends Plugin {
beforeLoad() {
const db = new Map();
this.app.use(
ratelimit({
driver: 'memory',
db: db,
duration: 60000,
errorMessage: 'Sometimes You Just Have to Slow Down.',
id: (ctx) => ctx.ip,
headers: {
remaining: 'Rate-Limit-Remaining',
reset: 'Rate-Limit-Reset',
total: 'Rate-Limit-Total',
},
max: 200,
disableHeader: false,
whitelist: (ctx) => {
// some logic that returns a boolean
},
blacklist: (ctx) => {
// some logic that returns a boolean
},
}),
);
}
async load() {}
async install(options: InstallOptions) {
// TODO
}
}
export default CustomPagePlugin;