Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
나평근 committed Oct 24, 2023
2 parents 5967d34 + cb59ffe commit cfda859
Show file tree
Hide file tree
Showing 32 changed files with 652 additions and 37 deletions.
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ dependencies {


implementation("net.ttddyy:datasource-proxy:1.9")

// // 로그백 의존성
implementation(group = "ca.pjer", name = "logback-awslogs-appender", version = "1.6.0")
// 프로퍼티 제어 in xml
implementation("org.codehaus.janino:janino:3.1.7")
}

tasks.withType<KotlinCompile> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.hanghea99.commerce

import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.data.jpa.repository.config.EnableJpaAuditing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hanghea99.commerce.api.manager.domain

import com.hanghea99.commerce.api.common.domain.core.CoreRequest

data class PostManagerLoginRequest(
val id: String,
val password: String
) : CoreRequest()
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hanghea99.commerce.api.manager.domain

import com.hanghea99.commerce.api.common.domain.core.CoreRequest
import com.hanghea99.commerce.api.common.domain.seller.SellerVo

data class PostManagerSellerPermitRequest (
val sellers: List<SellerVo>
): CoreRequest()

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.hanghea99.commerce.api.common.comp.impl

import com.hanghea99.commerce.api.common.comp.ManagerComponent
import com.hanghea99.commerce.database.entity.ManagerEntity
import com.hanghea99.commerce.database.entity.StoreEntity
import com.hanghea99.commerce.database.repository.ManagerRepository
import com.hanghea99.commerce.database.repository.StoreRepository
import org.springframework.stereotype.Component

@Component
class ManagerManager(var managerRepository: ManagerRepository) : ManagerComponent<ManagerEntity, String>() {
override fun update(entities: List<ManagerEntity>): Long {
return managerRepository.saveAllAndFlush(entities).count().toLong()
}

override fun delete(entityIds: List<String>) {
managerRepository.deleteAllById(entityIds)
}

override fun create(entities: List<ManagerEntity>): List<ManagerEntity> {
return managerRepository.saveAllAndFlush(entities)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.hanghea99.commerce.api.common.comp.impl

import com.hanghea99.commerce.api.common.comp.ReaderComponent
import com.hanghea99.commerce.database.entity.ManagerEntity
import com.hanghea99.commerce.database.entity.QManagerEntity
import com.hanghea99.commerce.database.repository.ManagerRepository
import com.querydsl.core.BooleanBuilder
import com.querydsl.core.types.OrderSpecifier
import com.querydsl.jpa.impl.JPAQueryFactory
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component

@Component
class ManagerReader(
val managerRepository: ManagerRepository,
val jpaQueryFactory: JPAQueryFactory,
) :
ReaderComponent<ManagerEntity?, String>(
) {
private val log = LoggerFactory.getLogger("StoreReader")

val qManagerEntity: QManagerEntity = QManagerEntity("m1")

override fun read(id: String): ManagerEntity {
TODO("Not yet implemented")
}

override fun readAll(ids: List<String>): List<ManagerEntity> {
TODO("Not yet implemented")
}

override fun readAllCount(ids: List<String>): Long {
TODO("Not yet implemented")
}

override fun read(where: BooleanBuilder): ManagerEntity? {
val orders: MutableList<OrderSpecifier<*>> = mutableListOf()
return jpaQueryFactory.selectFrom(qManagerEntity)
.where(where)
.fetchFirst()
}

override fun readAll(
where: BooleanBuilder,
offset: Long,
count: Long,
orders: MutableList<OrderSpecifier<*>>,
): List<ManagerEntity> {
val orders: MutableList<OrderSpecifier<*>> = mutableListOf()
return jpaQueryFactory.selectFrom(qManagerEntity)
.where(where)
.orderBy(*orders.toTypedArray())
.offset(offset)
.limit(count)
.fetch()
}

override fun readAllCount(
where: BooleanBuilder,
): Long {
return jpaQueryFactory.selectFrom(qManagerEntity)
.where(where)
.fetchCount()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.hanghea99.commerce.api.common.comp.impl

import com.hanghea99.commerce.api.common.comp.ManagerComponent
import com.hanghea99.commerce.database.entity.SellerEntity
import com.hanghea99.commerce.database.repository.SellerRepository
import org.springframework.stereotype.Component

@Component
class SellerManager(var sellerRepository: SellerRepository) :
ManagerComponent<SellerEntity, String>() {
override fun update(entities: List<SellerEntity>): Long {
return sellerRepository.saveAllAndFlush(entities).count().toLong()
}

override fun delete(entityIds: List<String>) {
sellerRepository.deleteAllById(entityIds)
}

override fun create(entities: List<SellerEntity>): List<SellerEntity> {
return sellerRepository.saveAllAndFlush(entities)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.hanghea99.commerce.api.common.comp.impl

import com.hanghea99.commerce.api.common.comp.ReaderComponent
import com.hanghea99.commerce.database.entity.QSellerEntity
import com.hanghea99.commerce.database.entity.SellerEntity
import com.hanghea99.commerce.database.repository.SellerRepository
import com.querydsl.core.BooleanBuilder
import com.querydsl.core.types.OrderSpecifier
import com.querydsl.jpa.impl.JPAQueryFactory
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component

@Component
class SellerReader(
val selerRepository: SellerRepository,
val jpaQueryFactory: JPAQueryFactory,
) :
ReaderComponent<SellerEntity, Long>(
) {
private val log = LoggerFactory.getLogger("StoreReader")

val qSellerEntity: QSellerEntity = QSellerEntity("m1")

override fun read(id: Long): SellerEntity {
TODO("Not yet implemented")
}

override fun readAll(ids: List<Long>): List<SellerEntity> {
TODO("Not yet implemented")
}

override fun readAllCount(ids: List<Long>): Long {
TODO("Not yet implemented")
}

override fun read(where: BooleanBuilder): SellerEntity {
TODO("Not yet implemented")
}

override fun readAll(
where: BooleanBuilder,
offset: Long,
count: Long,
orders: MutableList<OrderSpecifier<*>>,
): List<SellerEntity> {
val orders: MutableList<OrderSpecifier<*>> = mutableListOf()
return jpaQueryFactory.selectFrom(qSellerEntity)
.where(where)
.orderBy(*orders.toTypedArray())
.offset(offset)
.limit(count)
.fetch()
}

override fun readAllCount(
where: BooleanBuilder,
): Long {
return jpaQueryFactory.selectFrom(qSellerEntity)
.where(where)
.fetchCount()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.hanghea99.commerce.api.common.domain.seller

import java.time.Instant

data class SellerVo(
var sellerId: String?,
var status: String?,
var password: String?,
var name: String?,
var ssn: String?,
var telecomName: String?,
var phoneNumber: String?,
var companyName: String?,
var businessRegestraionNumber: String?,
var representativeName: String?,
var representativeTelephoneNumber: String?,
var faxNumber: String?,
var businessZipCode: String?,
var businiessAddress: String?,
var createdAt: Instant?,
var updatedAt: Instant?,
var deletedAt: Instant?,
var allowedAt: Instant?,
var blockedAt: Instant?,
)
65 changes: 65 additions & 0 deletions src/main/kotlin/com/hanghea99/commerce/common/HttpLogMessage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.hanghea99.commerce.common

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.http.HttpStatus
import org.springframework.web.util.ContentCachingRequestWrapper
import org.springframework.web.util.ContentCachingResponseWrapper
import java.lang.StringBuilder
import java.nio.charset.Charset

data class HttpLogMessage(
val httpMethod: String,
val requestUri: String,
val httpStatus: HttpStatus,
val clientIp: String,
val elapsedTime: Double,
val headers: String?,
val requestParam: String?,
val requestBody: String?,
val responseBody: String?,
) {
companion object {
val objectMapper = ObjectMapper()

fun createInstance(
requestWrapper: ContentCachingRequestWrapper,
responseWrapper: ContentCachingResponseWrapper,
elapsedTime: Double
): HttpLogMessage {
val headerMap = mutableMapOf<String,String>()
val sb = StringBuilder()
for( headerName in requestWrapper.headerNames){
sb.append("${headerName}=${requestWrapper.getHeader(headerName)} ")
// headerMap[headerName] = requestWrapper.getHeader(headerName)
}



return HttpLogMessage(
httpMethod = requestWrapper.method,
requestUri = requestWrapper.requestURI,
httpStatus = HttpStatus.valueOf(responseWrapper.status),
clientIp = requestWrapper.remoteAddr,
elapsedTime = elapsedTime,
// headers = objectMapper.writeValueAsString(headerMap),
headers = sb.toString(),
requestParam = objectMapper.writeValueAsString(requestWrapper.parameterMap),
requestBody = requestWrapper.contentAsByteArray.toString(charset = Charset.defaultCharset()),
responseBody = responseWrapper.contentAsByteArray.toString(charset = Charset.defaultCharset()),
)
}
}

// 이부분은 각자 취향대로 포멧 정하는 것으로,,,
fun toPrettierLog(): String {
return """
|
|[REQUEST] ${this.httpMethod} ${this.requestUri} ${this.httpStatus} (${this.elapsedTime})
|>> CLIENT_IP: ${this.clientIp}
|>> HEADERS: ${this.headers}
|>> REQUEST_PARAM: ${this.requestParam}
|>> REQUEST_BODY: ${this.requestBody}
|>> RESPONSE_BODY: ${this.responseBody}
""".trimMargin()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.hanghea99.commerce.common

import jakarta.servlet.FilterChain
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.slf4j.MDC
import org.springframework.stereotype.Component
import org.springframework.web.filter.OncePerRequestFilter
import org.springframework.web.util.ContentCachingRequestWrapper
import org.springframework.web.util.ContentCachingResponseWrapper
import java.util.*

@Component
class ReqResLogginFilter : OncePerRequestFilter() {
private val log = logger

companion object {
const val REQUEST_ID = "request_id"
}

override fun doFilterInternal(
request: HttpServletRequest,
response: HttpServletResponse,
filterChain: FilterChain
) {
val cachingRequestWrapper = ContentCachingRequestWrapper(request)
val cachingResponseWrapper = ContentCachingResponseWrapper(response)
val requestId = UUID.randomUUID().toString().substring(0, 8)

MDC.put(REQUEST_ID, requestId)// -> MCD 저장시 멀티 스레드 스레드가 바뀌면서 MDC가 날아갈수 있다 req id 꼬일수 있음
// 올바르게 잘 전달 되는지 테스트 및 확인 필요


val startTime = System.currentTimeMillis()
filterChain.doFilter(cachingRequestWrapper, cachingResponseWrapper)
val end = System.currentTimeMillis()

try {
log.info(HttpLogMessage.createInstance(
requestWrapper = cachingRequestWrapper,
responseWrapper = cachingResponseWrapper,
elapsedTime = (end - startTime) / 1000.0
).toPrettierLog())

cachingResponseWrapper.copyBodyToResponse()
} catch (e: Exception) {
log.error("Logging 실패", e)
}

MDC.remove(REQUEST_ID)
}
}
Loading

0 comments on commit cfda859

Please sign in to comment.