Config

Struct Config 

Source
pub struct Config {
    pub env: Environment,
    pub server: HttpServerConfig,
    pub db: DbConfig,
}
Expand description

Config is responsible of the configuration of a “full” server, reading the settings from environment variables: the deployment environment, the HTTP server settings and database settings.

Once you have the Rust objects with all the basic information you need for your server, like the database connection (DATABASE_URL), the deployment environment (APP_ENV) or the port where to start the app (PORT), you can use the struct objects to use those values to start up your Actix server, Rocket server, or whatever server your app use.

Fields§

§env: Environment

The environment name chosen to run the app, normally set through the environment variable APP_ENV. See Environment::init().

§server: HttpServerConfig

All the config needed to launch an HTTP server.

§db: DbConfig

All the config needed to setup a database, regardless of the engine.

Implementations§

Source§

impl Config

Source

pub fn init(default_port: u16) -> Result<Config>

Initialize all the configurations, setting each value with its corresponding environment variable, e.g. the env attribute with the APP_ENV environment variable.

The port number is get from the PORT env variable, otherwise defaulted to default_port.

§Examples
use std::env;
use server_env_config::Config;
use server_env_config::env::Environment;

// Configurations should be actually set by the OS environment
env::set_var("APP_ENV", "production");  // if not set, "local" is the default
env::set_var("APP_URI", "api/v1");
env::set_var("PORT", "8080");
env::set_var("DATABASE_URL", "postgresql://user:pass@localhost/db");

let result = Config::init(9999);        // 9999 will be used if "PORT" is not set
assert!(result.is_ok());
let config = result.unwrap();
assert_eq!(config.env, Environment::Production);
assert_eq!(config.server.port, 8080);
assert_eq!(config.server.url, "http://127.0.0.1:8080/api/v1/");    // calculated field
assert_eq!(config.db.database_url, "postgresql://user:pass@localhost/db");
// Some settings have default values if env variables are not set
assert_eq!(config.db.min_connections, 1);
assert_eq!(config.db.max_connections, 10);
// The `to_string()` method prints out all variables in .env format
println!("{}", config.to_string());
// # APP_URL --> http://127.0.0.1:8080/api/v1/
// APP_URI="api/v1"
// HOST=127.0.0.1
// PORT=8080
// APP_ENV=production
// DATABASE_URL="postgresql://user:pass@localhost/db"
// MIN_CONNECTIONS=1
// ...
Source

pub fn init_for( default_port: u16, environment: Option<Environment>, ) -> Result<Config>

Initialize config with the environment passed, if None, env will be set with the APP_ENV environment variable.

The port number is get from the PORT env variable, otherwise defaulted to default_port.

See Config::init().

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl ToString for Config

Source§

fn to_string(&self) -> String

This to_string() implementation prints out all the config values in .env format, using as key the environment variable used to set-up the config, even if the configuration was set in another way, e.g. using a default value.

Auto Trait Implementations§

§

impl Freeze for Config

§

impl RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl UnwindSafe for Config

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.