this repo has no description
1//! get static and parsed environment variables
2//!
3//! USER_DID is parsed into a jacquard Did
4//! DATABASE_URL is from DATABASE_URL or POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_HOST, and POSTGRES_DATABASE
5//! USER_PDS_URL is resolved from USER_DID if ommited
6//! USER_EXPORT_URL falls back to USER_PDS_URL
7//! USER_SUBSCRIBE_URL falls back to USER_PDS_URL
8
9use jacquard::prelude::IdentityResolver;
10use jacquard::types::string::Did;
11use std::env;
12
13pub const DB_MAX_REQ: usize = 65535;
14
15mod config_value;
16
17use crate::config::config_value::ConfigValue;
18
19// this should be loaded before the program starts any threads
20// if this panics threads that access it will be poisoned
21pub static USER_DID: ConfigValue<Did<'static>> = ConfigValue::new_sync(|| {
22 let Ok(env) = env::var("USER_DID") else {
23 panic!("USER_DID not set");
24 };
25
26 let Ok(did) = Did::new_owned(env) else {
27 panic!("USER_DID was not a valid did")
28 };
29
30 did
31});
32
33pub static DATABASE_URL: ConfigValue<String> = ConfigValue::new_sync(|| {
34 if let Ok(url) = env::var("DATABASE_URL") {
35 return url;
36 }
37
38 let user = env::var("POSTGRES_USER");
39 let db = env::var("POSTGRES_DATABASE").or_else(|_| user.clone());
40 let password = env::var("POSTGRES_PASSWORD");
41 let host = env::var("POSTGRES_HOST");
42
43 if let Ok(user) = user.clone()
44 && let Ok(db) = db.clone()
45 && let Ok(password) = password.clone()
46 && let Ok(host) = host.clone()
47 {
48 format!("postgres://{}:{}@{}/{}", user, password, host, db)
49 } else {
50 let missing = [
51 (user, "USER"),
52 (db, "DATABASE"),
53 (password, "PASSWORD"),
54 (host, "HOST"),
55 ]
56 .iter()
57 .filter_map(|x| {
58 if x.0.is_err() {
59 Some(String::from("POSTGRES_") + x.1)
60 } else {
61 None
62 }
63 })
64 .collect::<Vec<String>>()
65 .join(", ");
66
67 panic!(
68 "Could not generate database url. Missing environment variables {}. Set DATABASE_URL to define the postgres url manually",
69 missing
70 );
71 }
72});
73
74pub static USER_PDS_URL: ConfigValue<String> = ConfigValue::new_async(|| {
75 Box::pin(async {
76 if let Ok(url) = env::var("USER_PDS_URL") {
77 url
78 } else {
79 let resolver = jacquard::identity::PublicResolver::default();
80 resolver
81 .pds_for_did(&self::USER_DID)
82 .await
83 .unwrap()
84 .domain()
85 .unwrap()
86 .to_string()
87 }
88 })
89});
90
91pub static USER_EXPORT_URL: ConfigValue<String> =
92 ConfigValue::new_sync(|| env::var("USER_EXPORT_URL").unwrap_or(USER_PDS_URL.clone()));
93
94pub static USER_SUBSCRIBE_URL: ConfigValue<String> =
95 ConfigValue::new_sync(|| env::var("USER_SUBSCRIBE_URL").unwrap_or(USER_PDS_URL.clone()));