change log dir option from toml to command line
This commit is contained in:
parent
275135c33d
commit
f252959a2f
8 changed files with 61 additions and 62 deletions
|
|
@ -1,6 +1,11 @@
|
||||||
# CHANGELOG
|
# CHANGELOG
|
||||||
|
|
||||||
## 0.9.8 or 0.10.0 (Unreleased)
|
## 0.10.0 (Unreleased)
|
||||||
|
|
||||||
|
### Important Changes
|
||||||
|
|
||||||
|
- [Breaking] We removed non-`watch` execute option and enabled the dynamic reloading of the config file by default.
|
||||||
|
- We newly added `log-dir` execute option to specify the directory for `access.log`,`error.log` and `rpxy.log`. This is optional, and if not specified, the logs are written to the standard output by default.
|
||||||
|
|
||||||
## 0.9.7
|
## 0.9.7
|
||||||
|
|
||||||
|
|
|
||||||
8
Cargo.lock
generated
8
Cargo.lock
generated
|
|
@ -2084,7 +2084,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rpxy"
|
name = "rpxy"
|
||||||
version = "0.9.7"
|
version = "0.10.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash",
|
"ahash",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
|
@ -2108,7 +2108,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rpxy-acme"
|
name = "rpxy-acme"
|
||||||
version = "0.9.7"
|
version = "0.10.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash",
|
"ahash",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
|
|
@ -2129,7 +2129,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rpxy-certs"
|
name = "rpxy-certs"
|
||||||
version = "0.9.7"
|
version = "0.10.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash",
|
"ahash",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
|
|
@ -2147,7 +2147,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rpxy-lib"
|
name = "rpxy-lib"
|
||||||
version = "0.9.7"
|
version = "0.10.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash",
|
"ahash",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.9.7"
|
version = "0.10.0"
|
||||||
authors = ["Jun Kurihara"]
|
authors = ["Jun Kurihara"]
|
||||||
homepage = "https://github.com/junkurihara/rust-rpxy"
|
homepage = "https://github.com/junkurihara/rust-rpxy"
|
||||||
repository = "https://github.com/junkurihara/rust-rpxy"
|
repository = "https://github.com/junkurihara/rust-rpxy"
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ You can run `rpxy` with a configuration file like
|
||||||
% ./target/release/rpxy --config config.toml
|
% ./target/release/rpxy --config config.toml
|
||||||
```
|
```
|
||||||
|
|
||||||
If you specify `-w` option along with the config file path, `rpxy` tracks the change of `config.toml` in the real-time manner and apply the change immediately without restarting the process.
|
`rpxy` tracks the change of `config.toml` in the real-time manner and apply the change immediately without restarting the process.
|
||||||
|
|
||||||
The full help messages are given follows.
|
The full help messages are given follows.
|
||||||
|
|
||||||
|
|
@ -89,11 +89,16 @@ usage: rpxy [OPTIONS] --config <FILE>
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-c, --config <FILE> Configuration file path like ./config.toml
|
-c, --config <FILE> Configuration file path like ./config.toml
|
||||||
-w, --watch Activate dynamic reloading of the config file via continuous monitoring
|
|
||||||
-h, --help Print help
|
-h, --help Print help
|
||||||
-V, --version Print version
|
-V, --version Print version
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you set `--log-dir=<log_dir>`, the log files are created in the specified directory. Otherwise, the log is printed to stdout.
|
||||||
|
|
||||||
|
- `${log_dir}/access.log` for access log
|
||||||
|
- `${log_dir}/error.log` for error log
|
||||||
|
- `${log_dir}/rpxy.log` for system log
|
||||||
|
|
||||||
That's all!
|
That's all!
|
||||||
|
|
||||||
## Basic Configuration
|
## Basic Configuration
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,18 @@
|
||||||
use super::toml::ConfigToml;
|
use super::toml::ConfigToml;
|
||||||
use crate::error::{anyhow, ensure};
|
use crate::error::{anyhow, ensure};
|
||||||
use ahash::HashMap;
|
use ahash::HashMap;
|
||||||
use clap::{Arg, ArgAction};
|
use clap::Arg;
|
||||||
use hot_reload::{ReloaderReceiver, ReloaderService};
|
use hot_reload::{ReloaderReceiver, ReloaderService};
|
||||||
use rpxy_certs::{build_cert_reloader, CryptoFileSourceBuilder, CryptoReloader, ServerCryptoBase};
|
use rpxy_certs::{CryptoFileSourceBuilder, CryptoReloader, ServerCryptoBase, build_cert_reloader};
|
||||||
use rpxy_lib::{AppConfig, AppConfigList, ProxyConfig};
|
use rpxy_lib::{AppConfig, AppConfigList, ProxyConfig};
|
||||||
|
|
||||||
#[cfg(feature = "acme")]
|
#[cfg(feature = "acme")]
|
||||||
use rpxy_acme::{AcmeManager, ACME_DIR_URL, ACME_REGISTRY_PATH};
|
use rpxy_acme::{ACME_DIR_URL, ACME_REGISTRY_PATH, AcmeManager};
|
||||||
|
|
||||||
/// Parsed options
|
/// Parsed options
|
||||||
pub struct Opts {
|
pub struct Opts {
|
||||||
pub config_file_path: String,
|
pub config_file_path: String,
|
||||||
pub watch: bool,
|
pub log_dir_path: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse arg values passed from cli
|
/// Parse arg values passed from cli
|
||||||
|
|
@ -28,19 +28,22 @@ pub fn parse_opts() -> Result<Opts, anyhow::Error> {
|
||||||
.help("Configuration file path like ./config.toml"),
|
.help("Configuration file path like ./config.toml"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("watch")
|
Arg::new("log_dir")
|
||||||
.long("watch")
|
.long("log-dir")
|
||||||
.short('w')
|
.short('l')
|
||||||
.action(ArgAction::SetTrue)
|
.value_name("LOG_DIR")
|
||||||
.help("Activate dynamic reloading of the config file via continuous monitoring"),
|
.help("Directory for log files. If not specified, logs are printed to stdout."),
|
||||||
);
|
);
|
||||||
let matches = options.get_matches();
|
let matches = options.get_matches();
|
||||||
|
|
||||||
///////////////////////////////////
|
///////////////////////////////////
|
||||||
let config_file_path = matches.get_one::<String>("config_file").unwrap().to_owned();
|
let config_file_path = matches.get_one::<String>("config_file").unwrap().to_owned();
|
||||||
let watch = matches.get_one::<bool>("watch").unwrap().to_owned();
|
let log_dir_path = matches.get_one::<String>("log_dir").map(|v| v.to_owned());
|
||||||
|
|
||||||
Ok(Opts { config_file_path, watch })
|
Ok(Opts {
|
||||||
|
config_file_path,
|
||||||
|
log_dir_path,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_settings(config: &ConfigToml) -> std::result::Result<(ProxyConfig, AppConfigList), anyhow::Error> {
|
pub fn build_settings(config: &ConfigToml) -> std::result::Result<(ProxyConfig, AppConfigList), anyhow::Error> {
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use crate::{
|
||||||
log::warn,
|
log::warn,
|
||||||
};
|
};
|
||||||
use ahash::HashMap;
|
use ahash::HashMap;
|
||||||
use rpxy_lib::{reexports::Uri, AppConfig, ProxyConfig, ReverseProxyConfig, TlsConfig, UpstreamUri};
|
use rpxy_lib::{AppConfig, ProxyConfig, ReverseProxyConfig, TlsConfig, UpstreamUri, reexports::Uri};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::{fs, net::SocketAddr};
|
use std::{fs, net::SocketAddr};
|
||||||
use tokio::time::Duration;
|
use tokio::time::Duration;
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,12 @@ use tracing_subscriber::{fmt, prelude::*};
|
||||||
pub use tracing::{debug, error, info, warn};
|
pub use tracing::{debug, error, info, warn};
|
||||||
|
|
||||||
/// Initialize the logger with the RUST_LOG environment variable.
|
/// Initialize the logger with the RUST_LOG environment variable.
|
||||||
pub fn init_logger() {
|
pub fn init_logger(log_dir_path: Option<&str>) {
|
||||||
|
if log_dir_path.is_some() {
|
||||||
|
// TODO:
|
||||||
|
println!("Activate logging to files")
|
||||||
|
}
|
||||||
|
|
||||||
let level_string = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string());
|
let level_string = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string());
|
||||||
let level = tracing::Level::from_str(level_string.as_str()).unwrap_or(tracing::Level::INFO);
|
let level = tracing::Level::from_str(level_string.as_str()).unwrap_or(tracing::Level::INFO);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,6 @@ use std::sync::Arc;
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
init_logger();
|
|
||||||
|
|
||||||
let mut runtime_builder = tokio::runtime::Builder::new_multi_thread();
|
let mut runtime_builder = tokio::runtime::Builder::new_multi_thread();
|
||||||
runtime_builder.enable_all();
|
runtime_builder.enable_all();
|
||||||
runtime_builder.thread_name("rpxy");
|
runtime_builder.thread_name("rpxy");
|
||||||
|
|
@ -30,16 +28,11 @@ fn main() {
|
||||||
runtime.block_on(async {
|
runtime.block_on(async {
|
||||||
// Initially load options
|
// Initially load options
|
||||||
let Ok(parsed_opts) = parse_opts() else {
|
let Ok(parsed_opts) = parse_opts() else {
|
||||||
error!("Invalid toml file");
|
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
};
|
};
|
||||||
|
|
||||||
if !parsed_opts.watch {
|
init_logger(parsed_opts.log_dir_path.as_deref());
|
||||||
if let Err(e) = rpxy_service_without_watcher(&parsed_opts.config_file_path, runtime.handle().clone()).await {
|
|
||||||
error!("rpxy service existed: {e}");
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let (config_service, config_rx) = ReloaderService::<ConfigTomlReloader, ConfigToml, String>::new(
|
let (config_service, config_rx) = ReloaderService::<ConfigTomlReloader, ConfigToml, String>::new(
|
||||||
&parsed_opts.config_file_path,
|
&parsed_opts.config_file_path,
|
||||||
CONFIG_WATCH_DELAY_SECS,
|
CONFIG_WATCH_DELAY_SECS,
|
||||||
|
|
@ -55,7 +48,7 @@ fn main() {
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rpxy_res = rpxy_service_with_watcher(config_rx, runtime.handle().clone()) => {
|
rpxy_res = rpxy_service(config_rx, runtime.handle().clone()) => {
|
||||||
if let Err(e) = rpxy_res {
|
if let Err(e) = rpxy_res {
|
||||||
error!("rpxy service existed: {e}");
|
error!("rpxy service existed: {e}");
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
|
|
@ -63,7 +56,6 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -233,18 +225,7 @@ impl RpxyService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn rpxy_service_without_watcher(
|
async fn rpxy_service(
|
||||||
config_file_path: &str,
|
|
||||||
runtime_handle: tokio::runtime::Handle,
|
|
||||||
) -> Result<(), anyhow::Error> {
|
|
||||||
info!("Start rpxy service");
|
|
||||||
let config_toml = ConfigToml::new(config_file_path).map_err(|e| anyhow!("Invalid toml file: {e}"))?;
|
|
||||||
let service = RpxyService::new(&config_toml, runtime_handle).await?;
|
|
||||||
// Create cancel token that is never be called as dummy
|
|
||||||
service.start(tokio_util::sync::CancellationToken::new()).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn rpxy_service_with_watcher(
|
|
||||||
mut config_rx: ReloaderReceiver<ConfigToml, String>,
|
mut config_rx: ReloaderReceiver<ConfigToml, String>,
|
||||||
runtime_handle: tokio::runtime::Handle,
|
runtime_handle: tokio::runtime::Handle,
|
||||||
) -> Result<(), anyhow::Error> {
|
) -> Result<(), anyhow::Error> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue