From 42a41da0666e9c0eaebc5c1aa7f665ade0ff0a16 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 4 Feb 2020 16:45:54 +0100 Subject: [PATCH] Add OpenAPI route and server --- .env | 1 + .idea/runConfigurations/Start_OpenAPI.xml | 16 ++++++++++++++++ docker-compose.yml | 8 ++++++++ src/main/kotlin/fr/dcproject/Application.kt | 1 + src/main/kotlin/fr/dcproject/routes/OpenAPI.kt | 18 ++++++++++++++++++ .../kotlin/fr/dcproject/utils/Resources.kt | 7 +++++++ 6 files changed, 51 insertions(+) create mode 100644 .idea/runConfigurations/Start_OpenAPI.xml create mode 100644 src/main/kotlin/fr/dcproject/routes/OpenAPI.kt create mode 100644 src/main/kotlin/fr/dcproject/utils/Resources.kt diff --git a/.env b/.env index 5a1420a..6f094db 100644 --- a/.env +++ b/.env @@ -3,6 +3,7 @@ NAME=dc-project DATABASE_URL=jdbc:postgresql:dc-project APP_PORT=8080 +OPENAPI_PORT=8181 ELASTIC_REST=9200 ELASTIC_NODES=9300 diff --git a/.idea/runConfigurations/Start_OpenAPI.xml b/.idea/runConfigurations/Start_OpenAPI.xml new file mode 100644 index 0000000..fc27ac1 --- /dev/null +++ b/.idea/runConfigurations/Start_OpenAPI.xml @@ -0,0 +1,16 @@ + + + + + + + + + + \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 4b77e66..0108060 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,14 @@ # Add the "-d" flag at the end for detached execution version: '3.7' services: + openapi: + container_name: openapi_${NAME} + image: swaggerapi/swagger-ui + restart: always + ports: + - ${OPENAPI_PORT}:8080 + environment: + URL: "http://localhost:8080" app: container_name: app_${NAME} diff --git a/src/main/kotlin/fr/dcproject/Application.kt b/src/main/kotlin/fr/dcproject/Application.kt index ab8a840..96ec84b 100644 --- a/src/main/kotlin/fr/dcproject/Application.kt +++ b/src/main/kotlin/fr/dcproject/Application.kt @@ -186,6 +186,7 @@ fun Application.module(env: Env = PROD) { commentConstitution(get()) voteArticle(get(), get(), get()) voteConstitution(get()) + definition() } } diff --git a/src/main/kotlin/fr/dcproject/routes/OpenAPI.kt b/src/main/kotlin/fr/dcproject/routes/OpenAPI.kt new file mode 100644 index 0000000..861c010 --- /dev/null +++ b/src/main/kotlin/fr/dcproject/routes/OpenAPI.kt @@ -0,0 +1,18 @@ +package fr.dcproject.routes + +import fr.dcproject.utils.readResource +import io.ktor.application.call +import io.ktor.http.ContentType +import io.ktor.locations.KtorExperimentalLocationsAPI +import io.ktor.response.respondText +import io.ktor.routing.Route +import io.ktor.routing.get +import io.ktor.util.KtorExperimentalAPI + +@KtorExperimentalLocationsAPI +@KtorExperimentalAPI +fun Route.definition() { + get("/") { + call.respondText("/openApi.yaml".readResource(), ContentType("text", "yaml")) + } +} diff --git a/src/main/kotlin/fr/dcproject/utils/Resources.kt b/src/main/kotlin/fr/dcproject/utils/Resources.kt new file mode 100644 index 0000000..fda23ea --- /dev/null +++ b/src/main/kotlin/fr/dcproject/utils/Resources.kt @@ -0,0 +1,7 @@ +package fr.dcproject.utils + +fun String.readResource(callbak: (String) -> Unit = {}): String { + val content = callbak::class.java.getResource(this).readText() + callbak(content) + return content +} \ No newline at end of file