Last active
April 10, 2024 10:03
-
-
Save nphausg/b0224e5b5ab6f7f37f86608bf3d261ca 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
| 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