You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
autoflow-server-mgmt/src/main/java/kr/re/etri/autoflow/swagger/OpenAPIConfig.java

106 lines
3.9 KiB

package kr.re.etri.autoflow.swagger;
11 months ago
import io.swagger.v3.oas.models.Components;
11 months ago
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
11 months ago
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
11 months ago
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import lombok.RequiredArgsConstructor;
11 months ago
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import org.springframework.core.env.Environment;
11 months ago
@Configuration
@RequiredArgsConstructor
11 months ago
public class OpenAPIConfig {
private final Environment environment;
private static final String SECURITY_SCHEME_ACCESS = "cuuva-jwt-cookie";
private static final String SECURITY_SCHEME_REFRESH = "cuuva-jwt-refresh-cookie";
11 months ago
private static final String PRODUCTION_SERVER_URL = "http://cuuva.com:2481/autoflow";
private static final String LOCAL_SERVER_URL = "http://localhost:8080";
11 months ago
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.components(createComponents())
.addSecurityItem(createSecurityRequirement())
.info(createApiInfo())
.servers(createServerList());
}
private static final String SECURITY_SCHEME_BEARER = "bearerAuth";
private Components createComponents() {
return new Components()
.addSecuritySchemes(SECURITY_SCHEME_ACCESS,
new SecurityScheme()
.name("cuuva-jwt")
.bearerFormat("JWT")
.type(SecurityScheme.Type.APIKEY)
.in(SecurityScheme.In.HEADER)
)
.addSecuritySchemes(SECURITY_SCHEME_REFRESH,
new SecurityScheme()
.name("cuuva-jwt-refresh")
.type(SecurityScheme.Type.APIKEY)
.in(SecurityScheme.In.HEADER)
)
.addSecuritySchemes(SECURITY_SCHEME_BEARER,
new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
);
}
private SecurityRequirement createSecurityRequirement() {
return new SecurityRequirement()
.addList(SECURITY_SCHEME_ACCESS)
.addList(SECURITY_SCHEME_REFRESH);
}
private Info createApiInfo() {
return new Info()
.title("Autoflow API")
.description("Autoflow 서비스의 Swagger 문서")
.version("1.0.0")
.contact(new Contact()
.name("API Support")
.email("jhg88@cuuva.com"))
.license(new License()
.name("CUUVA")
.url("http://cuuva.com"));
}
public List<Server> createServerList() {
String[] activeProfiles = environment.getActiveProfiles();
if (activeProfiles.length == 0) {
// 프로필이 없으면 default
return List.of(new Server().url(LOCAL_SERVER_URL).description("Default Local Server"));
}
String profile = activeProfiles[0];
return switch (profile) {
case "local" -> List.of(
new Server().url(LOCAL_SERVER_URL).description("Local Server")
);
case "prod" -> List.of(
new Server().url(PRODUCTION_SERVER_URL).description("Production Server")
);
default -> List.of(
new Server().url(LOCAL_SERVER_URL).description("Default Local Server")
);
};
11 months ago
}
}