Skip to content

Commit 50fa3ac

Browse files
committed
Add a hack to adjust Swagger UI to use reverse different URLs when behind proxy
1 parent ab535dc commit 50fa3ac

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
11
package net.hackyourfuture.coursehub;
22

33
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;
48
import org.springframework.context.annotation.Bean;
59
import org.springframework.context.annotation.Configuration;
610

11+
/**
12+
* This configuration adjusts Swagger URLs when the application is running behind a reverse proxy on Digital Ocean.
13+
*/
714
@Configuration
815
public class SwaggerBehindReverseProxyConfig {
916

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+
1026
@Bean
1127
ServerBaseUrlCustomizer serverBaseUrlCustomizer() {
1228
return (serverBaseUrl, request) -> {
13-
if (serverBaseUrl.contains("coursehub.hyf.dev")) {
29+
if (isBehindDigitalOceanReverseProxy()) {
1430
return serverBaseUrl + "/api";
1531
}
1632
return serverBaseUrl;
1733
};
1834
}
35+
36+
private boolean isBehindDigitalOceanReverseProxy() {
37+
var appDomain = System.getenv("APP_DOMAIN");
38+
return appDomain != null && appDomain.contains("coursehub.hyf.dev");
39+
}
1940
}

0 commit comments

Comments
 (0)