Skip to content

Commit

Permalink
update example app
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Feb 21, 2024
1 parent f7eb583 commit 4f81df0
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions example/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] =
Expand All @@ -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)
Expand All @@ -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)
run(onRequest, serverSettings)

0 comments on commit 4f81df0

Please sign in to comment.