-
Notifications
You must be signed in to change notification settings - Fork 45
Fix: default value of rate-limit and unnecessary content of access logs. #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
oscar60310
merged 5 commits into
develop
from
fix/ratelimit-default-value-with-access-logs-contents
Sep 15, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
977e858
fix(serve): default value of rate-limit and unnecessary content of au…
kokokuo 916da3e
chore(serve): rename "audit-log" to "access-log".
kokokuo 5099cb4
fix(serve): make other scope logger also hidden the file path and fun…
kokokuo 18a90d7
fix(serve): move the access log middleware logging after other middle…
kokokuo 9efb93e
chore(core): refactor logger to make all defined and non-defined logg…
kokokuo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { | ||
| getLogger, | ||
| LoggerOptions, | ||
| VulcanInternalExtension, | ||
| } from '@vulcan-sql/core'; | ||
| import * as bytes from 'bytes'; | ||
| import { BuiltInMiddleware, KoaContext, Next } from '@vulcan-sql/serve/models'; | ||
|
|
||
| @VulcanInternalExtension('access-log') | ||
| export class AccessLogMiddleware extends BuiltInMiddleware<LoggerOptions> { | ||
| private logger = getLogger({ | ||
| scopeName: 'ACCESS_LOG', | ||
| options: this.getOptions(), | ||
| }); | ||
|
|
||
| public async handle(context: KoaContext, next: Next) { | ||
| if (!this.enabled) return next(); | ||
|
|
||
| const { request: req, response: resp, params } = context; | ||
|
|
||
| const reqSize = req.length ? bytes(req.length).toLowerCase() : 'none'; | ||
| const respSize = resp.length ? bytes(resp.length).toLowerCase() : 'none'; | ||
| this.logger.info( | ||
| `--> ${req.ip} -- "${req.method} ${req.path}" -- size: ${reqSize}` | ||
| ); | ||
| this.logger.info(` -> header: ${JSON.stringify(req.header)}`); | ||
| this.logger.info(` -> query: ${JSON.stringify(req.query)}`); | ||
| this.logger.info(` -> params: ${JSON.stringify(params)}`); | ||
| await next(); | ||
| this.logger.info(`<-- status: ${resp.status} -- size: ${respSize}`); | ||
| this.logger.info(` <- header: ${JSON.stringify(resp.header)}`); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,26 @@ | ||
| import { RateLimit, RateLimitOptions } from 'koa2-ratelimit'; | ||
| import { BuiltInMiddleware, KoaContext, Next } from '@vulcan-sql/serve/models'; | ||
| import { VulcanInternalExtension } from '@vulcan-sql/core'; | ||
| import { VulcanInternalExtension, TYPES as CORE_TYPES } from '@vulcan-sql/core'; | ||
| import { inject } from 'inversify'; | ||
|
|
||
| export { RateLimitOptions }; | ||
|
|
||
| @VulcanInternalExtension('rate-limit') | ||
| export class RateLimitMiddleware extends BuiltInMiddleware<RateLimitOptions> { | ||
| private koaRateLimit = RateLimit.middleware(this.getOptions()); | ||
| private options: RateLimitOptions; | ||
| private koaRateLimitFunc; | ||
| constructor( | ||
| @inject(CORE_TYPES.ExtensionConfig) config: any, | ||
| @inject(CORE_TYPES.ExtensionName) name: string | ||
| ) { | ||
| super(config, name); | ||
| this.options = (this.getOptions() as RateLimitOptions) || { max: 60 }; | ||
| if (!this.options['max']) this.options['max'] = 60; | ||
| this.koaRateLimitFunc = RateLimit.middleware(this.options); | ||
| } | ||
|
|
||
| public async handle(context: KoaContext, next: Next) { | ||
| if (!this.enabled) return next(); | ||
| return this.koaRateLimit(context, next); | ||
| return this.koaRateLimitFunc(context, next); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to print request/response size, but I can't get both of them in lab env, how do we get them to work?
Should we calculate the response size after other middlewares (after next() function)? Response data might be set after them.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @oscar60310, thanks for reviewing and suggesting, I have checked the lab env both, and like you said the request/response size (Content-Length) shows an
undefinedvalue ( Also includesintegrating-testingand myapp.spectest cases inservepackage.So I searching for the reason, and I the request header only have
Content-Lengthwhen HTTP method isPOSTorPUT, becauseContent-Lengthonly calculates payload data size, please check the references below and my test by sample withtypescript-koa-starterto add test api:POST request and return JSON format
GET request and return JSON format
For the response header, as we know, because we use the stream and the header will shows
Transfer-Encoding: chunck, soContent-Lengthwon't work.GET request and return Stream format
POST request and return Stream format
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently I still keep the request and response size, because maybe we will have POST / PUT API in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the survey!