Skip to content

Commit

Permalink
read config from env var
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Dec 3, 2021
1 parent 44e0d60 commit 2f94407
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 28 deletions.
20 changes: 14 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,26 @@ GLOBAL OPTIONS:

start from command line arguments:
```shell
# enable udp
realm -l 127.0.0.1:5000 -r 1.1.1.1:443 --udp

# specify outbound ip
realm -l 127.0.0.1:5000 -r 1.1.1.1:443 --through 127.0.0.1
realm -l 0.0.0.0:5000 -r 1.1.1.1:443
```

or use a config file:
start from config file:
```shell
# use toml
realm -c config.toml

# use json
realm -c config.json
```

start from environment variable:
```shell
CONFIG='{"endpoints":[{"local":"127.0.0.1:5000","remote":"1.1.1.1:443"}]}' realm

export CONFIG=`cat config.json | jq -c `
realm
```

## Configuration
TOML Example
```toml
Expand Down
32 changes: 22 additions & 10 deletions src/conf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs;
use std::io::{Result, Error, ErrorKind};

use serde::{Serialize, Deserialize};

Expand Down Expand Up @@ -45,21 +46,32 @@ impl FullConf {
}
}

pub fn from_config_file(file: &str) -> Self {
let config = fs::read_to_string(file)
pub fn from_conf_file(file: &str) -> Self {
let conf = fs::read_to_string(file)
.unwrap_or_else(|e| panic!("unable to open {}: {}", file, &e));
let toml_err = match toml::from_str(&config) {
Ok(x) => return x,
match Self::from_conf_str(&conf) {
Ok(x) => x,
Err(e) => panic!("failed to parse {}: {}", file, &e),
}
}

pub fn from_conf_str(conf: &str) -> Result<Self> {
let toml_err = match toml::from_str(conf) {
Ok(x) => return Ok(x),
Err(e) => e,
};
let json_err = match serde_json::from_str(&config) {
Ok(x) => return x,
let json_err = match serde_json::from_str(conf) {
Ok(x) => return Ok(x),
Err(e) => e,
};
panic!(
"parse {0} as toml: {1}; parse {0} as json: {2}",
file, &toml_err, &json_err
);

Err(Error::new(
ErrorKind::Other,
format!(
"parse as toml: {0}; parse as json: {1}",
&toml_err, &json_err
),
))
}

pub fn add_endpoint(&mut self, endpoint: EndpointConf) -> &mut Self {
Expand Down
35 changes: 23 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ mod conf;
mod utils;
mod relay;

use std::env;

use cfg_if::cfg_if;
use cmd::CmdInput;
use conf::FullConf;
use utils::Endpoint;

const VERSION: &str = "1.5.0-rc6";
const ENV_CONFIG: &str = "CONFIG";

cfg_if! {
if #[cfg(all(feature = "mi-malloc"))] {
Expand All @@ -24,21 +27,29 @@ cfg_if! {
}

fn main() {
let conf = match cmd::scan() {
CmdInput::Endpoint(ep, opts) => {
let mut conf = FullConf::default();
conf.add_endpoint(ep).apply_global_opts(opts);
conf
}
CmdInput::Config(conf, opts) => {
let mut conf = FullConf::from_config_file(&conf);
conf.apply_global_opts(opts);
conf
let conf = || {
if let Ok(conf_str) = env::var(ENV_CONFIG) {
if let Ok(conf) = FullConf::from_conf_str(&conf_str) {
return conf;
}
};

match cmd::scan() {
CmdInput::Endpoint(ep, opts) => {
let mut conf = FullConf::default();
conf.add_endpoint(ep).apply_global_opts(opts);
conf
}
CmdInput::Config(conf, opts) => {
let mut conf = FullConf::from_conf_file(&conf);
conf.apply_global_opts(opts);
conf
}
CmdInput::None => std::process::exit(0),
}
CmdInput::None => std::process::exit(0),
};

start_from_conf(conf);
start_from_conf(conf());
}

fn start_from_conf(conf: FullConf) {
Expand Down

0 comments on commit 2f94407

Please sign in to comment.