Hideout/hideout-mastodon/templates/homeController.mustache

92 lines
3.2 KiB
Plaintext

package {{basePackage}}
import org.springframework.context.annotation.Bean
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
{{#sourceDocumentationProvider}}
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper
import org.springframework.beans.factory.annotation.Value
import org.springframework.core.io.Resource
import org.springframework.util.StreamUtils
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.GetMapping
{{/sourceDocumentationProvider}}
{{^sourceDocumentationProvider}}
{{#useSwaggerUI}}
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.GetMapping
{{/useSwaggerUI}}
{{/sourceDocumentationProvider}}
{{#reactive}}
import org.springframework.web.reactive.function.server.HandlerFunction
import org.springframework.web.reactive.function.server.RequestPredicates.GET
import org.springframework.web.reactive.function.server.RouterFunction
import org.springframework.web.reactive.function.server.RouterFunctions.route
import org.springframework.web.reactive.function.server.ServerResponse
import java.net.URI
{{/reactive}}
{{#sourceDocumentationProvider}}
import java.nio.charset.Charset
{{/sourceDocumentationProvider}}
/**
* Home redirection to OpenAPI api documentation
*/
@Controller
class HomeController {
{{#useSwaggerUI}}
{{^springDocDocumentationProvider}}
{{#sourceDocumentationProvider}}
private val apiDocsPath = "/openapi.json"
{{/sourceDocumentationProvider}}
{{#springFoxDocumentationProvider}}
private val apiDocsPath = "/v2/api-docs"
{{/springFoxDocumentationProvider}}
{{/springDocDocumentationProvider}}
{{/useSwaggerUI}}
{{#sourceDocumentationProvider}}
private val yamlMapper = YAMLMapper()
@Value("classpath:/openapi.yaml")
private lateinit var openapi: Resource
@Bean
fun openapiContent(): String {
return openapi.inputStream.use {
StreamUtils.copyToString(it, Charset.defaultCharset())
}
}
@GetMapping(value = ["/openapi.yaml"], produces = ["application/vnd.oai.openapi"])
@ResponseBody
fun openapiYaml(): String = openapiContent()
@GetMapping(value = ["/openapi.json"], produces = ["application/json"])
@ResponseBody
fun openapiJson(): Any = yamlMapper.readValue(openapiContent(), Any::class.java)
{{/sourceDocumentationProvider}}
{{#useSwaggerUI}}
{{^springDocDocumentationProvider}}
@GetMapping(value = ["/swagger-config.yaml"], produces = ["text/plain"])
@ResponseBody
fun swaggerConfig(): String = "url: $apiDocsPath\n"
{{/springDocDocumentationProvider}}
{{#reactive}}
@Bean
fun index(): RouterFunction
<ServerResponse> = route(
GET("/"), HandlerFunction
<ServerResponse> {
ServerResponse.temporaryRedirect(URI.create("swagger-ui.html")).build()
})
{{/reactive}}
{{^reactive}}
@RequestMapping("/")
fun index(): String = "redirect:swagger-ui.html"
{{/reactive}}
{{/useSwaggerUI}}
}