-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
44 lines (36 loc) · 1.36 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use tonic_build::compile_protos;
use serde::{Deserialize, Serialize};
use std::io::Write;
#[derive(Serialize, Deserialize)]
pub struct Config {
application_port: i32,
database: DatabaseConfig,
}
#[derive(Serialize, Deserialize)]
pub struct DatabaseConfig {
host: String,
port: i32,
username: String,
password: String,
database_name: String,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
compile_protos("protos/common.proto")?;
compile_protos("protos/endpoints.proto")?;
// Read DB_HOST env var and overwrite the config file based on that
let db_host: Option<&'static str> = option_env!("DB_HOST_ADDR");
let db_host = db_host.unwrap_or("127.0.0.1").to_string();
let config_file = std::fs::File::open("configuration.yaml")?;
let mut config: Config = serde_yaml::from_reader(config_file)?;
config.database.host = db_host;
let config_file = std::fs::File::create("configuration.yaml").expect("File should exist");
serde_yaml::to_writer(&config_file, &config)?;
// Overwrite the .env file needed by sqlx to contain the IP of the DB server
let mut env_file = std::fs::File::create(".env")?;
let database_url = format!(
r#"DATABASE_URL="postgres://postgres:password@{}:5432/ev_accounts_test""#,
config.database.host
);
env_file.write_all(database_url.as_bytes())?;
Ok(())
}