Skip to content

Commit

Permalink
(doc): add example of open api generator
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasForst committed Aug 9, 2022
1 parent d59d86a commit 91ebe9b
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,61 @@ fun Application.minimalExample() {
}
```

## Ktor OpenAPI Generator & Swagger UI

Ktor OpenAPI plugin (with support for Ktor `2.x.y`) that generates OpenAPI from your routes definition and allows you to
host Swagger UI. Hosted on different
repository: [LukasForst/ktor-openapi-generator](https://github.com/LukasForst/ktor-openapi-generator/) as it is a fork
of popular [papsign/Ktor-OpenAPI-Generator](https://github.com/papsign/Ktor-OpenAPI-Generator) with some refactoring and
support for Ktor `2.x.y`.

It supports most of the stuff from Ktor including JWT and Session auth.

```kotlin
/**
* Minimal example of OpenAPI plugin for Ktor.
*/
fun Application.minimalExample() {
// install OpenAPI plugin
install(OpenAPIGen) {
// this automatically servers Swagger UI on /swagger-ui
serveSwaggerUi = true
info {
title = "Minimal Example API"
}
}
// install JSON support
install(ContentNegotiation) {
jackson()
}
// add basic routes for openapi.json and redirect to UI
routing {
// serve openapi.json
get("/openapi.json") {
call.respond(this@routing.application.openAPIGen.api.serialize())
}
// and do redirect to make it easier to remember
get("/swagger-ui") {
call.respondRedirect("/swagger-ui/index.html?url=/openapi.json", true)
}
}
// and now example routing
apiRouting {
route("/example/{name}") {
// SomeParams are parameters (query or path), SomeResponse is what the backend returns and SomeRequest
// is what was passed in the body of the request
post<SomeParams, SomeResponse, SomeRequest> { params, someRequest ->
respond(SomeResponse(bar = "Hello ${params.name}! From body: ${someRequest.foo}."))
}
}
}
}

data class SomeParams(@PathParam("who to say hello") val name: String)
data class SomeRequest(val foo: String)
data class SomeResponse(val bar: String)
```

## Ktor Rate Limiting

A simple library that enables Rate Limiting in Ktor.
Expand Down

0 comments on commit 91ebe9b

Please sign in to comment.