From 4f81df006f99ff06088ea2610a6580ed2d30e290 Mon Sep 17 00:00:00 2001 From: George Lemon Date: Thu, 22 Feb 2024 00:12:53 +0200 Subject: [PATCH] update example app Signed-off-by: George Lemon --- example/app.nim | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/example/app.nim b/example/app.nim index f33b0f1..4d95b33 100644 --- a/example/app.nim +++ b/example/app.nim @@ -7,7 +7,6 @@ from std/httpcore import HttpCode, Http200 from std/net import Port const projectPath = getProjectPath() -let currentYear = now().format("yyyy") template getStaticAsset(path: string) {.dirty.} = let assetsPath = projectPath / "storage" / path @@ -33,20 +32,21 @@ var indent = 2 ) +let currentYear = now().format("yyyy") +let globalData = %*{ + "year": parseInt(currentYear) + } + proc precompileEngine() {.thread.} = - # use a thread precompile Tim Engine in watch mode - # todo browser reload and sync {.gcsafe.}: - timl.precompile(waitThread = true) + timl.precompile(waitThread = true, global = globalData) var thr: Thread[void] createThread(thr, precompileEngine) proc resp(req: Request, view: string, code = Http200, - headers = "Content-Type: text/html", global, local: JsonNode = nil) = - let htmlOutput = timl.render(view, global = %*{ - "year": $(currentYear) - }) + headers = "Content-Type: text/html", local = newJObject()) = + let htmlOutput = timl.render(view, local = local) req.send(code, htmlOutput, headers) proc onRequest(req: Request): Future[void] = @@ -57,6 +57,8 @@ proc onRequest(req: Request): Future[void] = case path of "/": req.resp("index") + of "/about": + req.resp("about") else: if path.startsWith("/assets"): getStaticAsset(path) @@ -65,4 +67,4 @@ proc onRequest(req: Request): Future[void] = echo "http://127.0.0.1:1234" let serverSettings = initSettings(Port(1234), numThreads = 1) -run(onRequest, serverSettings) \ No newline at end of file +run(onRequest, serverSettings)