98 lines
2.9 KiB
Rust
98 lines
2.9 KiB
Rust
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
const CONFIG_FILE: &str = "config.toml";
|
|
|
|
#[derive(serde::Serialize, serde::Deserialize, Clone)]
|
|
pub struct Config {
|
|
// Backend server configuration
|
|
pub log_file_prefix: String,
|
|
pub host_ip: String,
|
|
pub host_port: u16,
|
|
pub redis_url: String,
|
|
|
|
pub frontend_url: String,
|
|
|
|
// GSD RestAPI configuration
|
|
pub gsd_app_key: String,
|
|
pub gsd_rest_url: String,
|
|
pub gsd_user: String,
|
|
pub gsd_password: String,
|
|
pub gsd_app_names: Vec<String>,
|
|
|
|
pub keycloak: Keycloak,
|
|
}
|
|
|
|
#[derive(serde::Serialize, serde::Deserialize, Clone)]
|
|
pub struct Keycloak {
|
|
pub realm_url: String,
|
|
pub client_id: String,
|
|
pub client_secret: String,
|
|
pub auth_url: String,
|
|
pub token_url: String,
|
|
pub redirect_url: String,
|
|
pub realm: String,
|
|
pub base_url: String,
|
|
}
|
|
|
|
impl Config {
|
|
pub fn get_host_url(&self) -> String {
|
|
format!("{}:{}", self.host_ip, self.host_port)
|
|
}
|
|
}
|
|
|
|
fn get_config_absolute_path() -> PathBuf {
|
|
PathBuf::from(CONFIG_FILE)
|
|
}
|
|
|
|
pub fn load_config() -> Result<Config, Box<dyn std::error::Error>> {
|
|
if fs::exists(get_config_absolute_path())? {
|
|
Ok(toml::from_str(&fs::read_to_string(CONFIG_FILE)?)?)
|
|
} else {
|
|
let config = create_standard_config();
|
|
save_config(&config)?;
|
|
|
|
Ok(config)
|
|
}
|
|
}
|
|
|
|
pub fn generate_log_file_name(prefix: String) -> String {
|
|
format!("{}_{}.log", prefix, chrono::Local::now().format("%Y-%m-%dT%H-%M-%S%.6f%z"))
|
|
}
|
|
|
|
pub fn save_config(config: &Config) -> Result<(), Box<dyn std::error::Error>> {
|
|
fs::write(get_config_absolute_path(), toml::to_string(config)?)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn create_standard_config() -> Config {
|
|
Config {
|
|
log_file_prefix: String::from("delivery_backend"),
|
|
host_ip: String::from("127.0.0.1"),
|
|
host_port: 3000,
|
|
redis_url: String::from("redis://127.0.0.1:6379"),
|
|
gsd_rest_url: String::from("http://127.0.0.1:8334"),
|
|
gsd_app_key: String::from("GSD-RestApi"),
|
|
frontend_url: String::from("http://127.0.0.1:3000"),
|
|
gsd_app_names: vec![String::from("GSD-RestApi")],
|
|
gsd_user: String::from("<GSD-USER>"),
|
|
gsd_password: String::from("<GSD-Password>"),
|
|
|
|
keycloak: Keycloak {
|
|
realm_url: String::from("http://127.0.0.1:8080/auth/realms/master"),
|
|
client_id: String::from("delivery-backend"),
|
|
client_secret: String::from(""),
|
|
realm: String::from("master"),
|
|
base_url: String::from("http://127.0.0.1:8080"),
|
|
auth_url: String::from(
|
|
"http://127.0.0.1:8080/auth/realms/master/protocol/openid-connect/auth",
|
|
),
|
|
token_url: String::from(
|
|
"http://127.0.0.1:8080/auth/realms/master/protocol/openid-connect/token",
|
|
),
|
|
redirect_url: String::from("http://127.0.0.1:3000/callback"),
|
|
},
|
|
}
|
|
}
|