Skip to content

Commit

Permalink
Add version to Swagger Docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
cstroe committed Jan 23, 2019
1 parent 635d2be commit 53ca11b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
/.nb-gradle/

/.data/

src/main/resources/checklister-build.properties
12 changes: 10 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ tasks.resolve {
outputPath = "test"
}

val generateBuildConfigResourceTask by tasks.registering {
File("src/main/resources/checklister-build.properties")
.printWriter().use { out ->
out.println("version=${project.version.toString()}")
}
}

val compileKotlinTask = tasks.named("compileKotlin").get()
compileKotlinTask.dependsOn(generateBuildConfigResourceTask)

dependencies {
// Kotlin
implementation(kotlin("stdlib-jdk8"))
Expand Down Expand Up @@ -130,6 +140,4 @@ dependencies {

// integrationTestCompile("org.seleniumhq.selenium:selenium-java:3.13.0")
// integrationTestCompile("org.seleniumhq.selenium:selenium-remote-driver:3.13.0")

implementation(project("modules:buildconfig"))
}
11 changes: 0 additions & 11 deletions modules/buildconfig/build.gradle.kts

This file was deleted.

3 changes: 1 addition & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
rootProject.name = "checklister"
include("modules:buildconfig")
rootProject.name = "checklister"
13 changes: 6 additions & 7 deletions src/main/kotlin/cloud/cosmin/checklister/config/SwaggerConfig.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cloud.cosmin.checklister.config

import cloud.cosmin.checklister.service.BuildConfigService
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import springfox.documentation.builders.ApiInfoBuilder
Expand All @@ -14,22 +15,20 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2
@EnableSwagger2
class SwaggerConfig {
@Bean
fun checklisterApi(): Docket {
fun checklisterApi(buildConfigService: BuildConfigService): Docket {
return Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.apiInfo(apiInfo(buildConfigService))
.select()
.apis(basePackage("cloud.cosmin.checklister.rest"))
.build()
}

fun apiInfo(): ApiInfo {
val appName = this.javaClass.`package`.implementationTitle
val version = this.javaClass.`package`.implementationVersion
private fun apiInfo(buildConfigService: BuildConfigService): ApiInfo {
return ApiInfoBuilder()
.title("Checklister API")
.title("Checklister")
.description("A RESTful API for lists.")
.contact(Contact("GitHub", "https://github.com/cosmincloud/checklister", null))
.version(version)
.version(buildConfigService.getVersion())
.license("Apache License Version 2.0")
.licenseUrl("https://www.apache.org/licenses/LICENSE-2.0")
.build()
Expand Down
19 changes: 4 additions & 15 deletions src/main/kotlin/cloud/cosmin/checklister/rest/IndexController.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
package cloud.cosmin.checklister.rest

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import springfox.documentation.annotations.ApiIgnore
import cloud.cosmin.checklister.BuildConfig

@RestController
@Controller
@ApiIgnore
class IndexController {
@RequestMapping(path = arrayOf("/"), produces = arrayOf("text/html"))
@RequestMapping(path = arrayOf("/"))
fun index(): String {
val name = BuildConfig.NAME
val version = BuildConfig.VERSION
return """
<html><body>
<h1>${name}</h1>
<h2>${version}</h2>
<p>
<a href="swagger-ui.html">API</a>
</p>
</body></html>
""".trimIndent()
return "redirect:/swagger-ui.html"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cloud.cosmin.checklister.service

import org.springframework.stereotype.Service
import java.util.*

@Service
class BuildConfigService {
fun getVersion(): String {
val stream = javaClass.classLoader
.getResourceAsStream("/checklister-build.properties")

return when(stream) {
null -> "null"
else -> {
val props = Properties()
props.load(stream)
props.getProperty("version")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cloud.cosmin.checklister.service.eventsink

import cloud.cosmin.checklister.service.EventSink
import cloud.cosmin.checklister.service.ItemEvent
import org.apache.logging.log4j.LogManager
import org.springframework.stereotype.Service

@Service
class LoggerEventSink : EventSink {
private val log = LogManager.getLogger(javaClass)

override fun accept(event: ItemEvent) {
log.info("ItemEvent: {}", event)
}
}

0 comments on commit 53ca11b

Please sign in to comment.