diff --git a/.gitignore b/.gitignore index 48f07a6..9ee3eb8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ /target -config.toml +nomilo.toml db.sqlite __pycache__ -/env \ No newline at end of file +/env diff --git a/Cargo.lock b/Cargo.lock index c1f0088..b10b8d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -914,6 +914,7 @@ dependencies = [ "diesel", "diesel-derive-enum", "djangohashers", + "figment", "humantime", "jsonwebtoken", "rocket", diff --git a/Cargo.toml b/Cargo.toml index 035d151..e3268bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,4 @@ jsonwebtoken = "7.2.0" chrono = { version = "0.4", features = ["serde"] } humantime = "2.1.0" tokio = "1" +figment = { version = "0.10.6", features = ["toml", "env"] } \ No newline at end of file diff --git a/Rocket.toml b/Rocket.toml deleted file mode 100644 index 4e9292a..0000000 --- a/Rocket.toml +++ /dev/null @@ -1,2 +0,0 @@ -[global.databases] -db = { url = "db.sqlite" } diff --git a/config.example.toml b/nomilo.example.toml similarity index 61% rename from config.example.toml rename to nomilo.example.toml index ade3716..b32e08d 100644 --- a/config.example.toml +++ b/nomilo.example.toml @@ -1,7 +1,10 @@ -[web_app] +[global.databases.sqlite] +url = "db.sqlite" + +[release.web_app] # base64 secret, change it (openssl rand -base64 32) secret = "Y2hhbmdlbWUK" token_duration = "1d" -[dns] +[release.dns] server = "127.0.0.1:53" diff --git a/src/config.rs b/src/config.rs index 74c6f3a..e286f03 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,4 @@ use std::net::SocketAddr; -use std::path::PathBuf; -use std::fs; use serde::{Deserialize, Deserializer}; use chrono::Duration; @@ -39,11 +37,6 @@ fn from_duration<'de, D>(deserializer: D) -> Result .and_then(|duration| Duration::from_std(duration).map_err(|err| Error::custom(err.to_string()))) } -pub fn load(file_name: PathBuf) -> Config { - let file_content = fs::read_to_string(file_name).expect("could not read config file"); - toml::from_str(&file_content).expect("could not parse config file") -} - // TODO: Maybe remove this #[rocket::async_trait] impl<'r> FromRequest<'r> for DnsClient { @@ -60,7 +53,6 @@ impl<'r> FromRequest<'r> for DnsClient { } } - #[rocket::async_trait] impl<'r> FromRequest<'r> for Box { type Error = (); diff --git a/src/main.rs b/src/main.rs index fe46f3b..9ad2e54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,20 +9,41 @@ mod schema; mod routes; mod dns; +use std::process::exit; + +use figment::{Figment, Profile, providers::{Format, Toml, Env}}; + use routes::users::*; use routes::zones::*; use rocket_sync_db_pools::database; -#[database("db")] + +#[database("sqlite")] pub struct DbConn(diesel::SqliteConnection); + #[launch] async fn rocket() -> _ { - let app_config = config::load("config.toml".into()); - println!("{:#?}", app_config); + //let app_config = config::load("config.toml".into()); + //println!("{:#?}", app_config); - rocket::build() + + let figment = Figment::from(rocket::Config::default()) + .merge(Toml::file(Env::var_or("NOMILO_CONFIG", "nomilo.toml")).nested()) + .merge(Env::prefixed("NOMILO_").ignore(&["PROFILE"]).global()) + .select(Profile::from_env_or("NOMILO_PROFILE", "release")); + + let app_config = match figment.extract::() { + Ok(c) => c, + Err(e) => { + eprintln!("Error loading configuration: {}", e); + exit(1); + } + }; + //let app_config = .expect("unable to load configuration"); + + rocket::custom(figment) .manage(app_config) .attach(DbConn::fairing()) .mount("/api/v1", routes![