From ae4bfcf614264f91ed39b455aca4295c1b4e006b Mon Sep 17 00:00:00 2001 From: silverpill Date: Wed, 4 May 2022 19:44:38 +0000 Subject: [PATCH] Add cargo feature for making production build --- .env | 2 +- Cargo.toml | 3 +++ README.md | 8 ++++---- contrib/mitra.service | 2 -- src/bin/mitractl.rs | 1 + src/config.rs | 16 ++++++++++++++-- src/main.rs | 1 + 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/.env b/.env index 93ce2c9..77682dd 100644 --- a/.env +++ b/.env @@ -1,2 +1,2 @@ # Allowed values: development, production -ENVIRONMENT=development +#ENVIRONMENT=development diff --git a/Cargo.toml b/Cargo.toml index bdf3542..4fdf473 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -86,3 +86,6 @@ web3 = { version = "0.18.0", default-features = false, features = ["http", "http [dev-dependencies] serial_test = "0.5.1" + +[features] +production = [] diff --git a/README.md b/README.md index 7547983..02b4f59 100644 --- a/README.md +++ b/README.md @@ -34,17 +34,17 @@ Matrix chat: [#mitra:halogen.city](https://matrix.to/#/#mitra:halogen.city) Run: ``` -cargo build --release +cargo build --release --features production ``` This command will produce two binaries in `target/release` directory, `mitra` and `mitractl`. -Create a database and a configuration file (see [example](./config.yaml.example)). +Create the database and the configuration file (see [example](./config.yaml.example)). Default config file path is `/etc/mitra/config.yaml`, but it can be changed using `CONFIG_PATH` environment variable. -When starting Mitra, set the value of `ENVIRONMENT` variable to `production` and specify the path to configuration file with `CONFIG_PATH`: +Start Mitra: ``` -ENVIRONMENT=production CONFIG_PATH=/etc/mitra/config.yaml mitra +./mitra ``` An HTTP server will be needed to handle HTTPS requests and serve the frontend. See the example of [nginx configuration file](./contrib/mitra.nginx). diff --git a/contrib/mitra.service b/contrib/mitra.service index 28ee5f9..22e61d9 100644 --- a/contrib/mitra.service +++ b/contrib/mitra.service @@ -6,8 +6,6 @@ Requires=postgresql.service [Service] ExecStart=/usr/bin/mitra WorkingDirectory=/var/lib/mitra -Environment="ENVIRONMENT=production" -Environment="CONFIG_PATH=/etc/mitra/config.yaml" User=mitra Group=mitra diff --git a/src/bin/mitractl.rs b/src/bin/mitractl.rs index e99c77d..37b760b 100644 --- a/src/bin/mitractl.rs +++ b/src/bin/mitractl.rs @@ -101,6 +101,7 @@ async fn main() { // Other commands require initialized app let config = config::parse_config(); configure_logger(config.log_level); + log::info!("config loaded from {}", config.config_path); let db_config = config.database_url.parse().unwrap(); let db_client = &mut create_database_client(&db_config).await; apply_migrations(db_client).await; diff --git a/src/config.rs b/src/config.rs index c919060..bace9f6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,6 +25,9 @@ pub enum Environment { } impl Default for Environment { + #[cfg(feature = "production")] + fn default() -> Self { Self::Production } + #[cfg(not(feature = "production"))] fn default() -> Self { Self::Development } } @@ -47,6 +50,11 @@ struct EnvConfig { crate_version: String, } +#[cfg(feature = "production")] +const DEFAULT_CONFIG_PATH: &str = "/etc/mitra/config.yaml"; +#[cfg(not(feature = "production"))] +const DEFAULT_CONFIG_PATH: &str = "config.yaml"; + fn parse_env() -> EnvConfig { dotenv::from_filename(".env.local").ok(); dotenv::dotenv().ok(); @@ -55,7 +63,7 @@ fn parse_env() -> EnvConfig { .map(|val| Environment::from_str(&val).expect("invalid environment type")) .unwrap_or_default(); let config_path = std::env::var("CONFIG_PATH") - .unwrap_or("config.yaml".to_string()); + .unwrap_or(DEFAULT_CONFIG_PATH.to_string()); let crate_version = env!("CARGO_PKG_VERSION").to_string(); EnvConfig { environment, @@ -93,6 +101,9 @@ pub struct Config { #[serde(skip)] pub environment: Environment, + #[serde(skip)] + pub config_path: String, + #[serde(skip)] pub version: String, @@ -233,12 +244,13 @@ fn read_instance_rsa_key(storage_dir: &Path) -> RsaPrivateKey { pub fn parse_config() -> Config { let env = parse_env(); - let config_yaml = std::fs::read_to_string(env.config_path) + let config_yaml = std::fs::read_to_string(&env.config_path) .expect("failed to load config file"); let mut config = serde_yaml::from_str::(&config_yaml) .expect("invalid yaml data"); // Set parameters from environment config.environment = env.environment; + config.config_path = env.config_path; config.version = env.crate_version; // Validate config if !config.storage_dir.exists() { diff --git a/src/main.rs b/src/main.rs index 07990fd..d2dc0a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,7 @@ const MAX_UPLOAD_SIZE: usize = 1024 * 1024 * 10; async fn main() -> std::io::Result<()> { let config = parse_config(); configure_logger(config.log_level); + log::info!("config loaded from {}", config.config_path); let db_pool = create_pool(&config.database_url); let mut db_client = get_database_client(&db_pool).await.unwrap(); apply_migrations(&mut **db_client).await;