parent
155886eb39
commit
ac1d221f38
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,23 @@
|
||||
import {Cache, http, HTTPStatus, Inject, Injectable, Middleware} from '@extollo/lib'
|
||||
|
||||
/**
|
||||
* RateLimit Middleware
|
||||
* --------------------------------------------
|
||||
* Limits a route to one request / 30 seconds / IP address.
|
||||
*/
|
||||
@Injectable()
|
||||
export class RateLimit extends Middleware {
|
||||
@Inject()
|
||||
protected readonly cache!: Cache
|
||||
|
||||
public async apply() {
|
||||
const slug = `extollo__rate_limit__${this.request.path}__${this.request.address.address}`
|
||||
if ( await this.cache.has(slug) ) {
|
||||
return http(HTTPStatus.TOO_MANY_REQUESTS)
|
||||
}
|
||||
|
||||
const date = new Date()
|
||||
date.setSeconds(date.getSeconds() + 30) // one request / 30 seconds
|
||||
await this.cache.put(slug, slug, date)
|
||||
}
|
||||
}
|
@ -1,14 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"target": "esnext",
|
||||
"module": "commonjs",
|
||||
"declaration": true,
|
||||
"outDir": "./lib",
|
||||
"strict": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"lib": ["ESNext"]
|
||||
"skipLibCheck": true,
|
||||
"lib": ["esnext", "dom", "dom.iterable"],
|
||||
"preserveSymlinks": true,
|
||||
"jsx": "react",
|
||||
"reactNamespace": "JSX"
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules", "src/app/resources", "../extollo/lib"]
|
||||
"include": ["src"]
|
||||
}
|
||||
|
Loading…
Reference in new issue