Last active
November 23, 2018 08:42
-
-
Save lmller/a13bfc8a21ead71991029c8cdfbd9899 to your computer and use it in GitHub Desktop.
ktor fruit example
This file contains 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
Gists for a blog post on blog.codecentric.de |
This file contains 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
dependencies { | |
... | |
implementation "io.ktor:ktor-auth:1.0.0" | |
} |
This file contains 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
repositories { | |
... | |
jcenter() | |
} | |
dependencies { | |
... | |
implementation "io.ktor:ktor-server-core:1.0.0" | |
implementation "io.ktor:ktor-server-netty:1.0.0" | |
} |
This file contains 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 Application.module() { | |
installGlobalFeatures() | |
val fruitStash = mutableListOf<Fruit>() | |
routing { | |
... | |
} | |
} | |
fun Application.installGlobalFeatures() { | |
install(ContentNegotiation) {...} | |
install(StatusPages) {...} | |
install(Authentication) {...} | |
} |
This file contains 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
dependencies { | |
... | |
implementation "io.ktor:ktor-jackson:1.0.0" | |
} |
This file contains 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
install(Authentication) { | |
basic { | |
validate { (name, password) -> | |
if(name == "lovis" && password == "cc hamburg") { | |
UserIdPrincipal(name) | |
} else { | |
null | |
} | |
} | |
} | |
} | |
... | |
authenticate { | |
post("/fruits") { | |
... | |
} | |
} |
This file contains 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 Application.module() { | |
install(feature = ContentNegotiation) { | |
jackson() | |
} | |
... | |
} |
This file contains 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
exception<IndexOutOfBoundsException> { | |
call.respond(NotFound, "404 Not Found") | |
} |
This file contains 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 Application.module() { | |
install(ContentNegotiation) {...} | |
install(StatusPages) { | |
exception<Throwable> { err -> call.respondText { err.message ?: "Unknown Error" } } | |
} | |
val fruitStash = mutableListOf<Fruit>() | |
routing { | |
... | |
get("/fruits/{index}") { | |
val index = call.parameters["index"]?.toIntOrNull() | |
if(index != null) { | |
val fruit = fruitStash[index] | |
call.respond(fruit) | |
} else { | |
call.respond(HttpStatusCode.NotFound) | |
} | |
} | |
} | |
} |
This file contains 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 Application.module() { | |
install(ContentNegotiation) {...} | |
val fruitStash = mutableListOf<Fruit>() | |
routing { | |
... | |
get("/fruits/{index}") { | |
val index = call.parameters["index"]?.toIntOrNull() | |
if(index != null) { | |
val fruit = fruitStash[index] | |
call.respond(fruit) | |
} else { | |
call.respond(HttpStatusCode.NotFound) | |
} | |
} | |
} | |
} |
This file contains 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
routing { | |
route("/fruits") { | |
authenticate { | |
post { ... } | |
} | |
get { ... } | |
get("/{index}") { ... } | |
} | |
} |
This file contains 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 Application.module() { | |
val fruitStash = mutableListOf<String>() | |
routing { | |
post("/fruits") { | |
fruitStash.add(call.receive<String>()) | |
call.respond(HttpStatusCode.Created) | |
} | |
get("/fruits") { | |
call.respond(fruitStash) | |
} | |
} | |
} |
This file contains 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 main(args: Array<String>) { | |
embeddedServer( | |
factory = Netty, | |
port = 8080, | |
module = Application::module | |
).start(wait = true) | |
} |
This file contains 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 Application.module() { | |
routing { | |
... | |
} | |
} |
This file contains 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
data class Fruit(val name: String) | |
fun Application.module() { | |
val fruitStash = mutableListOf<Fruit>() | |
routing { | |
post("/fruits") { | |
fruitStash.add(call.receive<Fruit>()) | |
call.respond(Created) | |
} | |
get("/fruits") { | |
call.respond(fruitStash) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment