generated from openpeeps/pistachio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_prologue.nim
56 lines (45 loc) · 1.76 KB
/
example_prologue.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#This example demonstrates using Tim with Prologue
import std/[strutils, times]
import prologue
include ./initializer
#init Settings for prologue
let
settings = newSettings(port = Port(8082))
var app = newApp(settings = settings)
#define your route handling callbacks
proc indexPageHandler(ctx: Context) {.async, gcsafe.} =
let localObjects = %*{
"meta": {
"title": "Tim Engine is Awesome!"
},
"path": "/"
}
{.cast(gcsafe).}: #timl is a global using GC'ed memory and prologue loves it's callbacks to be gc-safe
let indexPage = timl.render(viewName = "index", layoutName = "base", local = localObjects)
resp indexPage
proc aboutPageHandler(ctx: Context) {.async, gcsafe.} =
let localObjects = %*{
"meta": {
"title": "About Tim Engine"
},
"path": "/about"
}
{.cast(gcsafe).}: #timl is a global using GC'ed memory and prologue loves it's callbacks to be gc-safe
let aboutPage = timl.render(viewName = "about", layoutName = "secondary", local = localObjects)
resp aboutPage
proc e404(ctx: Context) {.async, gcsafe.} =
let localObjects = %*{
"meta": {
"title": "Oh, you're a genius!",
"msg": "Oh yes, yes. It's got action, it's got drama, it's got dance! Oh, it's going to be a hit hit hit!"
},
"path": %*(ctx.request.path)
}
{.cast(gcsafe).}: #timl is a global using GC'ed memory and prologue loves it's callbacks to be gc-safe
let e404Page = timl.render(viewName = "error", layoutName = "base", local = localObjects)
resp e404Page
#tell prologue how to handle routes
app.addRoute("/", indexPageHandler)
app.addRoute("/about", aboutPageHandler)
app.registerErrorHandler(Http404, e404)
app.run()