fun main() {
embeddedServer(
Netty,
port = 8080,
host = "127.0.0.1",
watchPaths = listOf("classes")
) { // this: Application
main()
}.start(wait = true)
}
fun Application.main() {
config()
static()
rest()
logRoute()
}
fun Application.static() {
routing { // this: Routing
static("/static") {
resources()
}
...
}
}
fun Application.static() {
routing {
get("/") {
call.respondHtml(HttpStatusCode.OK, HTML::index)
}
static("/static") {
resources()
}
}
}
fun HTML.index() {
head {
title("WebApp")
}
body {
div {
id = "root"
+"React will be here!!"
}
script(src = "/static/webapp.js") {}
}
}
fun Application.config() {
install(ContentNegotiation) {
json()
}
}
fun Application.rest() {
routing {
studentRoutes()
}
}
val students: MutableList<Student> = mutableListOf(
Student("Sheldon", "Cooper"),
Student("Leonard", "Hofstadter"),
Student("Howard", "Wolowitz"),
Student("Penny", "Hofstadter")
)
fun Route.studentRoutes(){
route(Config.studentsPath){ // this: Route
get { // this: PipelineContext
if (students.isEmpty()){
call.respondText(
"No students found",
status = HttpStatusCode.NotFound
)
} else {
call.respond(students)
}
}
}
}