syndicate-rs/syndicate-server/src/main.rs

145 lines
5.3 KiB
Rust
Raw Normal View History

2021-08-09 13:19:00 +00:00
use std::path::PathBuf;
use std::sync::Arc;
2021-08-11 21:48:04 +00:00
use structopt::StructOpt;
use syndicate::actor::*;
2021-08-28 16:50:55 +00:00
use syndicate::convert::from_io_value;
use syndicate::dataspace::*;
2021-08-28 16:50:55 +00:00
use syndicate::relay;
use syndicate::schemas::service;
use syndicate::schemas::transport_address;
2021-08-13 01:40:48 +00:00
use syndicate::value::NestedValue;
mod gatekeeper;
mod protocol;
mod services;
2020-05-18 08:44:57 +00:00
2021-08-28 16:50:55 +00:00
mod schemas {
include!(concat!(env!("OUT_DIR"), "/src/schemas/mod.rs"));
}
use schemas::internal_services;
2021-08-11 21:48:04 +00:00
#[derive(Clone, StructOpt)]
struct ServerConfig {
2021-08-28 16:55:02 +00:00
#[structopt(short = "p", long = "port")]
2021-08-11 21:48:04 +00:00
ports: Vec<u16>,
#[structopt(short = "s", long = "socket")]
sockets: Vec<PathBuf>,
2021-08-25 14:11:15 +00:00
2021-08-25 14:11:24 +00:00
#[structopt(long)]
inferior: bool,
2021-08-25 14:11:15 +00:00
#[structopt(long)]
debt_reporter: bool,
2021-08-30 10:08:11 +00:00
#[structopt(short = "c", long)]
config: Vec<PathBuf>,
2021-08-11 21:48:04 +00:00
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
2021-07-15 07:13:31 +00:00
syndicate::convenient_logging()?;
2020-05-18 12:33:36 +00:00
2021-08-11 21:48:04 +00:00
let config = Arc::new(ServerConfig::from_args());
2021-08-09 13:19:00 +00:00
2020-06-01 08:02:39 +00:00
{
const BRIGHT_GREEN: &str = "\x1b[92m";
2020-06-01 08:12:18 +00:00
const RED: &str = "\x1b[31m";
2020-06-01 08:02:39 +00:00
const GREEN: &str = "\x1b[32m";
const NORMAL: &str = "\x1b[0m";
const BRIGHT_YELLOW: &str = "\x1b[93m";
2020-06-01 08:12:18 +00:00
tracing::info!(r"{} ______ {}", GREEN, NORMAL);
tracing::info!(r"{} / {}\_{}\{} ", GREEN, BRIGHT_GREEN, GREEN, NORMAL);
tracing::info!(r"{} / {},{}__/{} \ {} ____ __", GREEN, RED, BRIGHT_GREEN, GREEN, NORMAL);
tracing::info!(r"{} /{}\__/ \{},{} \{} _______ ______ ____/ /_/________ / /____", GREEN, BRIGHT_GREEN, RED, GREEN, NORMAL);
tracing::info!(r"{} \{}/ \__/ {}/{} / ___/ / / / __ \/ __ / / ___/ __ \/ __/ _ \", GREEN, BRIGHT_GREEN, GREEN, NORMAL);
tracing::info!(r"{} \ {}'{} \__{}/ {} _\_ \/ /_/ / / / / /_/ / / /__/ /_/ / /_/ __/", GREEN, RED, BRIGHT_GREEN, GREEN, NORMAL);
tracing::info!(r"{} \____{}/{}_/ {} /____/\__, /_/ /_/\____/_/\___/\__/_/\__/\___/", GREEN, BRIGHT_GREEN, GREEN, NORMAL);
tracing::info!(r" /____/");
tracing::info!(r"");
tracing::info!(r" {}version {}{}", BRIGHT_YELLOW, env!("CARGO_PKG_VERSION"), NORMAL);
tracing::info!(r"");
tracing::info!(r" documentation & reference material: https://syndicate-lang.org/");
tracing::info!(r" source code & bugs: https://git.syndicate-lang.org/syndicate-lang/syndicate-rs");
tracing::info!(r"");
2020-06-01 08:02:39 +00:00
}
tracing::trace!("startup");
2020-06-01 08:12:18 +00:00
2021-08-27 14:35:45 +00:00
Actor::new().boot(tracing::Span::current(), move |t| {
let root_ds = Cap::new(&t.create(Dataspace::new()));
2021-08-28 16:50:55 +00:00
if config.inferior {
2021-08-28 16:55:08 +00:00
tracing::info!("inferior server instance");
2021-08-28 16:50:55 +00:00
let root_ds = Arc::clone(&root_ds);
t.spawn(syndicate::name!("parent"), move |t| protocol::run_io_relay(
t,
relay::Input::Bytes(Box::pin(tokio::io::stdin())),
relay::Output::Bytes(Box::pin(tokio::io::stdout())),
root_ds));
}
let server_config_ds = Cap::new(&t.create(Dataspace::new()));
2021-08-25 14:11:24 +00:00
gatekeeper::bind(t, &root_ds, AnyValue::new("syndicate"), [0; 16],
Arc::clone(&root_ds));
gatekeeper::bind(t, &root_ds, AnyValue::new("server-config"), [0; 16],
Arc::clone(&server_config_ds));
2021-08-28 16:50:55 +00:00
let gateway = Cap::guard(&t.create(
syndicate::entity(Arc::clone(&root_ds)).on_asserted(gatekeeper::handle_resolve)));
services::debt_reporter::on_demand(t, Arc::clone(&server_config_ds));
2021-08-28 16:50:55 +00:00
services::tcp_relay_listener::on_demand(t, Arc::clone(&server_config_ds), Arc::clone(&gateway));
services::unix_relay_listener::on_demand(t, Arc::clone(&server_config_ds), Arc::clone(&gateway));
2021-08-30 10:08:11 +00:00
services::config_watcher::on_demand(t, Arc::clone(&server_config_ds));
2021-08-28 16:50:55 +00:00
if config.debt_reporter {
2021-08-28 16:50:55 +00:00
server_config_ds.assert(t, &service::RequireService {
service_name: from_io_value(&internal_services::DebtReporter)?,
});
}
2021-08-27 13:31:18 +00:00
for port in config.ports.clone() {
2021-08-28 16:50:55 +00:00
server_config_ds.assert(t, &service::RequireService {
service_name: from_io_value(
&internal_services::TcpRelayListener {
addr: transport_address::Tcp {
host: "0.0.0.0".to_owned(),
port: (port as i32).into(),
}
})?,
});
2021-08-27 13:31:18 +00:00
}
for path in config.sockets.clone() {
2021-08-28 16:50:55 +00:00
server_config_ds.assert(t, &service::RequireService {
service_name: from_io_value(
&internal_services::UnixRelayListener {
addr: transport_address::Unix {
path: path.to_str().expect("representable UnixListener path").to_owned(),
}
})?,
});
2021-08-27 13:31:18 +00:00
}
2021-08-30 10:08:11 +00:00
for path in config.config.clone() {
server_config_ds.assert(t, &service::RequireService {
service_name: from_io_value(
&internal_services::ConfigWatcher {
path: path.to_str().expect("representable ConfigWatcher path").to_owned(),
})?,
});
}
2021-08-27 13:31:18 +00:00
Ok(())
}).await??;
Ok(())
}