diff --git a/pennyway-app-external-api/build.gradle b/pennyway-app-external-api/build.gradle index ff72ac957..f482cc3fe 100644 --- a/pennyway-app-external-api/build.gradle +++ b/pennyway-app-external-api/build.gradle @@ -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' } diff --git a/pennyway-app-external-api/src/main/java/kr/co/pennyway/config/SwaggerConfig.java b/pennyway-app-external-api/src/main/java/kr/co/pennyway/config/SwaggerConfig.java new file mode 100644 index 000000000..1a55e052b --- /dev/null +++ b/pennyway-app-external-api/src/main/java/kr/co/pennyway/config/SwaggerConfig.java @@ -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") + } +) +@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); + } + + private Info apiInfo(String activeProfile) { + return new Info() + .title("Pennyway API (" + activeProfile + ")") + .description("지출 관리 SNS 플랫폼 Pennyway API 명세서") + .version("v1.0.0"); + } +} diff --git a/pennyway-app-external-api/src/main/resources/application.yml b/pennyway-app-external-api/src/main/resources/application.yml index 1e3147ec6..a7d2fe067 100644 --- a/pennyway-app-external-api/src/main/resources/application.yml +++ b/pennyway-app-external-api/src/main/resources/application.yml @@ -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: @@ -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 \ No newline at end of file + 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 \ No newline at end of file diff --git a/pennyway-common/src/main/resources/application-common.yml b/pennyway-common/src/main/resources/application-common.yml index a0ff13ad6..b1e7bbcd9 100644 --- a/pennyway-common/src/main/resources/application-common.yml +++ b/pennyway-common/src/main/resources/application-common.yml @@ -8,4 +8,4 @@ spring: spring: config: activate: - on-profile: prod \ No newline at end of file + on-profile: dev \ No newline at end of file diff --git a/pennyway-domain/src/main/resources/application-domain.yml b/pennyway-domain/src/main/resources/application-domain.yml index 7e4f79280..fcf90ab18 100644 --- a/pennyway-domain/src/main/resources/application-domain.yml +++ b/pennyway-domain/src/main/resources/application-domain.yml @@ -2,7 +2,7 @@ spring: profiles: group: local: common - prod: common + dev: common datasource: url: ${DB_URL} @@ -42,7 +42,7 @@ logging: spring: config: activate: - on-profile: prod + on-profile: dev jpa: database: MySQL diff --git a/pennyway-infra/src/main/resources/application-infra.yml b/pennyway-infra/src/main/resources/application-infra.yml index 306c178f1..ee08bd17e 100644 --- a/pennyway-infra/src/main/resources/application-infra.yml +++ b/pennyway-infra/src/main/resources/application-infra.yml @@ -2,7 +2,7 @@ spring: profiles: group: local: common - prod: common + dev: common --- spring: @@ -14,4 +14,4 @@ spring: spring: config: activate: - on-profile: prod \ No newline at end of file + on-profile: dev \ No newline at end of file