From 493d909da8c6540b63ca2f461a9d2462715ebd0e Mon Sep 17 00:00:00 2001 From: Neil Kollack Date: Mon, 31 Jan 2022 23:24:07 -0600 Subject: feat: use figment Crate to setup configs Fixes #5 --- zoned/src/lib.rs | 21 +++++++++++++++++++++ zoned/src/main.rs | 25 ------------------------- 2 files changed, 21 insertions(+), 25 deletions(-) (limited to 'zoned/src') diff --git a/zoned/src/lib.rs b/zoned/src/lib.rs index baa567d..3ec492a 100644 --- a/zoned/src/lib.rs +++ b/zoned/src/lib.rs @@ -1,3 +1,4 @@ +use figment::{Figment, Provider, Error}; use rocket::{get, post, serde::json::Json, Build, Config, Rocket}; use rocket_okapi::{ openapi, openapi_get_routes, @@ -7,6 +8,23 @@ use rocket_okapi::{ }; use std::net::Ipv4Addr; use zone_core::{Container, PartialEqOrDefault}; +use zone_zfs::file_system::ZFSConfig; + +struct ZoneConf { + pub config: ZFSConfig, + pub figment: Figment, +} + +impl ZoneConf { + pub fn new() -> Result { + ZoneConf::custom(Config::figment()) + } + + pub fn custom(provider: T) -> Result { + let figment = Figment::from(provider); + Ok(ZoneConf { config: ZFSConfig::from(&figment)?, figment }) + } +} /// # List containers /// @@ -26,9 +44,12 @@ pub fn container_list(container: Container) -> Json> { #[openapi(tag = "Container")] #[post("/container", data = "")] fn create_container(container: Json) -> Json { + let zone_config = ZoneConf::new().unwrap(); + let container = zone_zfs::create_file_system( container.template.clone(), format!("{}-{}", container.user, container.id), + &zone_config.config.quota, ); match container { diff --git a/zoned/src/main.rs b/zoned/src/main.rs index 71ae05e..a8d8674 100644 --- a/zoned/src/main.rs +++ b/zoned/src/main.rs @@ -1,28 +1,3 @@ -use figment::providers::{Format, Serialized}; -use figment::{providers::Toml, Figment}; -use serde::{Deserialize, Serialize}; - -#[macro_use] -extern crate lazy_static; - -#[derive(Deserialize, Serialize)] -struct Config { - quota: String, -} - -impl Default for Config { - fn default() -> Self { - Config { - quota: "16G".to_string(), - } - } -} - -lazy_static! { - static ref CONFIG: Figment = Figment::from(Serialized::defaults(Config::default())) - .merge(Toml::file("/etc/zoned/Config.toml")); -} - #[rocket::main] async fn main() { match zoned::build_rocket().launch().await { -- cgit v1.2.3-70-g09d2