add proxy_configs.toml (unread currently)

This commit is contained in:
Brandon Thomas
2020-06-29 04:06:28 -04:00
parent 67d094d38e
commit f6619e5fef
4 changed files with 124 additions and 4 deletions
+48 -2
View File
@@ -1,4 +1,5 @@
#[macro_use] extern crate log;
#[macro_use] extern crate serde_derive;
/*#[derive(Deserialize)]
@@ -13,15 +14,40 @@ use futures::future::{self, Future};
use hyper::server::conn::AddrStream;
use hyper::service::{service_fn, make_service_fn};
use hyper::{Body, Request, Response, Server};
use anyhow::Result as AnyhowResult;
use std::env;
type BoxFut = Box<Future<Item=Response<Body>, Error=hyper::Error> + Send>;
const ENV_PROXY_CONFIG_FILE : &'static str = "PROXY_CONFIG_FILE";
const ENV_ROUTE_ONE : &'static str = "ROUTE_ONE";
const ENV_ROUTE_TWO : &'static str = "ROUTE_TWO";
const ENV_RUST_LOG : &'static str = "RUST_LOG";
const ROUTE_ONE_DEFAULT : &'static str = "http://127.0.0.1:12345";
const ROUTE_TWO_DEFAULT : &'static str = "http://127.0.0.1:3000";
const DEFAULT_PROXY_CONFIG_FILE : &'static str = "proxy_configs.toml";
const DEFAULT_RUST_LOG: &'static str = "debug";
#[derive(Deserialize, Debug, Clone)]
struct ProxyConfigs {
pub backends: Vec<ProxyConfig>,
}
#[derive(Deserialize, Debug, Clone)]
struct ProxyConfig {
pub voice : String,
pub ip: String,
pub port: String,
}
impl ProxyConfigs {
pub fn load_from_file(filename: &str) -> AnyhowResult<Self> {
let contents = std::fs::read_to_string(filename)?;
let configs = toml::from_str(&contents)?;
Ok(configs)
}
}
fn get_env_string(env_name: &str, default: &str) -> String {
match env::var(env_name).as_ref().ok() {
@@ -39,13 +65,31 @@ fn debug_request(req: Request<Body>) -> BoxFut {
Box::new(future::ok(response))
}
fn main() {
fn main() -> AnyhowResult<()> {
if env::var(ENV_RUST_LOG)
.as_ref()
.ok()
.is_none()
{
println!("Setting default logging level to \"{}\", override with env var {}.",
DEFAULT_RUST_LOG, ENV_RUST_LOG);
std::env::set_var(ENV_RUST_LOG, DEFAULT_RUST_LOG);
}
env_logger::init();
let route_one = get_env_string(ENV_ROUTE_ONE, ROUTE_ONE_DEFAULT);
let route_two = get_env_string(ENV_ROUTE_TWO, ROUTE_TWO_DEFAULT);
info!("Route one: {}", route_one);
info!("Route two: {}", route_two);
let proxy_configs_file = get_env_string(ENV_PROXY_CONFIG_FILE, DEFAULT_PROXY_CONFIG_FILE);
info!("Proxy config file: {}", proxy_configs_file);
let proxy_configs = ProxyConfigs::load_from_file(&proxy_configs_file)?;
info!("Proxy configs: {:?}", proxy_configs);
// This is our socket address...
let addr = ([127, 0, 0, 1], 13900).into();
@@ -77,4 +121,6 @@ fn main() {
// Run this server for... forever!
hyper::rt::run(server);
}
Ok(())
}