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": {
|
"compilerOptions": {
|
||||||
"target": "es6",
|
"target": "esnext",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"outDir": "./lib",
|
"outDir": "./lib",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"emitDecoratorMetadata": true,
|
"emitDecoratorMetadata": true,
|
||||||
"lib": ["ESNext"]
|
"skipLibCheck": true,
|
||||||
},
|
"lib": ["esnext", "dom", "dom.iterable"],
|
||||||
"include": ["src"],
|
"preserveSymlinks": true,
|
||||||
"exclude": ["node_modules", "src/app/resources", "../extollo/lib"]
|
"jsx": "react",
|
||||||
|
"reactNamespace": "JSX"
|
||||||
|
},
|
||||||
|
"include": ["src"]
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue