mysql_fetcher/
config.rs

1/************************************************
2
3   File: config:MySqlConfig
4   Author: Rohit Joshi
5   LICENSE: Apache 2.0
6
7**************************************************/
8use std::collections::HashMap;
9use std::fs::File;
10use std::io::prelude::*;
11use std::str;
12
13#[derive(Serialize, Deserialize, Debug, Clone)]
14pub enum WhereClauseDataType {
15    UnixTime,
16    ID,
17}
18
19#[derive(Serialize, Deserialize, Debug, Clone)]
20pub struct KeyValConfig {
21    pub table_name: String,
22    pub key_format : String,
23    pub val_format : String
24}
25
26impl KeyValConfig {
27    pub fn new(table_name: &str, key_format: &str, val_format: &str) -> KeyValConfig {
28        KeyValConfig {
29            table_name : table_name.to_string(),
30            key_format : key_format.to_string(),
31            val_format : val_format.to_string()
32        }
33    }
34}
35
36impl Default for KeyValConfig {
37    fn default() -> KeyValConfig {
38        KeyValConfig {
39            table_name : "".to_string(),
40            key_format: "{1}pan".to_string(),
41            val_format: "{2}".to_string()
42        }
43    }
44}
45
46#[derive(Serialize, Deserialize, Debug, Clone)]
47pub struct TableConfig {
48    pub select_query: String,
49    pub count_query: String,
50    pub where_clause_type: WhereClauseDataType,
51    pub id_index: usize,
52    pub key_val_pairs: Vec<KeyValConfig>,
53    pub offset: u64,
54    pub last_updated: String,
55    pub total_rows: u64,
56}
57
58impl Default for TableConfig {
59    fn default() -> TableConfig {
60        let mut key_val = Vec::with_capacity(1);
61        key_val.push(KeyValConfig::new("path_t", "{1}pan", "{2}"));
62        key_val.push(KeyValConfig::new("pan_d", "{2}pan", "{3}"));
63        TableConfig {
64            select_query:
65                "select id, hash, token, enc_value from turing_vault_pan where id > {id} "
66                    .to_owned(),
67            count_query: "select COUNT(*) from turing_vault_pan where id > {id} ".to_owned(),
68            where_clause_type: WhereClauseDataType::ID,
69            id_index: 0,
70            key_val_pairs: key_val,
71            offset: 0,
72            last_updated: "".to_string(),
73            total_rows: 0,
74        }
75    }
76}
77
78#[derive(Serialize, Deserialize, Debug, Clone)]
79pub struct DbConfig {
80    pub host: String,
81    pub port: u16,
82    pub user: String,
83    pub password: String,
84    pub db_name: String,
85    pub read_timeout: u64,
86    pub write_timeout: u64,
87    pub tcp_connect_timeout: u64,
88    pub tcp_keepalive_time: u32,
89}
90
91impl Default for DbConfig {
92    fn default() -> DbConfig {
93        DbConfig {
94            host: "localhost".to_owned(),
95            port: 3306,
96            user: "root".to_owned(),
97            password: "root1234".to_owned(),
98            db_name: "turing_db".to_owned(),
99            read_timeout: 60000,
100            write_timeout: 60000,
101            tcp_connect_timeout: 60000,
102            tcp_keepalive_time: 60000,
103        }
104    }
105}
106
107#[derive(Serialize, Deserialize, Debug, Clone)]
108pub struct MySqlConfig {
109    pub enabled: bool,
110    pub db_config: DbConfig,
111    pub tables: HashMap<String, TableConfig>,
112    pub periodic_fetch_duration: u32,
113    pub fetch_limit: usize,
114}
115
116impl Default for MySqlConfig {
117    fn default() -> MySqlConfig {
118        let mut tables = HashMap::with_capacity(1);
119        tables.insert("turing_vault_pan".to_string(), TableConfig::default());
120        MySqlConfig {
121            enabled: true,
122            db_config: DbConfig::default(),
123            tables,
124            periodic_fetch_duration: 10000,
125            fetch_limit: 5000,
126        }
127    }
128}
129
130impl MySqlConfig {
131    pub fn from_file(file: &str) -> MySqlConfig {
132        debug!("Config file: {}", file);
133        let mut f = File::open(file).unwrap();
134        let mut buffer = String::new();
135        f.read_to_string(&mut buffer).unwrap();
136        debug!("Config file Str: {}", buffer.as_str());
137        serde_json::from_str(&buffer).unwrap()
138    }
139
140    pub fn to_string(&self) -> String {
141        serde_json::to_string(&self).unwrap()
142    }
143}