A room is one conversation, one ssh sandbox, and one agent. Accounts create rooms; friends join through a single-use invite link and a display name; the agent speaks only through the message tool. The server is Axum and SQLite. It serves the built Nuxt client, streams room events over SSE, and runs one serialized turn loop per room with turn-level steering copied from rhema-code. The client has the chat, preview iframe, read-only file pane, xterm terminal, and presence. Preview URLs are signed wildcard hosts, and the proxy tunnels HTTP over ssh direct-tcpip. HostAgent::step remains deferred in the framework.
93 lines
2.5 KiB
Rust
93 lines
2.5 KiB
Rust
mod api;
|
|
mod auth;
|
|
mod config;
|
|
mod crypto;
|
|
mod db;
|
|
mod error;
|
|
mod models;
|
|
mod presence;
|
|
mod preview;
|
|
mod room;
|
|
mod ssh;
|
|
mod state;
|
|
mod util;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tower_http::trace::TraceLayer;
|
|
|
|
use crate::config::Config;
|
|
use crate::crypto::Secrets;
|
|
use crate::db::Db;
|
|
use crate::state::{AppState, Hub};
|
|
|
|
#[tokio::main]
|
|
async fn main() -> anyhow::Result<()> {
|
|
tracing_subscriber::fmt()
|
|
.with_env_filter(
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
|
.unwrap_or_else(|_| "rhema_together=info,tower_http=warn".into()),
|
|
)
|
|
.init();
|
|
let config_path = std::env::args()
|
|
.nth(1)
|
|
.map(std::path::PathBuf::from)
|
|
.unwrap_or_else(|| std::path::PathBuf::from("together.yaml"));
|
|
let config = Config::load(&config_path)?;
|
|
let db = Db::open(&config.database)?;
|
|
let secrets = Secrets::load_or_create(&config.secret_key_file)?;
|
|
print_setup_token(&db, &config);
|
|
let state = Arc::new(AppState {
|
|
config: config.clone(),
|
|
db,
|
|
secrets,
|
|
hub: Hub::default(),
|
|
rooms: Default::default(),
|
|
previews: Default::default(),
|
|
presence: Default::default(),
|
|
});
|
|
let app = api::router(state).layer(TraceLayer::new_for_http());
|
|
let listener = tokio::net::TcpListener::bind(&config.listen).await?;
|
|
tracing::info!(listen = %config.listen, "rhema-together");
|
|
axum::serve(listener, app).await?;
|
|
Ok(())
|
|
}
|
|
|
|
/// On a server with no accounts, keeps a first-run token and prints it.
|
|
/// The token survives restarts, so the banner stays valid.
|
|
fn print_setup_token(db: &Db, config: &Config) {
|
|
let accounts: i64 = db
|
|
.scalar("SELECT COUNT(*) FROM accounts", rusqlite::params![])
|
|
.unwrap_or(0);
|
|
if accounts > 0 {
|
|
return;
|
|
}
|
|
let existing: Option<String> = db
|
|
.conn()
|
|
.query_row(
|
|
"SELECT value FROM meta WHERE key = 'setup_token'",
|
|
[],
|
|
|row| row.get(0),
|
|
)
|
|
.ok();
|
|
let token = match existing {
|
|
Some(token) => token,
|
|
None => {
|
|
let token = crate::util::random_token(18);
|
|
let _ = db.conn().execute(
|
|
"INSERT OR REPLACE INTO meta (key, value) VALUES ('setup_token', ?1)",
|
|
rusqlite::params![token],
|
|
);
|
|
token
|
|
}
|
|
};
|
|
eprintln!();
|
|
eprintln!(
|
|
" First run. Open {}/setup and enter this token:",
|
|
config.public_url
|
|
);
|
|
eprintln!();
|
|
eprintln!(" {token}");
|
|
eprintln!();
|
|
}
|