diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2020-08-24 23:07:13 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2020-08-24 23:07:13 +0200 |
commit | 14ab5ec766adf7e07a57d5ab63770f6c63df4af2 (patch) | |
tree | dd2cda70f2475f19b727aebd21010f83b85130d0 | |
parent | 87734ea6b0311903d42ed9ae55965bdf4ea13cb4 (diff) | |
download | ramaskrik-social-14ab5ec766adf7e07a57d5ab63770f6c63df4af2.tar.gz ramaskrik-social-14ab5ec766adf7e07a57d5ab63770f6c63df4af2.tar.bz2 ramaskrik-social-14ab5ec766adf7e07a57d5ab63770f6c63df4af2.zip |
Move database config to Rocket.toml.
This makes for one place to config everything instead of programatically
setting the config.
-rw-r--r-- | Rocket.toml.example | 32 | ||||
-rw-r--r-- | src/lib.rs | 14 | ||||
-rw-r--r-- | src/main.rs | 5 |
3 files changed, 35 insertions, 16 deletions
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 } @@ -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<rocket::Rocket, Box<dyn Error>> { - 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<rocket::Rocket, Box<dyn Error>> { + 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<dyn Error>> { - 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(()) } |