Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ OpenAPI Swagger config 설정 #10

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pennyway-app-external-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ dependencies {
implementation project(':pennyway-domain')
implementation project(':pennyway-infra')

/* Swagger */
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.4.0'

implementation 'org.springframework.boot:spring-boot-starter-web:3.2.3'
implementation 'org.springframework.boot:spring-boot-starter-validation:3.2.3'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package kr.co.pennyway.config;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.servers.Server;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.ObjectUtils;
import org.springframework.web.filter.ForwardedHeaderFilter;

@Configuration
@OpenAPIDefinition(
servers = {
@Server(url = "${pennyway.domain.local}", description = "Local Server"),
@Server(url = "${pennyway.domain.dev}", description = "Develop Server")
jinlee1703 marked this conversation as resolved.
Show resolved Hide resolved
}
)
@RequiredArgsConstructor
public class SwaggerConfig {
private static final String JWT = "JWT";
private final Environment environment;

@Bean
public OpenAPI openAPI() {
String activeProfile = "";
if (!ObjectUtils.isEmpty(environment.getActiveProfiles()) && environment.getActiveProfiles().length >= 1) {
activeProfile = environment.getActiveProfiles()[0];
}

SecurityRequirement securityRequirement = new SecurityRequirement().addList(JWT);

return new OpenAPI()
.info(apiInfo(activeProfile))
.addServersItem(new io.swagger.v3.oas.models.servers.Server().url(""))
.addSecurityItem(securityRequirement)
.components(securitySchemes());
}

@Bean
ForwardedHeaderFilter forwardedHeaderFilter() {
return new ForwardedHeaderFilter();
}

private Components securitySchemes() {
final var securitySchemeAccessToken = new SecurityScheme()
.name(JWT)
.type(SecurityScheme.Type.HTTP)
.scheme("Bearer")
.bearerFormat("JWT")
.in(SecurityScheme.In.HEADER)
.name("Authorization");

return new Components()
.addSecuritySchemes(JWT, securitySchemeAccessToken);
}
asn6878 marked this conversation as resolved.
Show resolved Hide resolved

private Info apiInfo(String activeProfile) {
return new Info()
.title("Pennyway API (" + activeProfile + ")")
.description("지출 관리 SNS 플랫폼 Pennyway API 명세서")
.version("v1.0.0");
}
}
33 changes: 31 additions & 2 deletions pennyway-app-external-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ spring:
profiles:
group:
local: common, domain, infra
prod: common, domain, infra
dev: common, domain, infra

pennywah:
domain:
local: ${PENNYWAY_DOMAIN_LOCAL}
dev: ${PENNYWAY_DOMAIN_DEV}

jwt:
secret-key:
Expand All @@ -19,8 +24,32 @@ spring:
activate:
on-profile: local

springdoc:
default-consumes-media-type: application/json;charset=UTF-8
default-produces-media-type: application/json;charset=UTF-8
swagger-ui:
path: /swagger-ui
disable-swagger-default-url: true
display-request-duration: true
operations-sorter: alpha
api-docs:
groups:
enabled: true

---
spring:
config:
activate:
on-profile: prod
on-profile: dev

springdoc:
default-consumes-media-type: application/json;charset=UTF-8
default-produces-media-type: application/json;charset=UTF-8
swagger-ui:
path: /swagger-ui
disable-swagger-default-url: true
display-request-duration: true
operations-sorter: alpha
api-docs:
groups:
enabled: true
2 changes: 1 addition & 1 deletion pennyway-common/src/main/resources/application-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ spring:
spring:
config:
activate:
on-profile: prod
on-profile: dev
4 changes: 2 additions & 2 deletions pennyway-domain/src/main/resources/application-domain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spring:
profiles:
group:
local: common
prod: common
dev: common

datasource:
url: ${DB_URL}
Expand Down Expand Up @@ -42,7 +42,7 @@ logging:
spring:
config:
activate:
on-profile: prod
on-profile: dev

jpa:
database: MySQL
Expand Down
4 changes: 2 additions & 2 deletions pennyway-infra/src/main/resources/application-infra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spring:
profiles:
group:
local: common
prod: common
dev: common

---
spring:
Expand All @@ -14,4 +14,4 @@ spring:
spring:
config:
activate:
on-profile: prod
on-profile: dev