Skip to content
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

patch v4.7.3 #386

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion happyx.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

description = "Macro-oriented asynchronous web-framework written with ♥"
author = "HapticX"
version = "4.7.2"
version = "4.7.3"
license = "MIT"
srcDir = "src"
installExt = @["nim"]
Expand Down
2 changes: 1 addition & 1 deletion src/happyx/core/constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const
# Framework version
HpxMajor* = 4
HpxMinor* = 7
HpxPatch* = 2
HpxPatch* = 3
HpxVersion* = $HpxMajor & "." & $HpxMinor & "." & $HpxPatch


Expand Down
16 changes: 8 additions & 8 deletions src/happyx/routing/decorators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ type
arguments: seq[NimNode]
)
CachedResult* = object
data*: string
headers*: HttpHeaders
statusCode*: HttpCode
cachedData*: string
cachedHeaders*: HttpHeaders
cachedStatusCode*: HttpCode
CachedRoute* = object
create_at*: float
res*: CachedResult
cachedResult*: CachedResult
RateLimitInfo* = object
amount*: int
update_at*: float
Expand Down Expand Up @@ -251,7 +251,7 @@ if rateLimits[key].amount > {perSecond}:
usedVariables.add i[2]

let cachedRoutesResult = newNimNode(nnkDotExpr).add(
newNimNode(nnkBracketExpr).add(ident"cachedRoutes", ident"routeKey"), ident"res"
newNimNode(nnkBracketExpr).add(ident"cachedRoutes", ident"routeKey"), ident"cachedResult"
)
let cachedRoutesCreateAt = newNimNode(nnkDotExpr).add(
newNimNode(nnkBracketExpr).add(ident"cachedRoutes", ident"routeKey"), ident"create_at"
Expand All @@ -270,9 +270,9 @@ if rateLimits[key].amount > {perSecond}:
newCall(
"answer",
ident"req",
newNimNode(nnkDotExpr).add(cachedRoutesResult, ident"data"),
newNimNode(nnkDotExpr).add(cachedRoutesResult, ident"statusCode"),
newNimNode(nnkDotExpr).add(cachedRoutesResult, ident"headers"),
newNimNode(nnkDotExpr).add(cachedRoutesResult, ident"cachedData"),
newNimNode(nnkDotExpr).add(cachedRoutesResult, ident"cachedStatusCode"),
newNimNode(nnkDotExpr).add(cachedRoutesResult, ident"cachedHeaders"),
),
newNimNode(nnkBreakStmt).add(ident"__handleRequestBlock")
)
Expand Down
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
18 changes: 9 additions & 9 deletions src/happyx/ssr/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,14 @@ template answer*(
when declared(thisRouteCanBeCached) and declared(routeKey) and not declared(thisIsCachedResponse):
cachedRoutes[routeKey] = CachedRoute(create_at: cpuTime())
when message is string:
cachedRoutes[routeKey].res = CachedResult(data: message)
cachedRoutes[routeKey].cachedResult = CachedResult(cachedData: message)
else:
cachedRoutes[routeKey].res = CachedResult(data: $message)
cachedRoutes[routeKey].res.statusCode = code
cachedRoutes[routeKey].cachedResult = CachedResult(cachedData: $message)
cachedRoutes[routeKey].cachedResult.cachedStatusCode = code
when useHeaders:
cachedRoutes[routeKey].res.headers = h
cachedRoutes[routeKey].cachedResult.cachedHeaders = h
else:
cachedRoutes[routeKey].res.headers = newHttpHeaders([
cachedRoutes[routeKey].cachedResult.cachedHeaders = newHttpHeaders([
("Content-Type", "text/plain;charset=utf-8")
])

Expand Down Expand Up @@ -420,11 +420,11 @@ template answer*(
when declared(thisRouteCanBeCached) and declared(routeKey) and not declared(thisIsCachedResponse):
cachedRoutes[routeKey] = CachedRoute(create_at: cpuTime())
when message is string:
cachedRoutes[routeKey].res = CachedResult(data: message)
cachedRoutes[routeKey].cachedResult = CachedResult(cachedData: message)
else:
cachedRoutes[routeKey].res = CachedResult(data: $message)
cachedRoutes[routeKey].res.statusCode = code
cachedRoutes[routeKey].res.headers = h
cachedRoutes[routeKey].cachedResult = CachedResult(cachedData: $message)
cachedRoutes[routeKey].cachedResult.cachedStatusCode = code
cachedRoutes[routeKey].cachedResult.cachedHeaders = h

# HTTPX
when enableHttpx or enableBuiltin:
Expand Down
5 changes: 4 additions & 1 deletion 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,7 +60,9 @@ mount Profile:


serve "127.0.0.1", 5000:
@Cached(10)
mount "/profile" -> Profile

"/some":
## Hello, world
return "Hi"
Expand Down
Loading