-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_server.main.kts
executable file
·44 lines (34 loc) · 1.14 KB
/
5_server.main.kts
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
#!/usr/bin/env kotlin
/*
* 5. ARGUMENT PASSING - Start a server (useful for mocking dependencies)
*/
@file:DependsOn("com.hexagonkt:http_server_netty:3.6.1")
@file:DependsOn("com.hexagonkt:serialization_jackson_json:3.6.1")
import com.hexagonkt.http.server.netty.serve
import com.hexagonkt.core.logging.logger
import com.hexagonkt.core.media.TEXT_PLAIN
import com.hexagonkt.http.model.ContentType
import com.hexagonkt.http.server.callbacks.FileCallback
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.system.exitProcess
/*
* You can access command line arguments using the 'args' variable
*/
val servedDir = args.firstOrNull() ?: ""
val servedPath: Path = Paths.get("").toRealPath().resolve(servedDir)
logger.info { "Served path: $servedPath" }
serve {
get("/dir/*", FileCallback(servedPath.toFile()))
get("/hello/{name}") {
val name = pathParameters["name"]
ok("Hello, $name!", contentType = ContentType(TEXT_PLAIN))
}
post("/STOP") {
exitProcess(0)
}
}
/*
* If your code creates non daemon threads, the script won't exit automatically at the end
*/
logger.info { "SCRIPT STARTED" }