package com.elab.es.config;

import com.elab.es.client.configuration.ElabESProperties;
import io.github.iamazy.elasticsearch.dsl.jdbc.ElasticConnectionDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @Module TODO
 * @Description TODO
 * @Author liukaixiong
 * @Date 2021/1/29 14:25
 */
@Configuration
public class ElabDBConfiguration {
    String JDBC_DRIVER = "io.github.iamazy.elasticsearch.dsl.jdbc.ElasticDriver";
    String DB_URL = "jdbc:es://localhost:9200/posts?useSSL=false&mode=single";

    @Bean
    public DataSource getDefaultDataSource(ElabESProperties props) throws Exception {
        ElasticConnectionDataSource elasticConnectionDataSource = new ElasticConnectionDataSource(DB_URL, true);
        elasticConnectionDataSource.setDriverClassName(JDBC_DRIVER);
        java.util.Properties info = new java.util.Properties();
        if (props.getUsername() != null) {
            info.put("user", props.getUsername());
        }
        if (props.getPassword() != null) {
            info.put("password", props.getPassword());
        }
        return elasticConnectionDataSource;
    }
}
