From 14ab5ec766adf7e07a57d5ab63770f6c63df4af2 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 24 Aug 2020 23:07:13 +0200 Subject: Move database config to Rocket.toml. This makes for one place to config everything instead of programatically setting the config. --- Rocket.toml.example | 32 ++++++++++++++++++++++++++++++++ src/lib.rs | 14 ++------------ src/main.rs | 5 +---- 3 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 Rocket.toml.example diff --git a/Rocket.toml.example b/Rocket.toml.example new file mode 100644 index 0000000..9dbee4a --- /dev/null +++ b/Rocket.toml.example @@ -0,0 +1,32 @@ +[global.databases] +# Change the database url for your setup +main = { url = "postgres:///...." } + +[development] +address = "localhost" +port = 8000 +workers = 2 +keep_alive = 5 +log = "normal" +limits = { forms = 32768 } + +[staging] +address = "0.0.0.0" +port = 8000 +workers = 2 +keep_alive = 5 +log = "normal" +# You have to generate a secret key, f.eks by `openssl rand -base64 32`. +# Resetting this value will invalidate any cookies etc. +secret_key = ... +limits = { forms = 32768 } + +[production] +address = "0.0.0.0" +port = 8000 +keep_alive = 5 +log = "critical" +# You have to generate a secret key, f.eks by `openssl rand -base64 32`. +# Resetting this value will invalidate any cookies etc. +secret_key = ... +limits = { forms = 32768 } diff --git a/src/lib.rs b/src/lib.rs index 57d8b7a..330c462 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,21 +26,11 @@ pub mod schema; use crate::controllers::{film, room, screening}; use rocket::routes; -use std::collections::HashMap; use std::error::Error; use std::result::Result; -pub fn build_rocket(db_url: &str) -> Result> { - let mut db_config = HashMap::new(); - db_config.insert("url", rocket::config::Value::from(db_url)); - - let mut databases = HashMap::new(); - databases.insert("main", rocket::config::Value::from(db_config)); - - let mut config = rocket::Config::active()?; - config.extras.insert("databases".into(), rocket::config::Value::from(databases)); - - Ok(rocket::custom(config) +pub fn build_rocket() -> Result> { + Ok(rocket::ignite() .attach(db::Connection::fairing()) .attach(rocket_contrib::templates::Template::fairing()) .mount("/", rocket_contrib::serve::StaticFiles::from("./public")) diff --git a/src/main.rs b/src/main.rs index 773fc66..fdc8ff3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,9 +21,6 @@ use std::error::Error; use std::result::Result; fn main() -> Result<(), Box> { - let db_url = dotenv::var("DATABASE_URL") - .map_err(|_| "No database! Set DATABASE_URL env var and try again.")?; - - ramaskrik::build_rocket(&db_url)?.launch(); + ramaskrik::build_rocket()?.launch(); Ok(()) } -- cgit v1.2.3