-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathroute.kt
50 lines (47 loc) · 1.52 KB
/
route.kt
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
package examples.route
import io.vertx.kotlin.lang.*
import java.io.*
fun main(args: Array<String>) {
DefaultVertx {
httpServer(port = 8084, block = Route {
GET("/path") { request ->
bodyJson {
"field1" to "test me not"
}
}
GET(glob("*.html", "*.htm")) { request ->
contentType("text/html")
body {
write("""<!doctype html>
<html>
<head>
<title>${"[^/]+$".toRegex().find(request.path())?.value}</title>
</head>
<body>
<h1>Dynamic page</h1>
<p>The requested path is ${request.path()}</p>
</body>
</html>
""")
}
}
serve("/files", File("src/main/kotlin/io/vertx/kotlin/lang"))
webSocket("/ws") {
handler {
val text = it.toString("UTF-8")
if (text == "exit") {
close()
} else {
eventBus().send(textHandlerID(), text)
}
}
}
otherwise {
setStatus(404, "Resource not found")
body {
write("The requested resource was not found\n")
}
}
})
}
}