Skip to content

Instantly share code, notes, and snippets.

@hhariri
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhariri/6473e73ecbd22e6a9559 to your computer and use it in GitHub Desktop.
Save hhariri/6473e73ecbd22e6a9559 to your computer and use it in GitHub Desktop.
fun setDate(year: Int, month: Int, day: Int): Date {
val cal = Calendar.getInstance()
cal.set(year, month - 1, day);
return cal.getTime()
}
data class Customer(val id: Int, val name: String)
val getPersons = routeHandler {
response.send(people)
}
val getPersonById = routeHandler {
val person = people.find { it.id == request.routeParams["id"]?.toInt()}
if (person != null) {
response.send(person)
} else {
response.setStatus(StatusCodes.NotFound)
}
}
val createPerson = routeHandler {
val person = Person(people.count()+1,
request.bodyParams["name"].toString(),
request.bodyParams["email"].toString(),
request.bodyParams["profession"].toString(),
Date(),
1)
people.add(person)
response.setStatus(StatusCodes.Created)
response.location = "/person/${person.id}"
}
val people = arrayListOf<Person>(
Person(1, "Hadi Hariri", "mail@somewhere.com", "Developer", setDate(2005, 12, 10), 3),
Person(2, "Joe Smith", "joe@somewhere.com", "Marketing", setDate(2007, 11, 3), 2),
Person(3, "Jenny Jackson", "jenny@gmail.com", "Non Sleeper", setDate(2011, 6, 3), 1))
fun main(args: Array<String>) {
val appServer = AppServer()
appServer.enableContentNegotiation()
appServer.enableAutoOptions()
appServer.enableCORS(arrayListOf(CORSEntry()))
appServer.get("/hello", {
response.send("hello");
})
appServer.get("/person", getPersons)
appServer.get("/person/:id", getPersonById)
appServer.post("/person", createPerson)
appServer.get("/manual_conneg", {
response.negotiate (
"text/html" with { send("Hello")},
"application/json" with { send("..fdfdfdfd")}
)
})
appServer.start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment