1
0
Fork 0

Add DB URL to config

This commit is contained in:
Alex Kotov 2020-10-15 03:49:31 +05:00
parent 8624d912fd
commit 411d5c0391
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
5 changed files with 11 additions and 22 deletions

7
Cargo.lock generated
View file

@ -305,12 +305,6 @@ dependencies = [
"generic-array",
]
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]]
name = "error-chain"
version = "0.12.4"
@ -332,7 +326,6 @@ name = "fedihub-registry"
version = "0.0.0"
dependencies = [
"diesel",
"dotenv",
"r2d2",
"rocket",
"rocket_contrib",

View file

@ -13,7 +13,6 @@ categories = []
publish = true
[dependencies]
dotenv = "0.15.0"
r2d2 = "0.8.9"
rocket = "0.4.5"
serde = "1.0"

View file

@ -7,6 +7,8 @@ use rocket::config::{
const DEFAULT_ENVIRONMENT: Environment = Environment::Development;
const DEFAULT_ADDRESS: &str = "127.0.0.1";
const DEFAULT_PORT: u16 = 8000;
const DEFAULT_DATABASE_URL: &str =
"postgres://fedihub:fedihub@localhost/fedihub_development";
pub enum Environment {
Development,
@ -15,10 +17,11 @@ pub enum Environment {
}
pub struct Config {
root: String,
environment: Environment,
address: String,
port: u16,
pub root: String,
pub environment: Environment,
pub address: String,
pub port: u16,
pub database_url: String,
}
impl Environment {
@ -51,6 +54,7 @@ impl Config {
environment: DEFAULT_ENVIRONMENT,
address: DEFAULT_ADDRESS.to_string(),
port: DEFAULT_PORT,
database_url: DEFAULT_DATABASE_URL.to_string(),
}
)
}

View file

@ -4,7 +4,6 @@ use r2d2::{Pool, PooledConnection};
use rocket::{Outcome, Request, State};
use rocket::http::Status;
use rocket::request::{self, FromRequest};
use std::env;
use std::ops::Deref;
pub struct DbPool(Pool<ConnectionManager<PgConnection>>);
@ -33,11 +32,8 @@ impl Deref for DbConn {
}
}
pub fn create_db_pool() -> DbPool {
let credentials = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
let manager = ConnectionManager::<PgConnection>::new(credentials);
pub fn create_db_pool(database_url: String) -> DbPool {
let manager = ConnectionManager::<PgConnection>::new(database_url);
DbPool(Pool::new(manager).expect("Failed to create database pool"))
}

View file

@ -10,14 +10,11 @@ mod models;
#[macro_use] extern crate rocket;
#[macro_use] extern crate serde_derive;
extern crate dotenv;
extern crate rocket_contrib;
use rocket_contrib::templates::Template;
fn main() {
dotenv::dotenv().ok();
let config = config::Config::default().unwrap();
rocket(config).launch();
@ -25,7 +22,7 @@ fn main() {
fn rocket(config: config::Config) -> rocket::Rocket {
rocket::custom(config.to_rocket_config_builder().finalize().unwrap())
.manage(database::create_db_pool())
.manage(database::create_db_pool(config.database_url))
.attach(Template::fairing())
.mount("/", routes::routes())
}