From 20ee756355998c1511e1431e013df00b562133f6 Mon Sep 17 00:00:00 2001 From: George Lemon Date: Thu, 15 Feb 2024 16:47:02 +0200 Subject: [PATCH] update example app Signed-off-by: George Lemon --- example/app.nim | 64 ++++++++++++++++++++++++++++++++++++++++++++----- tim.nimble | 6 +---- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/example/app.nim b/example/app.nim index e6f1d7d..f33b0f1 100644 --- a/example/app.nim +++ b/example/app.nim @@ -1,8 +1,31 @@ import ../src/tim -import std/os +import std/[options, asyncdispatch, macros, + os, strutils, times, json] +import pkg/[httpbeast] + +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 + if fileExists(assetsPath): + var contentType = "Content-Type: " + if assetsPath.endsWith(".svg"): + add contentType, "image/svg+xml" + elif assetsPath.endsWith(".png"): + add contentType, "image/png" + elif assetsPath.endsWith(".js"): + add contentType, "text/javascript" + elif assetsPath.endsWith(".css"): + add contentType, "text/css" + req.send(Http200, readFile(assetsPath), headers = contentType) + return var - engine = newTim( + timl = newTim( src = "templates", output = "storage", basepath = currentSourcePath(), @@ -10,7 +33,36 @@ var indent = 2 ) -engine.precompile() -sleep(40) -let x = engine.render("index") -writeFile(getCurrentDir() / "example" / "preview.html", x) \ No newline at end of file +proc precompileEngine() {.thread.} = + # use a thread precompile Tim Engine in watch mode + # todo browser reload and sync + {.gcsafe.}: + timl.precompile(waitThread = true) + +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) + }) + req.send(code, htmlOutput, headers) + +proc onRequest(req: Request): Future[void] = + {.gcsafe.}: + let path = req.path.get() + case req.httpMethod.get() + of HttpGet: + case path + of "/": + req.resp("index") + else: + if path.startsWith("/assets"): + getStaticAsset(path) + req.send(Http404) + else: req.send(Http500) + +echo "http://127.0.0.1:1234" +let serverSettings = initSettings(Port(1234), numThreads = 1) +run(onRequest, serverSettings) \ No newline at end of file diff --git a/tim.nimble b/tim.nimble index 1c987b9..822e5e0 100755 --- a/tim.nimble +++ b/tim.nimble @@ -6,6 +6,7 @@ description = "A super fast template engine for cool kids!" license = "MIT" srcDir = "src" installExt = @["nim"] +skipDirs = @["example", "editors"] bin = @["tim"] @@ -21,14 +22,9 @@ requires "denim#head" requires "checksums" requires "flatty" requires "supersnappy" -# requires "httpx" -# requires "websocketx" task node, "Build a NODE addon": exec "denim build src/tim.nim --cmake --yes" task example, "Build example": exec "nim c -d:timHotCode --threads:on --mm:arc -o:./bin/app example/app.nim" - -task pexample, "Build example": - exec "nim c -d:timHotCode -d:danger --passC:-flto --threads:on --mm:arc -o:./bin/app example/app.nim" \ No newline at end of file