2021-12-19 16:43:13 +00:00
|
|
|
mod api;
|
2021-07-12 20:14:39 +00:00
|
|
|
mod auth;
|
2021-08-06 02:06:50 +00:00
|
|
|
mod db;
|
2021-07-12 20:14:39 +00:00
|
|
|
mod env;
|
|
|
|
mod rpc;
|
|
|
|
|
2022-09-16 19:25:42 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod db_tests;
|
2022-05-28 00:20:05 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod integration_tests;
|
|
|
|
|
2022-10-20 12:34:05 +00:00
|
|
|
use crate::rpc::ResultExt as _;
|
2022-10-21 20:15:34 +00:00
|
|
|
use anyhow::anyhow;
|
2022-10-19 22:31:47 +00:00
|
|
|
use axum::{routing::get, Router};
|
2022-05-26 22:40:46 +00:00
|
|
|
use collab::{Error, Result};
|
2022-11-10 03:28:06 +00:00
|
|
|
use db::DefaultDb as Db;
|
2022-04-21 14:57:49 +00:00
|
|
|
use serde::Deserialize;
|
2022-04-26 17:15:41 +00:00
|
|
|
use std::{
|
2022-10-21 20:15:34 +00:00
|
|
|
env::args,
|
2022-04-26 17:15:41 +00:00
|
|
|
net::{SocketAddr, TcpListener},
|
2022-10-21 20:15:34 +00:00
|
|
|
path::PathBuf,
|
2022-04-26 17:15:41 +00:00
|
|
|
sync::Arc,
|
2022-06-21 02:39:48 +00:00
|
|
|
time::Duration,
|
2022-04-26 17:15:41 +00:00
|
|
|
};
|
2022-10-20 12:34:05 +00:00
|
|
|
use tokio::signal;
|
2022-05-12 15:58:17 +00:00
|
|
|
use tracing_log::LogTracer;
|
2022-05-17 15:11:43 +00:00
|
|
|
use tracing_subscriber::{filter::EnvFilter, fmt::format::JsonFields, Layer};
|
2022-05-12 15:58:17 +00:00
|
|
|
use util::ResultExt;
|
2021-07-12 20:14:39 +00:00
|
|
|
|
2022-10-19 22:31:47 +00:00
|
|
|
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
|
|
|
|
2021-07-12 20:14:39 +00:00
|
|
|
#[derive(Default, Deserialize)]
|
|
|
|
pub struct Config {
|
|
|
|
pub http_port: u16,
|
|
|
|
pub database_url: String,
|
2021-12-19 16:43:13 +00:00
|
|
|
pub api_token: String,
|
2022-05-19 23:57:46 +00:00
|
|
|
pub invite_link_prefix: String,
|
2022-10-17 10:20:55 +00:00
|
|
|
pub live_kit_server: Option<String>,
|
|
|
|
pub live_kit_key: Option<String>,
|
|
|
|
pub live_kit_secret: Option<String>,
|
2022-05-12 15:58:17 +00:00
|
|
|
pub rust_log: Option<String>,
|
2022-05-17 15:11:43 +00:00
|
|
|
pub log_json: Option<bool>,
|
2021-07-12 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 20:15:34 +00:00
|
|
|
#[derive(Default, Deserialize)]
|
|
|
|
pub struct MigrateConfig {
|
|
|
|
pub database_url: String,
|
|
|
|
pub migrations_path: Option<PathBuf>,
|
|
|
|
}
|
|
|
|
|
2021-07-12 20:14:39 +00:00
|
|
|
pub struct AppState {
|
2022-11-10 03:28:06 +00:00
|
|
|
db: Arc<Db>,
|
2022-10-19 14:35:34 +00:00
|
|
|
live_kit_client: Option<Arc<dyn live_kit_server::api::Client>>,
|
2022-10-18 20:02:30 +00:00
|
|
|
config: Config,
|
2021-07-12 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppState {
|
2022-10-18 20:02:30 +00:00
|
|
|
async fn new(config: Config) -> Result<Arc<Self>> {
|
2022-11-10 03:28:06 +00:00
|
|
|
let db = Db::new(&config.database_url, 5).await?;
|
2022-10-17 10:20:55 +00:00
|
|
|
let live_kit_client = if let Some(((server, key), secret)) = config
|
|
|
|
.live_kit_server
|
|
|
|
.as_ref()
|
|
|
|
.zip(config.live_kit_key.as_ref())
|
|
|
|
.zip(config.live_kit_secret.as_ref())
|
|
|
|
{
|
2022-10-19 14:35:34 +00:00
|
|
|
Some(Arc::new(live_kit_server::api::LiveKitClient::new(
|
2022-10-17 10:20:55 +00:00
|
|
|
server.clone(),
|
|
|
|
key.clone(),
|
|
|
|
secret.clone(),
|
2022-10-19 14:35:34 +00:00
|
|
|
)) as Arc<dyn live_kit_server::api::Client>)
|
2022-10-17 10:20:55 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2021-07-12 20:14:39 +00:00
|
|
|
let this = Self {
|
2022-02-17 16:04:04 +00:00
|
|
|
db: Arc::new(db),
|
2022-10-17 10:20:55 +00:00
|
|
|
live_kit_client,
|
2022-10-18 20:02:30 +00:00
|
|
|
config,
|
2021-07-12 20:14:39 +00:00
|
|
|
};
|
|
|
|
Ok(Arc::new(this))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 19:33:19 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2021-07-12 20:14:39 +00:00
|
|
|
if let Err(error) = env::load_dotenv() {
|
2022-05-12 16:05:49 +00:00
|
|
|
eprintln!(
|
2021-07-12 20:14:39 +00:00
|
|
|
"error loading .env.toml (this is expected in production): {}",
|
|
|
|
error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-21 20:15:34 +00:00
|
|
|
match args().skip(1).next().as_deref() {
|
|
|
|
Some("version") => {
|
|
|
|
println!("collab v{VERSION}");
|
|
|
|
}
|
|
|
|
Some("migrate") => {
|
|
|
|
let config = envy::from_env::<MigrateConfig>().expect("error loading config");
|
2022-11-10 03:28:06 +00:00
|
|
|
let db = Db::new(&config.database_url, 5).await?;
|
2022-10-21 20:15:34 +00:00
|
|
|
|
|
|
|
let migrations_path = config
|
|
|
|
.migrations_path
|
|
|
|
.as_deref()
|
|
|
|
.or(db::DEFAULT_MIGRATIONS_PATH.map(|s| s.as_ref()))
|
|
|
|
.ok_or_else(|| anyhow!("missing MIGRATIONS_PATH environment variable"))?;
|
|
|
|
|
|
|
|
let migrations = db.migrate(&migrations_path, false).await?;
|
|
|
|
for (migration, duration) in migrations {
|
|
|
|
println!(
|
|
|
|
"Ran {} {} {:?}",
|
|
|
|
migration.version, migration.description, duration
|
|
|
|
);
|
|
|
|
}
|
2021-09-29 17:15:19 +00:00
|
|
|
|
2022-10-21 20:15:34 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Some("serve") => {
|
|
|
|
let config = envy::from_env::<Config>().expect("error loading config");
|
|
|
|
init_tracing(&config);
|
2022-06-21 02:39:48 +00:00
|
|
|
|
2022-10-24 18:46:01 +00:00
|
|
|
let state = AppState::new(config).await?;
|
2022-10-21 20:15:34 +00:00
|
|
|
let listener = TcpListener::bind(&format!("0.0.0.0:{}", state.config.http_port))
|
|
|
|
.expect("failed to bind TCP listener");
|
2021-07-12 20:14:39 +00:00
|
|
|
|
2022-10-21 20:15:34 +00:00
|
|
|
let rpc_server = rpc::Server::new(state.clone(), None);
|
|
|
|
rpc_server
|
|
|
|
.start_recording_project_activity(Duration::from_secs(5 * 60), rpc::RealExecutor);
|
2021-07-12 20:14:39 +00:00
|
|
|
|
2022-10-24 18:46:01 +00:00
|
|
|
let app = api::routes(rpc_server.clone(), state.clone())
|
|
|
|
.merge(rpc::routes(rpc_server.clone()))
|
2022-10-21 20:15:34 +00:00
|
|
|
.merge(Router::new().route("/", get(handle_root)));
|
|
|
|
|
|
|
|
axum::Server::from_tcp(listener)?
|
|
|
|
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
|
2022-10-24 18:46:01 +00:00
|
|
|
.with_graceful_shutdown(graceful_shutdown(rpc_server, state))
|
2022-10-21 20:15:34 +00:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
Err(anyhow!("usage: collab <version | migrate | serve>"))?;
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 20:14:39 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-25 00:02:14 +00:00
|
|
|
|
2022-10-19 22:31:47 +00:00
|
|
|
async fn handle_root() -> String {
|
|
|
|
format!("collab v{VERSION}")
|
|
|
|
}
|
|
|
|
|
2022-04-29 14:45:29 +00:00
|
|
|
pub fn init_tracing(config: &Config) -> Option<()> {
|
2022-04-27 21:26:54 +00:00
|
|
|
use std::str::FromStr;
|
2022-04-27 17:48:43 +00:00
|
|
|
use tracing_subscriber::layer::SubscriberExt;
|
2022-05-12 16:16:50 +00:00
|
|
|
let rust_log = config.rust_log.clone()?;
|
2022-05-12 15:58:17 +00:00
|
|
|
|
|
|
|
LogTracer::init().log_err()?;
|
2022-04-27 17:48:43 +00:00
|
|
|
|
|
|
|
let subscriber = tracing_subscriber::Registry::default()
|
2022-05-17 15:11:43 +00:00
|
|
|
.with(if config.log_json.unwrap_or(false) {
|
|
|
|
Box::new(
|
|
|
|
tracing_subscriber::fmt::layer()
|
|
|
|
.fmt_fields(JsonFields::default())
|
|
|
|
.event_format(
|
|
|
|
tracing_subscriber::fmt::format()
|
|
|
|
.json()
|
|
|
|
.flatten_event(true)
|
|
|
|
.with_span_list(true),
|
|
|
|
),
|
|
|
|
) as Box<dyn Layer<_> + Send + Sync>
|
|
|
|
} else {
|
|
|
|
Box::new(
|
|
|
|
tracing_subscriber::fmt::layer()
|
|
|
|
.event_format(tracing_subscriber::fmt::format().pretty()),
|
|
|
|
)
|
|
|
|
})
|
2022-05-12 16:16:50 +00:00
|
|
|
.with(EnvFilter::from_str(rust_log.as_str()).log_err()?);
|
2022-04-27 17:48:43 +00:00
|
|
|
|
|
|
|
tracing::subscriber::set_global_default(subscriber).unwrap();
|
|
|
|
|
2022-04-29 14:45:29 +00:00
|
|
|
None
|
2022-04-22 17:55:10 +00:00
|
|
|
}
|
2022-10-20 12:34:05 +00:00
|
|
|
|
|
|
|
async fn graceful_shutdown(rpc_server: Arc<rpc::Server>, state: Arc<AppState>) {
|
|
|
|
let ctrl_c = async {
|
|
|
|
signal::ctrl_c()
|
|
|
|
.await
|
|
|
|
.expect("failed to install Ctrl+C handler");
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
let terminate = async {
|
|
|
|
signal::unix::signal(signal::unix::SignalKind::terminate())
|
|
|
|
.expect("failed to install signal handler")
|
|
|
|
.recv()
|
|
|
|
.await;
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
let terminate = std::future::pending::<()>();
|
|
|
|
|
|
|
|
tokio::select! {
|
|
|
|
_ = ctrl_c => {},
|
|
|
|
_ = terminate => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(live_kit) = state.live_kit_client.as_ref() {
|
|
|
|
let deletions = rpc_server
|
|
|
|
.store()
|
|
|
|
.await
|
|
|
|
.rooms()
|
|
|
|
.values()
|
|
|
|
.map(|room| {
|
|
|
|
let name = room.live_kit_room.clone();
|
|
|
|
async {
|
|
|
|
live_kit.delete_room(name).await.trace_err();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
tracing::info!("deleting all live-kit rooms");
|
|
|
|
if let Err(_) = tokio::time::timeout(
|
|
|
|
Duration::from_secs(10),
|
|
|
|
futures::future::join_all(deletions),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
tracing::error!("timed out waiting for live-kit room deletion");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|