-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
98 additions
and
10 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
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,48 @@ | ||
## # Decorators 🔌 | ||
## | ||
## Provides convenient wrapper to create route decorators | ||
## | ||
import | ||
macros, | ||
tables, | ||
base64 | ||
|
||
|
||
export base64 | ||
|
||
|
||
type | ||
decoratorImpl* = proc(httpMethods: seq[string], routePath: string, statementList: NimNode) | ||
|
||
|
||
var decorators* {.compileTime.} = newTable[string, decoratorImpl]() | ||
|
||
|
||
proc regDecorator*(decoratorName: string, decorator: decoratorImpl) {.compileTime.} = | ||
decorators[decoratorName] = decorator | ||
|
||
|
||
proc authBasicDecoratorImpl(httpMethods: seq[string], routePath: string, statementList: NimNode) = | ||
statementList.insert(0, parseStmt""" | ||
var (username, password) = ("", "") | ||
if not headers.hasKey("Authorization"): | ||
var statusCode = 401 | ||
return {"response": "failure", "reason": "You should to use Basic authorization!"} | ||
else: | ||
(username, password) = block: | ||
let code = headers["Authorization"].split(" ")[1] | ||
let decoded = base64.decode(code).split(":", 1) | ||
(decoded[0], decoded[1])""" | ||
) | ||
|
||
|
||
proc getUserAgentDecoratorImpl(httpMethods: seq[string], routePath: string, statementList: NimNode) = | ||
statementList.insert(0, parseStmt""" | ||
var userAgent = navigator.userAgent | ||
""" | ||
) | ||
|
||
|
||
static: | ||
regDecorator("AuthBasic", authBasicDecoratorImpl) | ||
regDecorator("GetUserAgent", getUserAgentDecoratorImpl) |
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