Skip to content

Commit

Permalink
fix(decorators): route decorators usage over mounts (also for #383)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Dec 18, 2024
1 parent 6ac4988 commit a8740fb
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 106 deletions.
11 changes: 6 additions & 5 deletions src/happyx/routing/mounting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ proc findAndReplaceMount*(body: NimNode) =
var mountBody = copy(registeredMounts[$name])
mountBody.findAndReplaceMount()

echo mountBody.toStrLit

var decoratorsOffset = 0

for statement in mountBody:
Expand All @@ -77,11 +79,6 @@ proc findAndReplaceMount*(body: NimNode) =
statement[0] = newLit($route & $statement[0])
elif statement[1].kind in [nnkStrLit, nnkTripleStrLit]:
statement[1] = newLit($route & $statement[1])
# Add mount decorators
for decorator in nextRouteDecorators:
body.insert(i, decorator)
inc offset
echo statement.treeRepr
# Add mount routes
# @Decorator
if statement.kind == nnkPrefix and statement[0] == ident"@":
Expand All @@ -95,6 +92,10 @@ proc findAndReplaceMount*(body: NimNode) =
inc decoratorsOffset
# get / post / etc.
elif statement.kind in [nnkCall, nnkCommand] and statement[0] != ident"mount":
# Add mount decorators
for decorator in nextRouteDecorators:
body.insert(i+decoratorsOffset, decorator)
inc offset
body.insert(i+decoratorsOffset, statement)
inc offset
decoratorsOffset = 0
Expand Down
205 changes: 104 additions & 101 deletions tests/testc16.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ model Generics{JSON}[T]:


mount Profile:
@AuthJWT
@RateLimit
get "/":
## Profile main page
return "Hello, world!"
Expand All @@ -48,7 +50,6 @@ mount Profile:
# echo rateLimits[key]
return "Hello, world"

@Cached(10)
get "/cached-test":
## Profile settings
return "Hello, world"
Expand All @@ -59,109 +60,111 @@ mount Profile:


serve "127.0.0.1", 5000:
@Cached(10)
mount "/profile" -> Profile
"/some":
## Hello, world
return "Hi"
get "/calculate/$left:float/$operator:string/$right?:float":
## Some
echo left
echo right
return fmt"{left + right}"
get "/auth[model:TestModel:json]":
## User authorization
return "Hello, world!"
get "/arrQuery":
## Parses array and simple queries
return {
"arr": queryArr?a,
"val": query?b
}
ws "/Hello":
discard
get "/":
## Set bestFramework to **"HappyX!"** in cookies
##
## ```nim
## echo "Hello, world!"
## ```
##
## Responds "Hello, world!"
echo inCookies
outCookies.add(setCookie("bestFramework", "HappyX!", secure = true, httpOnly = true))
return "Hello, world!"
get "/setStatusCode":
## Responds "Hello, world!" with 404 HttpCode
statusCode = 404
if true:
if true:
for i in 0..1:
if true:
when true:
statusCode = 400
return 1
return "Hello, world!"

# "/some":
# ## Hello, world
# return "Hi"
# get "/calculate/$left:float/$operator:string/$right?:float":
# ## Some
# echo left
# echo right
# return fmt"{left + right}"
# get "/auth[model:TestModel:json]":
# ## User authorization
# return "Hello, world!"
# get "/arrQuery":
# ## Parses array and simple queries
# return {
# "arr": queryArr?a,
# "val": query?b
# }
# ws "/Hello":
# discard
# get "/":
# ## Set bestFramework to **"HappyX!"** in cookies
# ##
# ## ```nim
# ## echo "Hello, world!"
# ## ```
# ##
# ## Responds "Hello, world!"
# echo inCookies
# outCookies.add(setCookie("bestFramework", "HappyX!", secure = true, httpOnly = true))
# return "Hello, world!"
# get "/setStatusCode":
# ## Responds "Hello, world!" with 404 HttpCode
# statusCode = 404
# if true:
# if true:
# for i in 0..1:
# if true:
# when true:
# statusCode = 400
# return 1
# return "Hello, world!"

get "/headers":
outHeaders["Test"] = 10
outHeaders["HappyXHeader"] = "Hello"
return "Hello, world!"
# get "/headers":
# outHeaders["Test"] = 10
# outHeaders["HappyXHeader"] = "Hello"
# return "Hello, world!"

# default values is perSecond=60, fromAll=false
@RateLimit(perSecond = 2, fromAll = true)
get "/test/rate-limit":
outHeaders["Test"] = 10
outHeaders["HappyXHeader"] = "Hello"
return "Hello, world!"

post "/post":
## Creates a new post
return "Hello, world!"

put "/post/$id:int":
## Edits a post
return "Hello, world!"
# # default values is perSecond=60, fromAll=false
# @RateLimit(perSecond = 2, fromAll = true)
# get "/test/rate-limit":
# outHeaders["Test"] = 10
# outHeaders["HappyXHeader"] = "Hello"
# return "Hello, world!"

# post "/post":
# ## Creates a new post
# return "Hello, world!"

# put "/post/$id:int":
# ## Edits a post
# return "Hello, world!"

@Cached # Expires in 60 seconds by default
get "/cached/{i:int}":
await sleepAsync(1000)
if true:
if (query?test) == "hello":
return 100
echo query?one
return i
# @Cached # Expires in 60 seconds by default
# get "/cached/{i:int}":
# await sleepAsync(1000)
# if true:
# if (query?test) == "hello":
# return 100
# echo query?one
# return i

@Cached(120) # Expires in 60 seconds by default
get "/cached/{x}":
await sleepAsync(1000)
if hasKey(query, "key"):
return query["key"]
await sleepAsync(1000)
return x
# @Cached(120) # Expires in 60 seconds by default
# get "/cached/{x}":
# await sleepAsync(1000)
# if hasKey(query, "key"):
# return query["key"]
# await sleepAsync(1000)
# return x

@Cached(expires = 120) # Expires in 60 seconds by default
post "/cached/[m:TestModel]":
await sleepAsync(1000)
return m.username

@AuthBasic
post "/test/basic-auth":
echo username # from @AuthBasic
echo password # from @AuthBasic
return "Hello, {username}!"

# You should install jwt library (https://github.com/yglukhov/nim-jwt)
# to use these decorators
# Authorization: JWT_TOKEN
@AuthJWT(token)
post "/test/jwt":
if token.hasKey("name"):
return "Hello, " & token["name"].node.str
return "who are you???"

# Authorization: Bearer JWT_TOKEN
@AuthBearerJWT(token)
post "/test/jwt-bearer":
if token.hasKey("name"):
return "Hello, " & token["name"].node.str
return "who are you???"
# @Cached(expires = 120) # Expires in 60 seconds by default
# post "/cached/[m:TestModel]":
# await sleepAsync(1000)
# return m.username

# @AuthBasic
# post "/test/basic-auth":
# echo username # from @AuthBasic
# echo password # from @AuthBasic
# return "Hello, {username}!"

# # You should install jwt library (https://github.com/yglukhov/nim-jwt)
# # to use these decorators
# # Authorization: JWT_TOKEN
# @AuthJWT(token)
# post "/test/jwt":
# if token.hasKey("name"):
# return "Hello, " & token["name"].node.str
# return "who are you???"

# # Authorization: Bearer JWT_TOKEN
# @AuthBearerJWT(token)
# post "/test/jwt-bearer":
# if token.hasKey("name"):
# return "Hello, " & token["name"].node.str
# return "who are you???"

0 comments on commit a8740fb

Please sign in to comment.