11package net .ironoc .portfolio .controller ;
22
3+ import module java .base ;
4+
35import io .swagger .v3 .oas .annotations .Operation ;
46import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
57import io .swagger .v3 .oas .annotations .responses .ApiResponses ;
8+ import jakarta .servlet .http .HttpServletRequest ;
69import org .springframework .beans .factory .annotation .Autowired ;
710import org .springframework .boot .info .BuildProperties ;
11+ import org .springframework .core .env .Environment ;
812import org .springframework .http .MediaType ;
913import org .springframework .web .bind .annotation .GetMapping ;
1014import org .springframework .web .bind .annotation .RequestMapping ;
1115import org .springframework .web .bind .annotation .RestController ;
16+ import org .springframework .web .servlet .support .ServletUriComponentsBuilder ;
1217
1318@ RestController
1419@ RequestMapping ("/api" )
1520public class VersionController {
1621
22+ private static final String PROD_PROFILE = "prod" ;
23+ private static final String OPEN_API_PATH = "/api-docs" ;
24+ private static final String SWAGGER_UI_PATH = "/swagger-ui-ironoc.html" ;
25+
1726 private final BuildProperties buildProperties ;
27+ private final Environment environment ;
1828
19- public VersionController (@ Autowired BuildProperties buildProperties ) {
29+ public VersionController (@ Autowired BuildProperties buildProperties ,
30+ @ Autowired Environment environment ) {
2031 this .buildProperties = buildProperties ;
32+ this .environment = environment ;
2133 }
2234
2335 @ Operation (summary = "Get ironoc Application Version" ,
@@ -30,4 +42,23 @@ public VersionController(@Autowired BuildProperties buildProperties) {
3042 public String getApplicationVersion () {
3143 return "Version: " + this .buildProperties .getVersion ();
3244 }
45+
46+ @ Operation (summary = "Get iRonoc API documentation endpoint" ,
47+ description = "Returns the fully qualified Swagger/OpenAPI documentation URL based on the active run profile" )
48+ @ ApiResponses (value = {
49+ @ ApiResponse (responseCode = "200" ,
50+ description = "Successfully retrieved iRonoc API documentation endpoint." )
51+ })
52+ @ GetMapping (value = {"/application/openapi-endpoint" }, produces = MediaType .TEXT_PLAIN_VALUE )
53+ public String getOpenApiDocumentationEndpoint (HttpServletRequest request ) {
54+ String endpointPath = isProdProfileActive () ? OPEN_API_PATH : SWAGGER_UI_PATH ;
55+ return ServletUriComponentsBuilder .fromContextPath (request )
56+ .path (endpointPath )
57+ .toUriString ();
58+ }
59+
60+ private boolean isProdProfileActive () {
61+ return Arrays .stream (environment .getActiveProfiles ())
62+ .anyMatch (PROD_PROFILE ::equalsIgnoreCase );
63+ }
3364}
0 commit comments