generated from openpeeps/pistachio
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: George Lemon <[email protected]>
- Loading branch information
1 parent
4c64126
commit 20ee756
Showing
2 changed files
with
59 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters