35 lines
1.1 KiB
Rust
35 lines
1.1 KiB
Rust
//! Fresh reads of provider configuration and keys for the host harness.
|
|
use sausage_vfs::{Layer, Open};
|
|
use serde::{Deserialize, Serialize};
|
|
use std::{collections::BTreeMap, sync::Arc};
|
|
|
|
pub const PATH: &str = "/etc/sausage/config.yaml";
|
|
#[derive(Default, Deserialize, Serialize)]
|
|
#[serde(default)]
|
|
pub struct Provider {
|
|
#[serde(rename = "type")]
|
|
pub api_type: String,
|
|
pub base_url: String,
|
|
pub api_key: String,
|
|
pub catalog: Option<String>,
|
|
pub models: Vec<serde_json::Value>,
|
|
}
|
|
#[derive(Default, Deserialize)]
|
|
#[serde(default)]
|
|
pub struct Config {
|
|
pub providers: BTreeMap<String, Provider>,
|
|
}
|
|
impl Config {
|
|
pub async fn read(root: &Arc<dyn Layer>) -> anyhow::Result<Self> {
|
|
let file = match root.open(PATH, Open::default()).await {
|
|
Ok(file) => file,
|
|
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
|
|
return Ok(Self::default());
|
|
}
|
|
Err(error) => return Err(error.into()),
|
|
};
|
|
serde_yaml_ng::from_slice(&file.read(0, 16 * 1024 * 1024).await?)
|
|
.map_err(|error| anyhow::anyhow!("Cannot read {PATH}: {error}"))
|
|
}
|
|
}
|