Add cargo feature for making production build

This commit is contained in:
silverpill 2022-05-04 19:44:38 +00:00
parent e927ed83ff
commit ae4bfcf614
7 changed files with 24 additions and 9 deletions

2
.env
View file

@ -1,2 +1,2 @@
# Allowed values: development, production
ENVIRONMENT=development
#ENVIRONMENT=development

View file

@ -86,3 +86,6 @@ web3 = { version = "0.18.0", default-features = false, features = ["http", "http
[dev-dependencies]
serial_test = "0.5.1"
[features]
production = []

View file

@ -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).

View file

@ -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

View file

@ -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;

View file

@ -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>(&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() {

View file

@ -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;