Skip to content

Commit 58372d7

Browse files
authored
Merge pull request #126 from filecoin-project/feat/config-defaults
Defaults - environment variables config file
2 parents fee8f92 + dc80d92 commit 58372d7

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

fplus-lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ tokio = { version = "1.32.0", features = ["rt", "macros"] }
2929
uuidv4 = "1.0.0"
3030
rayon = "1.8.0"
3131
log = "0.4.20"
32+
once_cell = "1.19.0"
3233
fplus-database = { path = "../fplus-database", version = "1.0.19"}

fplus-lib/src/config.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
11
use log::warn;
2+
use std::collections::HashMap;
3+
use once_cell::sync::OnceCell;
24

3-
pub fn get_env_var_or_default(key: &str, default: &str) -> String {
5+
pub fn default_env_vars() -> &'static HashMap<&'static str, &'static str> {
6+
static DEFAULTS: OnceCell<HashMap<&'static str, &'static str>> = OnceCell::new();
7+
DEFAULTS.get_or_init(|| {
8+
let mut m = HashMap::new();
9+
m.insert("GITHUB_OWNER", "filecoin-project");
10+
m.insert("GITHUB_REPO", "filecoin-plus-falcon");
11+
m.insert("GITHUB_APP_ID", "826129");
12+
m.insert("GITHUB_INSTALLATION_ID", "47207972");
13+
m.insert("RUST_LOG", "info");
14+
m.insert("RUST_BACKTRACE", "1");
15+
m.insert("DB_URL", "");
16+
m.insert("ALLOCATOR_GOVERNANCE_REPO", "Allocator-Governance-Staging");
17+
m.insert("ALLOCATOR_GOVERNANCE_OWNER", "fidlabs");
18+
m.insert("BOT_USER", "filplus-allocators-staging-bot[bot]");
19+
20+
m
21+
})
22+
}
23+
24+
pub fn get_env_var_or_default(key: &str) -> String {
425
match std::env::var(key) {
526
Ok(val) => val,
627
Err(_) => {
28+
let defaults = default_env_vars();
29+
let default = defaults.get(key).unwrap_or(&"");
730
warn!("{} not set, using default value: {}", key, default);
831
default.to_string()
932
}
1033
}
11-
}
34+
}

fplus-lib/src/core/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ impl LDNApplication {
855855
log::info!("- Application is in a valid state!");
856856
return Ok(true);
857857
}
858-
let bot_user = get_env_var_or_default("BOT_USER", "filplus-allocators-staging-bot[bot]");
858+
let bot_user = get_env_var_or_default("BOT_USER");
859859
if author != bot_user {
860860
log::warn!("- Author is not the bot user");
861861
return Ok(false);
@@ -883,7 +883,7 @@ impl LDNApplication {
883883
let app_state = application_file.lifecycle.get_state();
884884
let active_request_id = application_file.lifecycle.active_request.clone();
885885
let valid_verifier_list = Self::fetch_verifiers(owner.clone(), repo.clone()).await?;
886-
let bot_user = get_env_var_or_default("BOT_USER", "filplus-allocators-staging-bot[bot]");
886+
let bot_user = get_env_var_or_default("BOT_USER");
887887

888888
let res: bool = match app_state {
889889
AppState::Submitted => {

fplus-lib/src/external_services/github.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,21 @@ struct Author {
8888

8989
impl GithubWrapper {
9090
pub fn new(owner: String, repo: String) -> Self {
91-
let app_id = get_env_var_or_default("GITHUB_APP_ID", "826129")
92-
.parse::<u64>()
93-
.unwrap_or_else(|_| {
94-
log::error!("Failed to parse GITHUB_APP_ID, using default");
95-
826129
96-
});
97-
let installation_id = get_env_var_or_default("GITHUB_INSTALLATION_ID", "47207972")
98-
.parse::<u64>()
99-
.unwrap_or_else(|_| {
100-
log::error!("Failed to parse GITHUB_INSTALLATION_ID, using default");
101-
47207972
102-
});
91+
let app_id_str = get_env_var_or_default("GITHUB_APP_ID");
92+
let installation_id_str = get_env_var_or_default("GITHUB_INSTALLATION_ID");
93+
94+
let app_id = app_id_str.parse::<u64>().unwrap_or_else(|_| {
95+
log::error!("Failed to parse GITHUB_APP_ID as u64, using default value");
96+
0
97+
});
98+
99+
let installation_id = installation_id_str.parse::<u64>().unwrap_or_else(|_| {
100+
log::error!("Failed to parse GITHUB_INSTALLATION_ID as u64, using default value");
101+
0
102+
});
103+
103104
let gh_private_key = std::env::var("GH_PRIVATE_KEY").unwrap_or_else(|_| {
104-
log::warn!(
105-
"GH_PRIVATE_KEY not found in .env file, attempting to read from gh-private-key.pem"
106-
);
105+
log::warn!("GH_PRIVATE_KEY not found in .env file, attempting to read from gh-private-key.pem");
107106
std::fs::read_to_string("gh-private-key.pem").unwrap_or_else(|e| {
108107
log::error!("Failed to read gh-private-key.pem. Error: {:?}", e);
109108
std::process::exit(1);

0 commit comments

Comments
 (0)