diff --git a/happyx.nimble b/happyx.nimble index 463bfde8..2e9fe3c4 100755 --- a/happyx.nimble +++ b/happyx.nimble @@ -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"] diff --git a/src/happyx/core/constants.nim b/src/happyx/core/constants.nim index 939ba24b..28b9122b 100755 --- a/src/happyx/core/constants.nim +++ b/src/happyx/core/constants.nim @@ -110,7 +110,7 @@ const # Framework version HpxMajor* = 4 HpxMinor* = 7 - HpxPatch* = 2 + HpxPatch* = 3 HpxVersion* = $HpxMajor & "." & $HpxMinor & "." & $HpxPatch diff --git a/src/happyx/routing/decorators.nim b/src/happyx/routing/decorators.nim index 69d9a5cf..afdeda94 100644 --- a/src/happyx/routing/decorators.nim +++ b/src/happyx/routing/decorators.nim @@ -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 @@ -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" @@ -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") ) diff --git a/src/happyx/routing/mounting.nim b/src/happyx/routing/mounting.nim index 3bd2a3f6..9b1654d4 100644 --- a/src/happyx/routing/mounting.nim +++ b/src/happyx/routing/mounting.nim @@ -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: @@ -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"@": @@ -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 diff --git a/src/happyx/ssr/server.nim b/src/happyx/ssr/server.nim index 50cddb8c..fbe10687 100755 --- a/src/happyx/ssr/server.nim +++ b/src/happyx/ssr/server.nim @@ -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") ]) @@ -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: diff --git a/tests/testc16.nim b/tests/testc16.nim index e92e83d6..8645e4a2 100644 --- a/tests/testc16.nim +++ b/tests/testc16.nim @@ -34,6 +34,8 @@ model Generics{JSON}[T]: mount Profile: + @AuthJWT + @RateLimit get "/": ## Profile main page return "Hello, world!" @@ -48,7 +50,6 @@ mount Profile: # echo rateLimits[key] return "Hello, world" - @Cached(10) get "/cached-test": ## Profile settings return "Hello, world" @@ -59,7 +60,9 @@ mount Profile: serve "127.0.0.1", 5000: + @Cached(10) mount "/profile" -> Profile + "/some": ## Hello, world return "Hi"