-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from AnkanSaha/dev
Dev
- Loading branch information
Showing
10 changed files
with
165 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ node_modules | |
lib | ||
dist | ||
Cache | ||
a.js |
This file contains 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 |
---|---|---|
|
@@ -17,4 +17,5 @@ CODE_OF_CONDUCT.md | |
CONTRIBUTING.md | ||
LEARN.md | ||
SECURITY.md | ||
.circleci | ||
.circleci | ||
a.js |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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,89 @@ | ||
import { Request, Response, NextFunction } from "express"; // Importing express types | ||
import Storage from "../../Storage Management/ShortStorage.storage"; // Importing ShortStorage | ||
import { TodayDate } from "../../Config/Constant/Middleware.Constant"; // Importing Today's Date | ||
import { StatusCode } from "../../StatusCode/Code"; // Importing Status Code | ||
import { Serve } from "../../Config/outer"; // Importing Serve | ||
|
||
// Create new ShortStorage instance | ||
export const StorageInstance = new Storage( | ||
"Request-Counter-details-for-nodejs", | ||
999999, // 999999 MB = 1TB | ||
"Request-Counter-details-for-nodejs", | ||
); | ||
|
||
// Main middleware function | ||
export default function ( | ||
SaveIP: boolean = true, | ||
SaveUserAgent: boolean = true, | ||
SaveRequestTime: boolean = true, | ||
SaveContentType: boolean = true, | ||
SaveMethod: boolean = true, | ||
) { | ||
return async (Request: Request, Response: Response, Next: NextFunction) => { | ||
const GetPreviousData = await StorageInstance.Get(TodayDate); // Get previous data | ||
|
||
// Check if previous data is available or not | ||
if (GetPreviousData.status === StatusCode.NOT_FOUND) { | ||
const SaveStatus = await StorageInstance.Save(TodayDate, { | ||
TotalRequest: 0, | ||
TotalDetails: [ | ||
{ | ||
RequestDate: TodayDate, | ||
RequestTime: SaveRequestTime ? new Date().getTime() : undefined, | ||
RequestIP: SaveIP ? Request.ip : undefined, | ||
RequestUserAgent: SaveUserAgent | ||
? Request.headers["user-agent"] | ||
: undefined, | ||
RequestContentType: SaveContentType | ||
? Request.headers["content-type"] | ||
: undefined, | ||
RequestMethod: SaveMethod ? Request.method : undefined, | ||
}, | ||
], | ||
}); | ||
|
||
// Check if data is saved or not | ||
SaveStatus.status === StatusCode.OK | ||
? Next() | ||
: Serve.JSON({ | ||
response: Response, | ||
status: false, | ||
message: "Unable to process your request, please try again later", | ||
Title: "Error", | ||
statusCode: StatusCode.INTERNAL_SERVER_ERROR, | ||
data: undefined, | ||
}); | ||
} else { | ||
const UpdateStatus = await StorageInstance.Update(TodayDate, { | ||
TotalRequest: GetPreviousData.Data[0].Data.TotalRequest + 1, | ||
TotalDetails: [ | ||
...GetPreviousData.Data[0].Data.TotalDetails, | ||
{ | ||
RequestDate: TodayDate, | ||
RequestTime: SaveRequestTime ? new Date().getTime() : undefined, | ||
RequestIP: SaveIP ? Request.ip : undefined, | ||
RequestUserAgent: SaveUserAgent | ||
? Request.headers["user-agent"] | ||
: undefined, | ||
RequestContentType: SaveContentType | ||
? Request.headers["content-type"] | ||
: undefined, | ||
RequestMethod: SaveMethod ? Request.method : undefined, | ||
}, | ||
], | ||
}); | ||
|
||
// Check if data is updated or not | ||
UpdateStatus.status === StatusCode.OK | ||
? Next() | ||
: Serve.JSON({ | ||
response: Response, | ||
status: false, | ||
message: "Unable to process your request, please try again later", | ||
Title: "Error", | ||
statusCode: StatusCode.INTERNAL_SERVER_ERROR, | ||
data: undefined, | ||
}); | ||
} | ||
}; | ||
} |