|
| 1 | +package com.example |
| 2 | + |
| 3 | +import io.ktor.annotate.annotate |
| 4 | +import io.ktor.http.* |
| 5 | +import io.ktor.openapi.jsonSchema |
| 6 | +import io.ktor.server.application.* |
| 7 | +import io.ktor.server.plugins.openapi.* |
| 8 | +import io.ktor.server.request.* |
| 9 | +import io.ktor.server.response.* |
| 10 | +import io.ktor.server.routing.* |
| 11 | +import kotlinx.serialization.Serializable |
| 12 | + |
| 13 | +fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args) |
| 14 | + |
| 15 | +fun Application.module() { |
| 16 | + routing { |
| 17 | + /** |
| 18 | + * Hello, world. |
| 19 | + * |
| 20 | + * @response 200 text/plaintext Hello |
| 21 | + */ |
| 22 | + get("/hello") { |
| 23 | + call.respondText("Hello") |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Data back-end. |
| 28 | + */ |
| 29 | + route("/data") { |
| 30 | + |
| 31 | + /** |
| 32 | + * Users endpoint. |
| 33 | + */ |
| 34 | + route("/users") { |
| 35 | + val list = mutableListOf<User>() |
| 36 | + |
| 37 | + /** |
| 38 | + * Get a single user by ID. |
| 39 | + * |
| 40 | + * @path id [ULong] the ID of the user |
| 41 | + * @response 400 The ID parameter is malformatted or missing. |
| 42 | + * @response 404 The user for the given ID does not exist. |
| 43 | + * @response 200 [User] The user found with the given ID. |
| 44 | + */ |
| 45 | + get("/{id}") { |
| 46 | + val id = call.parameters["id"]?.toULongOrNull() |
| 47 | + ?: return@get call.respond(HttpStatusCode.BadRequest) |
| 48 | + val user = list.find { it.id == id } |
| 49 | + ?: return@get call.respond(HttpStatusCode.NotFound) |
| 50 | + call.respond(user) |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Get a list of users. |
| 55 | + * |
| 56 | + * @response 200 The list of items. |
| 57 | + */ |
| 58 | + get("/users") { |
| 59 | + val query = call.parameters["q"] |
| 60 | + val result = if (query != null) { |
| 61 | + list.filter {it.name.contains(query, ignoreCase = true) } |
| 62 | + } else { |
| 63 | + list |
| 64 | + } |
| 65 | + |
| 66 | + call.respond(result) |
| 67 | + }.annotate { |
| 68 | + summary = "Get users" |
| 69 | + description = "Retrieves a list of users." |
| 70 | + parameters { |
| 71 | + query("q") { |
| 72 | + description = "An encoded query" |
| 73 | + required = false |
| 74 | + } |
| 75 | + } |
| 76 | + responses { |
| 77 | + HttpStatusCode.OK { |
| 78 | + description = "A list of users" |
| 79 | + schema = jsonSchema<List<User>>() |
| 80 | + } |
| 81 | + HttpStatusCode.BadRequest { |
| 82 | + description = "Invalid query" |
| 83 | + ContentType.Text.Plain() |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Save a new user. |
| 90 | + * |
| 91 | + * @response 204 The new user was saved. |
| 92 | + */ |
| 93 | + post { |
| 94 | + list += call.receive<User>() |
| 95 | + call.respond(HttpStatusCode.NoContent) |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Delete the user with the given ID. |
| 100 | + * |
| 101 | + * @path id [ULong] the ID of the user to remove |
| 102 | + * @response 400 The ID parameter is malformatted or missing. |
| 103 | + * @response 404 The user for the given ID does not exist. |
| 104 | + * @response 204 The user was deleted. |
| 105 | + */ |
| 106 | + delete("/{id}") { |
| 107 | + val id = call.parameters["id"]?.toULongOrNull() |
| 108 | + ?: return@delete call.respond(HttpStatusCode.BadRequest) |
| 109 | + if (!list.removeIf { it.id == id }) |
| 110 | + return@delete call.respond(HttpStatusCode.NotFound) |
| 111 | + call.respond(HttpStatusCode.NoContent) |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + } |
| 116 | + openAPI( |
| 117 | + path = "/docs", |
| 118 | + swaggerFile = "openapi/generated.json" |
| 119 | + ) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +@Serializable |
| 124 | +data class User(val id: ULong, val name: String) |
0 commit comments