Skip to content

Commit

Permalink
Merge pull request #19 from Staketab/dev
Browse files Browse the repository at this point in the history
create config for connect to minascan db
  • Loading branch information
MrFoxogen authored Mar 7, 2024
2 parents a2cbb92 + ef879b9 commit 7c1d577
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/main/java/com/staketab/minanames/config/MinascanDbConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.staketab.minanames.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
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 javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@ConditionalOnProperty(value = "spring.minascan-datasource.enabled", havingValue = "true")
@PropertySource({"classpath:application.yaml"})
@EnableJpaRepositories(
basePackages = "com.staketab.minanames.minascan",
entityManagerFactoryRef = "minascanEntityManager",
transactionManagerRef = "minascanTransactionManager"
)
public class MinascanDbConfig {

private static final String MINASCAN_ENTITIES_PACKAGE = "com.staketab.minanames.minascan";
private static final String MINASCAN_PERSISTENCE_UNIT_NAME = "minascan";

@Value(value = "${hibernate.minascan.hbm2ddl.auto}")
private String dllAuto;
@Value(value = "${hibernate.minascan.show_sql}")
private String showSql;
@Value(value = "${hibernate.minascan.dialect}")
private String dialect;

@Bean
@ConfigurationProperties(prefix = "spring.minascan-datasource")
public DataSource minascanDataSource() {
return DataSourceBuilder.create().build();
}

@Bean
public Map<String, Object> minascanJpaProperties() {
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", dllAuto);
properties.put("hibernate.show_sql", showSql);
properties.put("hibernate.dialect", dialect);
return properties;
}

@Bean
public LocalContainerEntityManagerFactoryBean minascanEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPersistenceUnitName(MINASCAN_PERSISTENCE_UNIT_NAME);
em.setDataSource(minascanDataSource());
em.setPackagesToScan(MINASCAN_ENTITIES_PACKAGE);
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
em.setJpaPropertyMap(minascanJpaProperties());
return em;
}

@Bean
public PlatformTransactionManager minascanTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(minascanEntityManager().getObject());
return transactionManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.staketab.minanames.minascan;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
@ConditionalOnProperty(value = "spring.minascan-datasource.enabled", havingValue = "true")
public interface TransactionRepository extends JpaRepository<UserCommandsEntity, Integer> {

@Query(nativeQuery = true,
value = """
SELECT buc.status as status
FROM user_commands uc
JOIN blocks_user_commands buc ON uc.id = buc.user_command_id
JOIN a_canonical_blocks cb ON cb.id = buc.block_id
WHERE uc.hash = :txHash
""")
String getTxStatusByTxHash(String txHash);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.staketab.minanames.minascan;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;

@Data
@Entity
@Table(name = "user_commands")
public class UserCommandsEntity {

@Id
private int id;
private String type;
private long nonce;
private Long validUntil;
private String memo;
private String hash;

}
13 changes: 13 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ spring:
hikari:
connection-timeout: 60000
maximum-pool-size: 50 # default is 10
minascan-datasource:
enabled: true
jdbc-url: ${MINASCAN_DB_URL}
username: ${MINASCAN_DB_USERNAME}
password: ${MINASCAN_DB_PASSWORD}
driver-class-name: org.postgresql.Driver
hikari:
maximum-pool-size: 2 # default is 10
jpa:
open-in-view: true
generate-ddl: true
Expand All @@ -39,6 +47,11 @@ hibernate:
auto: update
show_sql: false
batch_size: 50
minascan:
hbm2ddl:
auto: update
show_sql: false
dialect: org.hibernate.dialect.PostgreSQLDialect

springdoc.swagger-ui:
doc-expansion: none
Expand Down
34 changes: 34 additions & 0 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/application.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/application.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>150MB</maxFileSize>
</triggeringPolicy>

<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
<append>false</append>
</appender>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/>
</root>

</configuration>

0 comments on commit 7c1d577

Please sign in to comment.