Files
rhema-together/src/main.rs
T
vhaudiquet a40a7a6b36
ci / checks (push) Successful in 51s
ci / test (push) Successful in 57s
ci / chart (push) Successful in 5s
ci / web (push) Successful in 19s
ci / docker (push) Successful in 1m42s
ci / publish-edge-image (push) Successful in 10s
ci / publish-edge-chart (push) Successful in 3s
room: close turns a restart left open
A turn writes turn_start before it runs and turn_end only after the
work settles. A restart in between leaves the log with one open turn,
and the client replays it as running, so the room shows
"Rhema is working..." over no task.

At startup, close every start without an end with an interrupted
turn_end, which the log already renders as "rhema stopped".
2026-12-03 12:45:17 +01:00

104 lines
2.9 KiB
Rust

mod api;
mod auth;
mod config;
mod crypto;
mod db;
mod error;
mod login;
mod models;
mod presence;
mod preview;
mod room;
mod ssh;
mod state;
mod util;
mod worker;
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_writer(std::io::stderr)
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "rhema_together=info,tower_http=warn".into()),
)
.init();
let mut args = std::env::args().skip(1);
let first = args.next();
if first.as_deref() == Some("worker") {
return worker::run_worker().await;
}
let config_path = first
.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(),
workers: Default::default(),
presence: Default::default(),
logins: Default::default(),
});
// A previous process may have stopped between a turn's start and its
// end. Close those turns before the feed can replay them as running.
room::runner::close_open_turns(&state);
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!();
}