Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3813081
D1664.id.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D1664.id.diff
View Options
diff --git a/Cargo.toml b/Cargo.toml
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -9,6 +9,7 @@
[dependencies]
diesel = { version = "1.0.0", features = ["postgres", "r2d2", "chrono"] }
+dotenv = "0.9.0"
log = "^0.4.4"
r2d2 = "^0.8.2"
rocket = "^0.3.16"
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,96 @@
+//! This module allows to configure the API.
+//!
+//! It provides a Config trait to build custom configuration implementation.
+//!
+//! It also provides a `DefaultConfig` implementation of this `Config` trait to
+//! extract variables from an .env file or environment.
+
+use dotenv::dotenv;
+use std::env;
+use std::error::Error;
+use ErrorResult;
+
+/* -------------------------------------------------------------
+ Config trait
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+/// This trait allows to provide a configuration for the resources needed by the API.
+pub trait Config {
+ fn get_database_url(&self) -> &str;
+ fn get_entry_point(&self) -> &str;
+ fn get_database_pool_size(&self) -> u32;
+}
+
+/* -------------------------------------------------------------
+ DefaultConfig
+
+ :: Config
+ :: sui generis implementation
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+/// This is a default implementation of the `Config` trait, which extracts the following variables
+/// from an .env file or environment:
+///
+/// - `API_ENTRY_POINT` (facultative, by default `/`): the mouting point of the API methods
+/// - `DATABASE_URL` (mandatory): the URL to connect to your database
+/// - `DATABASE_POOL_SIZE` (facultative, by default 4): the number of connections to open
+pub struct DefaultConfig {
+ database_url: String,
+ entry_point: String,
+ database_pool_size: u32,
+}
+
+impl Config for DefaultConfig {
+ fn get_database_url(&self) -> &str {
+ &self.database_url
+ }
+
+ fn get_entry_point(&self) -> &str {
+ &self.entry_point
+ }
+
+ fn get_database_pool_size(&self) -> u32 {
+ self.database_pool_size
+ }
+}
+
+impl DefaultConfig {
+ pub const DEFAULT_DATABASE_POOL_SIZE: u32 = 4;
+
+ pub fn parse_environment() -> ErrorResult<Self> {
+ if let Err(error) = dotenv() {
+ warn!(target: "config", "Can't parse .env: {}", error.description());
+ };
+
+ let database_url = match env::var("DATABASE_URL") {
+ Ok(url) => url,
+ Err(e) => {
+ error!(target: "config", "You need to specify a DATABASE_URL variable in the environment (or .env file).");
+ return Err(Box::new(e));
+ }
+ };
+
+ let entry_point = env::var("API_ENTRY_POINT").unwrap_or(String::from("/"));
+
+ let database_pool_size = match env::var("DATABASE_POOL_SIZE") {
+ Ok(variable) => {
+ match variable.parse::<u32>() {
+ Ok(size) => size,
+ Err(_) => {
+ warn!(target: "config", "The DATABASE_POOL_SIZE variable must be an unsigned integer.");
+
+ DefaultConfig::DEFAULT_DATABASE_POOL_SIZE
+ },
+ }
+ },
+ Err(_) => DefaultConfig::DEFAULT_DATABASE_POOL_SIZE,
+ };
+
+ Ok(DefaultConfig {
+ database_url,
+ entry_point,
+ database_pool_size,
+ })
+ }
+}
+
diff --git a/src/database.rs b/src/database.rs
--- a/src/database.rs
+++ b/src/database.rs
@@ -76,7 +76,7 @@
/// .mount("/", routes)
/// .launch();
/// ```
-pub fn initialize_database_pool(url: String, max_size: u32) -> Result<PostgreSQLPool, PoolError> {
+pub fn initialize_database_pool(url: &str, max_size: u32) -> Result<PostgreSQLPool, PoolError> {
let manager = ConnectionManager::<PgConnection>::new(url);
Pool::builder()
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,5 @@
extern crate diesel;
+extern crate dotenv;
#[macro_use] extern crate log;
extern crate r2d2;
extern crate rocket;
@@ -9,6 +10,7 @@
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
pub mod api;
+pub mod config;
pub mod database;
/* -------------------------------------------------------------
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Dec 2, 16:07 (13 h, 50 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2276012
Default Alt Text
D1664.id.diff (4 KB)
Attached To
Mode
D1664: Provide a config module
Attached
Detach File
Event Timeline
Log In to Comment