Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

구조 변경 작업 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ dependencies {
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}

runtimeOnly('com.h2database:h2')

compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
compile group: 'commons-io', name: 'commons-io', version: '2.4'
}

test {
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = 'recordary'
rootProject.name = 'recordary'
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

@SpringBootApplication
public class RecordaryApplication {

public static void main(String[] args) {
SpringApplication.run(RecordaryApplication.class, args);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fairy_pitt.recordary.model;
package com.fairy_pitt.recordary.common.entity;

import lombok.Data;
import javax.persistence.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fairy_pitt.recordary.repository;
package com.fairy_pitt.recordary.common.repository;

import com.fairy_pitt.recordary.model.Users;
import com.fairy_pitt.recordary.common.entity.Users;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.fairy_pitt.recordary.config.database;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
@EnableJpaRepositories(basePackages = "com.fairy_pitt.recordary.common")
@EnableTransactionManagement
@EnableJpaAuditing
@EnableConfigurationProperties({MasterDataSourceConfig.class, SlaveDataSourceConfig.class})
@Profile("!test")
public class DataSourceConfig {
private static final String PROMOTION_PERSISTENCE_UNIT_NAME = "recordary";

@Bean
@Primary
public DataSource dataSource(MasterDataSourceConfig masterDataSourceConfig, SlaveDataSourceConfig slaveDataSourceConfig) {

DataSource masterDataSource = new HikariDataSource(masterDataSourceConfig);
DataSource slaveDataSource = new HikariDataSource(slaveDataSourceConfig);

Map<Object, Object> dataSourceMap = new HashMap<>();
dataSourceMap.put("slave", slaveDataSource);
dataSourceMap.put("master", masterDataSource);

ReplicationRoutingDataSource routingDataSource = new ReplicationRoutingDataSource();
routingDataSource.setTargetDataSources(dataSourceMap);
routingDataSource.setDefaultTargetDataSource(masterDataSource);
routingDataSource.afterPropertiesSet();

return new LazyConnectionDataSourceProxy(routingDataSource);
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaProperties jpaProperties) {

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(true);

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setDataSource(dataSource);
factory.setPackagesToScan("com.fairy_pitt.recordary.common");
factory.setPersistenceUnitName(PROMOTION_PERSISTENCE_UNIT_NAME);

Properties properties = new Properties();
HibernateSettings hibernateSettings = new HibernateSettings()
.ddlAuto(() -> "none");
properties.putAll(new HibernateProperties().determineHibernateProperties(jpaProperties.getProperties(), hibernateSettings));
factory.setJpaProperties(properties);

return factory;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.fairy_pitt.recordary.config.database;

import com.zaxxer.hikari.HikariConfig;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;

@Getter
@Setter
@Configuration
@PropertySource("classpath:/database/database-${spring.profiles.active}.properties")
@ConfigurationProperties(prefix = "datasource.master")
@Profile("!test")
public class MasterDataSourceConfig extends HikariConfig { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.fairy_pitt.recordary.config.database;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.transaction.support.TransactionSynchronizationManager;

@Slf4j
public class ReplicationRoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
log.info("slave is selected!!!");
return "slave";
}

log.info("master is selected!!!");
return "master";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.fairy_pitt.recordary.config.database;

import com.zaxxer.hikari.HikariConfig;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;

@Getter
@Setter
@Configuration
@PropertySource("classpath:/database/database-${spring.profiles.active}.properties")
@ConfigurationProperties(prefix = "datasource.slave")
@Profile("!test")
public class SlaveDataSourceConfig extends HikariConfig { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.fairy_pitt.recordary.config.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

@Slf4j
@ControllerAdvice
public class ExceptionAdvice {
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String handleDefaultException(Exception ex) {
log.error("{}", ex.toString(), ex);

// return BaseResponse.error(ex.getMessage());
return ex.getMessage();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.fairy_pitt.recordary.config.helper;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class JsonHelper {
private static final ObjectMapper MAPPER = new ObjectMapper();

static {
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// QuattroServiceAdaptor.getOrderCompleteInfo()의 approvedAt serialize할때 사용
MAPPER.registerModule(createLocalDateTimeSerializeModule());
}

// LocalDateTime to milliseconds
private static JavaTimeModule createLocalDateTimeSerializeModule() {
JavaTimeModule javaTimeModule = new JavaTimeModule();

javaTimeModule.addSerializer(LocalDateTime.class, new JsonSerializer<LocalDateTime>() {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeNumber(value.atZone(ZoneOffset.UTC).toInstant().toEpochMilli());
}
});

return javaTimeModule;
}

/**
* JsonNode를 Class로 반환
*
* @@param JsonNode. Class
* @@return Class
*/
public static <T> T getClassByJsonNode(JsonNode j, Class<T> t) {
return MAPPER.convertValue(j, t);
}

public static JsonNode jsonNodeFromObject(Object object) {
return MAPPER.convertValue(object, JsonNode.class);
}

public static JsonNode convertStringToJsonNode(String plainText) {
try {
return MAPPER.readTree(plainText);
} catch (IOException e) {
log.error("{} : {}", e.getMessage(), plainText, e);
return null;
}
}

public static <T> T convertStringToObject(String plainText, Class<T> t) {
try {
return MAPPER.readValue(plainText, t);
} catch (IOException e) {
log.error("{} : {}", e.getMessage(), plainText, e);
return null;
}
}

public static <T> T convertStringToObject(String plainText, TypeReference t) {
try {
return (T) MAPPER.readValue(plainText, t);
} catch (IOException e) {
log.error("{} : {}", e.getMessage(), plainText, e);
return null;
}
}

public static String getStringFromObject(Object obj) {
try {
return MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
log.error("{} : {}", e.getMessage(), obj, e);
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.fairy_pitt.recordary.config.logging;

import org.apache.commons.io.IOUtils;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class BodyFetchedHttpRequestWrapper extends HttpServletRequestWrapper {
private byte[] bodyData;

public BodyFetchedHttpRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
InputStream is = super.getInputStream();
bodyData = IOUtils.toByteArray(is);
}

@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream bis = new ByteArrayInputStream(bodyData);
return new SimpleServletInputStream(bis);
}
}

class SimpleServletInputStream extends ServletInputStream {
private InputStream is;

SimpleServletInputStream(InputStream bis) {
is = bis;
}

@Override
public int read() throws IOException {
return is.read();
}

@Override
public int read(byte[] b) throws IOException {
return is.read(b);
}

@Override
public boolean isFinished() {
return false;
}

@Override
public boolean isReady() {
return false;
}

@Override
public void setReadListener(final ReadListener readListener) {

}
}
Loading