|
1 | 1 | package net.hackyourfuture.coursehub; |
2 | 2 |
|
3 | 3 | import org.springdoc.core.customizers.ServerBaseUrlCustomizer; |
| 4 | +import org.springdoc.core.properties.SpringDocConfigProperties; |
| 5 | +import org.springdoc.core.properties.SwaggerUiConfigProperties; |
| 6 | +import org.springdoc.core.providers.SpringWebProvider; |
| 7 | +import org.springdoc.webmvc.ui.SwaggerWelcomeWebMvc; |
4 | 8 | import org.springframework.context.annotation.Bean; |
5 | 9 | import org.springframework.context.annotation.Configuration; |
6 | 10 |
|
| 11 | +/** |
| 12 | + * This configuration adjusts Swagger URLs when the application is running behind a reverse proxy on Digital Ocean. |
| 13 | + */ |
7 | 14 | @Configuration |
8 | 15 | public class SwaggerBehindReverseProxyConfig { |
9 | 16 |
|
| 17 | + @Bean |
| 18 | + SwaggerWelcomeWebMvc swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties, SpringWebProvider springWebProvider) { |
| 19 | + if (isBehindDigitalOceanReverseProxy()) { |
| 20 | + swaggerUiConfig.setUrl("/api/v3/api-docs"); |
| 21 | + swaggerUiConfig.setConfigUrl("/api/v3/api-docs/swagger-config"); |
| 22 | + } |
| 23 | + return new SwaggerWelcomeWebMvc(swaggerUiConfig, springDocConfigProperties, springWebProvider); |
| 24 | + } |
| 25 | + |
10 | 26 | @Bean |
11 | 27 | ServerBaseUrlCustomizer serverBaseUrlCustomizer() { |
12 | 28 | return (serverBaseUrl, request) -> { |
13 | | - if (serverBaseUrl.contains("coursehub.hyf.dev")) { |
| 29 | + if (isBehindDigitalOceanReverseProxy()) { |
14 | 30 | return serverBaseUrl + "/api"; |
15 | 31 | } |
16 | 32 | return serverBaseUrl; |
17 | 33 | }; |
18 | 34 | } |
| 35 | + |
| 36 | + private boolean isBehindDigitalOceanReverseProxy() { |
| 37 | + var appDomain = System.getenv("APP_DOMAIN"); |
| 38 | + return appDomain != null && appDomain.contains("coursehub.hyf.dev"); |
| 39 | + } |
19 | 40 | } |
0 commit comments