Last active
August 29, 2015 14:02
-
-
Save hhariri/6473e73ecbd22e6a9559 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "[email protected]", "Developer", setDate(2005, 12, 10), 3), | |
Person(2, "Joe Smith", "[email protected]", "Marketing", setDate(2007, 11, 3), 2), | |
Person(3, "Jenny Jackson", "[email protected]", "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