This repository was archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhomeController.mustache
More file actions
88 lines (76 loc) · 2.63 KB
/
homeController.mustache
File metadata and controls
88 lines (76 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package {{configPackage}};
{{^useSpringfox}}
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
{{/useSpringfox}}
import org.springframework.stereotype.Controller;
{{^useSpringfox}}
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
{{/useSpringfox}}
import org.springframework.web.bind.annotation.RequestMapping;
{{^useSpringfox}}
import org.springframework.web.bind.annotation.ResponseBody;
{{/useSpringfox}}
{{#reactive}}
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
{{/reactive}}
{{^useSpringfox}}
import java.io.IOException;
import java.io.InputStream;
{{/useSpringfox}}
{{#reactive}}
import java.net.URI;
{{/reactive}}
{{^useSpringfox}}
import java.nio.charset.Charset;
{{/useSpringfox}}
{{#reactive}}
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
{{/reactive}}
/**
* Home redirection to OpenAPI api documentation
*/
@Controller
public class HomeController {
{{^useSpringfox}}
private static YAMLMapper yamlMapper = new YAMLMapper();
@Value("classpath:/openapi.yaml")
private Resource openapi;
@Bean
public String openapiContent() throws IOException {
try(InputStream is = openapi.getInputStream()) {
return StreamUtils.copyToString(is, Charset.defaultCharset());
}
}
@GetMapping(value = "/openapi.yaml", produces = "application/vnd.oai.openapi")
@ResponseBody
public String openapiYaml() throws IOException {
return openapiContent();
}
@GetMapping(value = "/openapi.json", produces = "application/json")
@ResponseBody
public Object openapiJson() throws IOException {
return yamlMapper.readValue(openapiContent(), Object.class);
}
{{/useSpringfox}}
{{#reactive}}
@Bean
RouterFunction<ServerResponse> index() {
return route(
GET("/"),
req -> ServerResponse.temporaryRedirect(URI.create("{{#useSpringfox}}swagger-ui.html{{/useSpringfox}}{{^useSpringfox}}swagger-ui/index.html?url=../openapi.json{{/useSpringfox}}")).build()
);
}
{{/reactive}}
{{^reactive}}
@RequestMapping("/")
public String index() {
return "redirect:{{#useSpringfox}}swagger-ui.html{{/useSpringfox}}{{^useSpringfox}}swagger-ui/index.html?url=../openapi.json{{/useSpringfox}}";
}
{{/reactive}}
}