forked from planety/prologue-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.nim
38 lines (27 loc) · 917 Bytes
/
app.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
import prologue
# Middlewares
proc middlewareA*(): HandlerAsync =
result = proc(ctx: Context) {.async.} =
echo "It's an 'A'"
await switch(ctx)
proc middlewareB*(): HandlerAsync =
result = proc(ctx: Context) {.async.} =
echo "It's a 'B'"
await switch(ctx)
proc middlewareC*(): HandlerAsync =
result = proc(ctx: Context) {.async.} =
echo "It's a 'C'"
await switch(ctx)
# Routes handler
proc pong(ctx: Context) {.async.} =
resp "pong"
# Create default settings
let settings = newSettings()
# Create instance with middleware
var app = newApp(settings = settings, middlewares = @[middlewareA()])
# Create routes with middlewares
app.addRoute("/", pong, middlewares = @[middlewareB(), middlewareC()])
app.addRoute("/only-b", pong, middlewares = @[middlewareB()])
app.addRoute("/only-c", pong, middlewares = @[middlewareC()])
# Run instance
app.run()