From 1a5a7eed4df2ee7aebdb752b78e7da78e5f15d28 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Tue, 1 Feb 2022 01:52:04 -0600 Subject: fix: integrated figment with rocket Co-authored-by: Neil Kollack --- zoned/src/lib.rs | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'zoned/src/lib.rs') diff --git a/zoned/src/lib.rs b/zoned/src/lib.rs index 3ec492a..dd35657 100644 --- a/zoned/src/lib.rs +++ b/zoned/src/lib.rs @@ -1,28 +1,33 @@ -use figment::{Figment, Provider, Error}; -use rocket::{get, post, serde::json::Json, Build, Config, Rocket}; +use figment::{Error, Provider, Figment}; +use rocket::{get, post, serde::json::Json, Build, Rocket, State}; use rocket_okapi::{ openapi, openapi_get_routes, rapidoc::{make_rapidoc, GeneralConfig, HideShowConfig, RapiDocConfig}, settings::UrlObject, swagger_ui::{make_swagger_ui, SwaggerUIConfig}, }; -use std::net::Ipv4Addr; +use serde::Serialize; use zone_core::{Container, PartialEqOrDefault}; -use zone_zfs::file_system::ZFSConfig; -struct ZoneConf { - pub config: ZFSConfig, - pub figment: Figment, +#[derive(Default, Serialize)] +pub struct Config { + pub zfs_config: zone_zfs::Config, + pub rocket_config: rocket::Config, } -impl ZoneConf { - pub fn new() -> Result { - ZoneConf::custom(Config::figment()) +impl Config { + pub fn new() -> Result { + Config::custom(zone_zfs::Config::figment(), rocket::Config::figment()) } - pub fn custom(provider: T) -> Result { - let figment = Figment::from(provider); - Ok(ZoneConf { config: ZFSConfig::from(&figment)?, figment }) + pub fn custom( + zfs_provider: T, + rocket_provider: U, + ) -> Result { + Ok(Config { + zfs_config: zone_zfs::Config::from(&zfs_provider)?, + rocket_config: rocket::Config::from(&rocket_provider), + }) } } @@ -43,13 +48,12 @@ pub fn container_list(container: Container) -> Json> { /// # Create container #[openapi(tag = "Container")] #[post("/container", data = "")] -fn create_container(container: Json) -> Json { - let zone_config = ZoneConf::new().unwrap(); - +fn create_container(container: Json, config: &State) -> 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, + &config.zfs_config, ); match container { @@ -58,13 +62,7 @@ fn create_container(container: Json) -> Json { } } -pub fn build_rocket() -> Rocket { - let config = Config { - address: Ipv4Addr::new(127, 0, 0, 1).into(), - port: 8000, - ..Config::debug_default() - }; - +pub fn build_rocket(config: Figment) -> Rocket { rocket::custom(config) .mount("/", openapi_get_routes![container_list, create_container,]) .mount( -- cgit v1.2.3-70-g09d2