race_env/
config.rs

1//! Configuration of application
2
3use std::{fs::File, io::Read, path::PathBuf};
4
5use serde::Deserialize;
6use tracing::info;
7
8#[derive(Deserialize)]
9pub struct FacadeConfig {
10    pub host: String,
11    pub address: String,
12}
13
14#[derive(Deserialize)]
15pub struct SolanaConfig {
16    pub rpc: String,
17    pub keyfile: PathBuf,
18    pub skip_preflight: Option<bool>,
19}
20
21#[derive(Deserialize)]
22pub struct BnbConfig {
23    pub rpc: String,
24    pub keyfile: PathBuf,
25}
26
27#[derive(Deserialize)]
28pub struct TransactorConfig {
29    pub port: u32,
30    pub endpoint: String,
31    pub chain: String,
32    pub address: String,
33    pub reg_addresses: Vec<String>,
34}
35
36#[derive(Deserialize)]
37pub struct Config {
38    pub transactor: Option<TransactorConfig>,
39    pub facade: Option<FacadeConfig>,
40    pub solana: Option<SolanaConfig>,
41    pub bnb: Option<BnbConfig>,
42}
43
44impl Config {
45    pub async fn from_path(path: &PathBuf) -> Config {
46        info!("Load configuration from {:?}", path);
47        let mut buf = Vec::with_capacity(1024);
48        let mut f = File::open(path).expect("Config file not found");
49        f.read_to_end(&mut buf).expect("Failed to read config file");
50        match toml::from_slice(&buf) {
51            Ok(config) => config,
52            Err(e) => {
53                panic!("Invalid config file: {:?}", e.to_string())
54            }
55        }
56    }
57}