-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.nim
53 lines (41 loc) · 1.51 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import prologue
# Handler for / (index) route.
proc handlerIndex(ctx: Context) {.async.} =
resp "This is index page"
# Handler for /about route.
proc handlerAbout(ctx: Context) {.async.} =
resp "This is about page"
# Create Nim sequence to store child routes
const
basicRoutes = @[
pattern("/", handlerIndex),
pattern("about", handlerAbout) # we don't use '/' before 'about'
]
textRoutes = @[
pattern("/articles", proc(ctx: Context) {.async.} = resp "This is articles page"),
pattern("/post", proc(ctx: Context) {.async.} = resp "This is post page"),
]
userRoutes = @[
pattern("/login", (proc(ctx: Context) {.async.} = resp "This is login page"), @[HttpGet, HttpPost]),
pattern("/logout", (proc(ctx: Context) {.async.} = resp "This is logout page"), @[HttpPost])
]
# Create default settings
let settings = newSettings()
# Create instance
var app = newApp(settings = settings)
# Create routes
app.addRoute(basicRoutes, "/")
app.addRoute(textRoutes, "/wiki")
app.addRoute(userRoutes, "/user")
# Run instance
app.run()
#[
You can now access to the following routes:
- http://127.0.0.1:8080/
- http://127.0.0.1:8080/about
- http://127.0.0.1:8080/wiki/articles
- http://127.0.0.1:8080/wiki/post
- http://127.0.0.1:8080/user/login (both GET and POST)
- http://127.0.0.1:8080/user/logout (only POST)
Also note that 'textRoutes' and 'userRoutes' use anonymous handlers (for simplicity and to save space).
]#