tricot/src/main.rs

77 lines
2 KiB
Rust
Raw Normal View History

#[macro_use]
extern crate anyhow;
2021-12-07 17:19:51 +00:00
use std::net::SocketAddr;
2021-12-07 16:56:15 +00:00
use structopt::StructOpt;
2021-12-07 12:50:44 +00:00
mod cert;
mod cert_store;
mod consul;
2021-12-07 12:50:44 +00:00
mod http;
2021-12-07 14:20:45 +00:00
mod https;
mod proxy_config;
2021-12-07 14:20:45 +00:00
mod reverse_proxy;
use log::*;
2021-12-07 16:56:15 +00:00
#[derive(StructOpt, Debug)]
#[structopt(name = "tricot")]
struct Opt {
/// Address of consul server
#[structopt(long = "consul-addr", env = "TRICOT_CONSUL_HOST", default_value = "http://127.0.0.1:8500/")]
pub consul_addr: String,
/// Prefix of Tricot's entries in Consul KV space
#[structopt(long = "consul-kv-prefix", env = "TRICOT_CONSUL_KV_PREFIX", default_value = "tricot/")]
pub consul_kv_prefix: String,
/// Node name
#[structopt(long = "node-name", env = "TRICOT_NODE_NAME", default_value = "<none>")]
pub node_name: String,
2021-12-07 17:19:51 +00:00
/// Bind address for HTTP server
#[structopt(long = "http-bind-addr", env = "TRICOT_HTTP_BIND_ADDR", default_value = "0.0.0.0:80")]
pub http_bind_addr: SocketAddr,
/// Bind address for HTTPS server
#[structopt(long = "https-bind-addr", env = "TRICOT_HTTPS_BIND_ADDR", default_value = "0.0.0.0:443")]
pub https_bind_addr: SocketAddr,
2021-12-07 16:56:15 +00:00
}
2021-12-07 14:20:45 +00:00
#[tokio::main(flavor = "multi_thread", worker_threads = 10)]
async fn main() {
2021-12-07 12:50:44 +00:00
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "tricot=debug")
}
pretty_env_logger::init();
2021-12-07 16:56:15 +00:00
let opt = Opt::from_args();
info!("Starting Tricot");
2021-12-07 16:56:15 +00:00
let consul = consul::Consul::new(&opt.consul_addr, &opt.consul_kv_prefix, &opt.node_name);
let mut rx_proxy_config = proxy_config::spawn_proxy_config_task(consul.clone());
2021-12-07 12:50:44 +00:00
let cert_store = cert_store::CertStore::new(consul.clone());
tokio::spawn(
cert_store
.clone()
.watch_proxy_config(rx_proxy_config.clone()),
);
2021-12-07 17:19:51 +00:00
tokio::spawn(http::serve_http(opt.http_bind_addr, consul.clone()));
2021-12-07 14:20:45 +00:00
tokio::spawn(https::serve_https(
2021-12-07 17:19:51 +00:00
opt.https_bind_addr,
2021-12-07 14:20:45 +00:00
cert_store.clone(),
rx_proxy_config.clone(),
));
2021-12-06 22:40:41 +00:00
while rx_proxy_config.changed().await.is_ok() {
2021-12-07 17:19:51 +00:00
info!("Proxy config:");
for ent in rx_proxy_config.borrow().entries.iter() {
info!(" {:?}", ent);
}
}
}