Skip to content

Instantly share code, notes, and snippets.

@nphausg
Last active April 10, 2024 10:03
Show Gist options
  • Select an option

  • Save nphausg/b0224e5b5ab6f7f37f86608bf3d261ca to your computer and use it in GitHub Desktop.

Select an option

Save nphausg/b0224e5b5ab6f7f37f86608bf3d261ca to your computer and use it in GitHub Desktop.
import io.ktor.features.*
// ...
fun main() {
private val server by lazy {
embeddedServer(Netty, PORT, watchPaths = emptyList()) {
// configures Cross-Origin Resource Sharing. CORS is needed to make calls from arbitrary
// JavaScript clients, and helps us prevent issues down the line.
install(CORS) {
anyHost()
}
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
})
}
routing {
get("/") {
call.respondText(
text = "Hello!! You are here in ${Build.MODEL}",
contentType = ContentType.Text.Plain
)
}
get("/fruits") {
call.respond(HttpStatusCode.OK, BaseResponse(Cart.sample()))
}
get("/fruits/{id}") {
val id = call.parameters["id"]
val fruit = Database.FRUITS.find { it.id == id }
if (fruit != null) {
call.respond(HttpStatusCode.OK, BaseResponse(fruit))
} else {
call.respond(HttpStatusCode.NotFound)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment