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 15, 2024
1 parent 4c64126 commit 20ee756
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
64 changes: 58 additions & 6 deletions example/app.nim
Original file line number Diff line number Diff line change
@@ -1,16 +1,68 @@
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(),
minify = true,
indent = 2
)

engine.precompile()
sleep(40)
let x = engine.render("index")
writeFile(getCurrentDir() / "example" / "preview.html", x)
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)
6 changes: 1 addition & 5 deletions tim.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ description = "A super fast template engine for cool kids!"
license = "MIT"
srcDir = "src"
installExt = @["nim"]
skipDirs = @["example", "editors"]
bin = @["tim"]


Expand All @@ -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"

0 comments on commit 20ee756

Please sign in to comment.