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

feat: Swagger setting hoho #8

Merged
merged 2 commits into from
Jun 23, 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
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
jjwtVersion=0.11.5
mysqlConnectorVersion=8.0.33
springDocOpenApiVersion=2.0.2
4 changes: 4 additions & 0 deletions pic-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tasks.jar {
apply(plugin = "com.google.cloud.tools.jib")

val jjwtVersion: String by project.extra
val springDocOpenApiVersion: String by project.extra

dependencies {
implementation(project(":pic-common"))
Expand All @@ -32,6 +33,9 @@ dependencies {
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
developmentOnly("org.springframework.boot:spring-boot-devtools")
testImplementation("org.springframework.boot:spring-boot-starter-test")

// swagger
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:$springDocOpenApiVersion")
}

configure<JibExtension> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ import com.mashup.pic.auth.applicationService.AuthApplicationService
import com.mashup.pic.auth.controller.dto.LoginRequest
import com.mashup.pic.auth.controller.dto.LoginResponse
import com.mashup.pic.common.ApiResponse
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.Valid

@Tag(name = "인증 컨트둀러")
@RestController
@RequestMapping("/api/v1/auth")
class AuthController(
private val authApplicationService: AuthApplicationService,
) {

@Operation(summary = "둜그인")
@PostMapping("/login")
fun login(
@Valid @RequestBody loginRequest: LoginRequest
): ApiResponse<LoginResponse> {
return ApiResponse.success(authApplicationService.login(loginRequest.toServiceRequest()))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ class SecurityConfig(
private const val MEMBER_ROLE = "MEMBER"
private val WHITELIST_ENDPOINTS = arrayOf(
"/api/v1/auth/login",
"/api/v1/auth/token"
"/api/v1/auth/token",
"/health",
"/swagger-ui/**",
"/v3/api-docs/**"
)
}

Expand Down
59 changes: 59 additions & 0 deletions pic-api/src/main/kotlin/com/mashup/pic/config/SwaggerConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.mashup.pic.config

import com.mashup.pic.constant.ROOT_PACKAGE
import io.swagger.v3.oas.annotations.OpenAPIDefinition
import io.swagger.v3.oas.annotations.info.Info
import io.swagger.v3.oas.models.Components
import io.swagger.v3.oas.models.OpenAPI
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 org.springdoc.core.models.GroupedOpenApi
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
@OpenAPIDefinition(
info = Info(
title = "pic API",
description = "pic API api documents",
version = "v0"
)
)
class SwaggerConfig(
@Value("\${swagger.host.url:http://localhost:8080}")
val serverHostUrl: String
) {

@Bean
fun openApi(): OpenAPI {
val schemeName = "Authorization"
val securityRequirement = SecurityRequirement()
.addList(schemeName)
val components = Components()
.addSecuritySchemes(
schemeName,
SecurityScheme()
.name(schemeName)
.type(SecurityScheme.Type.HTTP)
.`in`(SecurityScheme.In.HEADER)
.scheme("bearer")
.bearerFormat("JWT")
)

return OpenAPI()
.addServersItem(Server().url(serverHostUrl))
.addSecurityItem(securityRequirement)
.components(components)
}

@Bean
fun server(): GroupedOpenApi {
return GroupedOpenApi.builder()
.group("pic-api")
.pathsToExclude("/health")
.packagesToScan(ROOT_PACKAGE)
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mashup.pic.health

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class HealthCheckController {
@GetMapping("/health")
fun healthCheck(): String {
return "ok"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.mashup.pic.constant

const val ROOT_PACKAGE = "com.mashup.pic"
Loading