aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 79c366b1d8b9d24b72525805832f61c9367af5c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#[macro_use]
extern crate anyhow;

use std::net::SocketAddr;
use structopt::StructOpt;

mod cert;
mod cert_store;
mod consul;
mod http;
mod https;
mod proxy_config;
mod reverse_proxy;

use log::*;

#[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,

	/// 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,
}

#[tokio::main(flavor = "multi_thread", worker_threads = 10)]
async fn main() {
	if std::env::var("RUST_LOG").is_err() {
		std::env::set_var("RUST_LOG", "tricot=info")
	}
	pretty_env_logger::init();

	let opt = Opt::from_args();

	info!("Starting Tricot");

	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());

	let cert_store = cert_store::CertStore::new(consul.clone(), rx_proxy_config.clone());
	tokio::spawn(cert_store.clone().watch_proxy_config());

	tokio::spawn(http::serve_http(opt.http_bind_addr, consul.clone()));
	tokio::spawn(https::serve_https(
		opt.https_bind_addr,
		cert_store.clone(),
		rx_proxy_config.clone(),
	));

	while rx_proxy_config.changed().await.is_ok() {
		info!("Proxy config:");
		for ent in rx_proxy_config.borrow().entries.iter() {
			info!("   {}", ent);
		}
	}
}