Add cargo feature for making production build
This commit is contained in:
parent
e927ed83ff
commit
ae4bfcf614
7 changed files with 24 additions and 9 deletions
2
.env
2
.env
|
@ -1,2 +1,2 @@
|
||||||
# Allowed values: development, production
|
# Allowed values: development, production
|
||||||
ENVIRONMENT=development
|
#ENVIRONMENT=development
|
||||||
|
|
|
@ -86,3 +86,6 @@ web3 = { version = "0.18.0", default-features = false, features = ["http", "http
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
serial_test = "0.5.1"
|
serial_test = "0.5.1"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
production = []
|
||||||
|
|
|
@ -34,17 +34,17 @@ Matrix chat: [#mitra:halogen.city](https://matrix.to/#/#mitra:halogen.city)
|
||||||
Run:
|
Run:
|
||||||
|
|
||||||
```
|
```
|
||||||
cargo build --release
|
cargo build --release --features production
|
||||||
```
|
```
|
||||||
|
|
||||||
This command will produce two binaries in `target/release` directory, `mitra` and `mitractl`.
|
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).
|
An HTTP server will be needed to handle HTTPS requests and serve the frontend. See the example of [nginx configuration file](./contrib/mitra.nginx).
|
||||||
|
|
|
@ -6,8 +6,6 @@ Requires=postgresql.service
|
||||||
[Service]
|
[Service]
|
||||||
ExecStart=/usr/bin/mitra
|
ExecStart=/usr/bin/mitra
|
||||||
WorkingDirectory=/var/lib/mitra
|
WorkingDirectory=/var/lib/mitra
|
||||||
Environment="ENVIRONMENT=production"
|
|
||||||
Environment="CONFIG_PATH=/etc/mitra/config.yaml"
|
|
||||||
User=mitra
|
User=mitra
|
||||||
Group=mitra
|
Group=mitra
|
||||||
|
|
||||||
|
|
|
@ -101,6 +101,7 @@ async fn main() {
|
||||||
// Other commands require initialized app
|
// Other commands require initialized app
|
||||||
let config = config::parse_config();
|
let config = config::parse_config();
|
||||||
configure_logger(config.log_level);
|
configure_logger(config.log_level);
|
||||||
|
log::info!("config loaded from {}", config.config_path);
|
||||||
let db_config = config.database_url.parse().unwrap();
|
let db_config = config.database_url.parse().unwrap();
|
||||||
let db_client = &mut create_database_client(&db_config).await;
|
let db_client = &mut create_database_client(&db_config).await;
|
||||||
apply_migrations(db_client).await;
|
apply_migrations(db_client).await;
|
||||||
|
|
|
@ -25,6 +25,9 @@ pub enum Environment {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Environment {
|
impl Default for Environment {
|
||||||
|
#[cfg(feature = "production")]
|
||||||
|
fn default() -> Self { Self::Production }
|
||||||
|
#[cfg(not(feature = "production"))]
|
||||||
fn default() -> Self { Self::Development }
|
fn default() -> Self { Self::Development }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,6 +50,11 @@ struct EnvConfig {
|
||||||
crate_version: String,
|
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 {
|
fn parse_env() -> EnvConfig {
|
||||||
dotenv::from_filename(".env.local").ok();
|
dotenv::from_filename(".env.local").ok();
|
||||||
dotenv::dotenv().ok();
|
dotenv::dotenv().ok();
|
||||||
|
@ -55,7 +63,7 @@ fn parse_env() -> EnvConfig {
|
||||||
.map(|val| Environment::from_str(&val).expect("invalid environment type"))
|
.map(|val| Environment::from_str(&val).expect("invalid environment type"))
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let config_path = std::env::var("CONFIG_PATH")
|
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();
|
let crate_version = env!("CARGO_PKG_VERSION").to_string();
|
||||||
EnvConfig {
|
EnvConfig {
|
||||||
environment,
|
environment,
|
||||||
|
@ -93,6 +101,9 @@ pub struct Config {
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub environment: Environment,
|
pub environment: Environment,
|
||||||
|
|
||||||
|
#[serde(skip)]
|
||||||
|
pub config_path: String,
|
||||||
|
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub version: String,
|
pub version: String,
|
||||||
|
|
||||||
|
@ -233,12 +244,13 @@ fn read_instance_rsa_key(storage_dir: &Path) -> RsaPrivateKey {
|
||||||
|
|
||||||
pub fn parse_config() -> Config {
|
pub fn parse_config() -> Config {
|
||||||
let env = parse_env();
|
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");
|
.expect("failed to load config file");
|
||||||
let mut config = serde_yaml::from_str::<Config>(&config_yaml)
|
let mut config = serde_yaml::from_str::<Config>(&config_yaml)
|
||||||
.expect("invalid yaml data");
|
.expect("invalid yaml data");
|
||||||
// Set parameters from environment
|
// Set parameters from environment
|
||||||
config.environment = env.environment;
|
config.environment = env.environment;
|
||||||
|
config.config_path = env.config_path;
|
||||||
config.version = env.crate_version;
|
config.version = env.crate_version;
|
||||||
// Validate config
|
// Validate config
|
||||||
if !config.storage_dir.exists() {
|
if !config.storage_dir.exists() {
|
||||||
|
|
|
@ -32,6 +32,7 @@ const MAX_UPLOAD_SIZE: usize = 1024 * 1024 * 10;
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
let config = parse_config();
|
let config = parse_config();
|
||||||
configure_logger(config.log_level);
|
configure_logger(config.log_level);
|
||||||
|
log::info!("config loaded from {}", config.config_path);
|
||||||
let db_pool = create_pool(&config.database_url);
|
let db_pool = create_pool(&config.database_url);
|
||||||
let mut db_client = get_database_client(&db_pool).await.unwrap();
|
let mut db_client = get_database_client(&db_pool).await.unwrap();
|
||||||
apply_migrations(&mut **db_client).await;
|
apply_migrations(&mut **db_client).await;
|
||||||
|
|
Loading…
Reference in a new issue