land_core/storage/
mod.rs

1use anyhow::{anyhow, Result};
2use envconfig::Envconfig;
3use once_cell::sync::OnceCell;
4use opendal::Operator;
5use tracing::debug;
6
7use self::local::init_local;
8
9mod local;
10
11#[derive(Envconfig, Debug)]
12pub struct Config {
13    #[envconfig(from = "STORAGE_TYPE", default = "local")]
14    pub type_name: String,
15}
16
17pub static STORAGE: OnceCell<Operator> = OnceCell::new();
18
19
20#[tracing::instrument(name="[STORAGE]")]
21pub async fn init() -> Result<()> {
22    let cfg = Config::init_from_env().unwrap();
23    debug!("Init storage cfg: {:?}", cfg);
24    match cfg.type_name.as_str() {
25        "local" => {
26            let op = init_local().await?;
27            STORAGE.set(op).unwrap();
28        }
29        _ => {
30            return Err(anyhow!("unknown storage type: {}", cfg.type_name));
31        }
32    }
33
34    Ok(())
35}