3510764cc6
`backup [NAME]` zips the world and the settings into backups/; on a running server it turns saving off, flushes, zips and turns saving back on, so the game stays up. `restore BACKUP` puts a backup's world in place and keeps the current one in backups/replaced-<time>/; a running server restarts with it. `[backups] every = "6h"`, `keep = 12` in server.toml back up on a schedule and prune the automatic backups only. The control API gains GET/POST /api/v1/backups and POST /api/v1/restore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
6.2 KiB
Rust
166 lines
6.2 KiB
Rust
//! `horizon-server`: host a Horizon server (docs/server.md). It keeps the server's settings in `server.toml`,
|
|
//! runs the game headless as the engine, and manages the vanilla files around it.
|
|
|
|
mod api;
|
|
mod backup;
|
|
mod cli;
|
|
mod config;
|
|
mod engine;
|
|
mod http;
|
|
mod init;
|
|
mod lists;
|
|
mod signals;
|
|
mod supervisor;
|
|
|
|
use std::{path::PathBuf, process::ExitCode};
|
|
|
|
const HELP: &str = "\
|
|
horizon-server: host a Horizon (Minecraft Java Edition 26.3) server
|
|
|
|
Usage: horizon-server [--dir DIR] <command>
|
|
|
|
init Set up a server here: the EULA, the world, the port, who may join (asks, or takes flags:
|
|
--eula --world NAME --seed SEED --port N --whitelist/--no-whitelist --player NAME... --op NAME
|
|
--yes)
|
|
run Run the server in the foreground; type console commands, Ctrl-C stops it cleanly. It
|
|
restarts the game if it crashes.
|
|
start Run the server in the background (its output goes to logs/supervisor.log)
|
|
stop, restart
|
|
status [--json]
|
|
Up or not, TPS, MSPT, players, RAM and CPU
|
|
cmd COMMAND...
|
|
Run a console command on the running server and print what it answers
|
|
logs [-f] [-n N]
|
|
The last N lines of the log (default 50); -f follows it
|
|
backup [NAME]
|
|
Zip the world and the settings into backups/ (a running server saves first and stays up)
|
|
backups List the backups
|
|
restore BACKUP [--yes]
|
|
Put a backup's world in place (the current one is kept in backups/); a running
|
|
server restarts with it
|
|
import FILE Write server.toml from a vanilla server.properties
|
|
op NAME, deop NAME
|
|
whitelist on|off|list, whitelist add|remove NAME
|
|
Who may join (on the running server, or in the files when it is stopped)
|
|
|
|
--dir DIR The server folder (default: the current folder)
|
|
|
|
Settings live in server.toml; the server writes server.properties from it before every start.
|
|
";
|
|
|
|
fn main() -> ExitCode {
|
|
let mut args: Vec<String> = std::env::args().skip(1).collect();
|
|
let mut dir = PathBuf::from(".");
|
|
if let Some(i) = args.iter().position(|a| a == "--dir") {
|
|
if i + 1 >= args.len() {
|
|
eprintln!("horizon-server: --dir needs a folder");
|
|
return ExitCode::from(2);
|
|
}
|
|
dir = PathBuf::from(args.remove(i + 1));
|
|
args.remove(i);
|
|
}
|
|
let result = match args.first().map(String::as_str) {
|
|
None | Some("help" | "-h" | "--help") => {
|
|
print!("{HELP}");
|
|
Ok(())
|
|
}
|
|
Some("version" | "-V" | "--version") => {
|
|
println!("horizon-server {}", env!("CARGO_PKG_VERSION"));
|
|
Ok(())
|
|
}
|
|
Some("init") => init_answers(&args[1..]).and_then(|a| init::run(&dir, a)),
|
|
Some("run") => supervisor::run(&dir),
|
|
Some("start") => cli::start(&dir),
|
|
Some("stop") => cli::stop(&dir, false),
|
|
Some("restart") => cli::stop(&dir, true),
|
|
Some("status") => cli::status(&dir, args[1..].iter().any(|a| a == "--json")),
|
|
Some("cmd") => cli::command(&dir, &args[1..]),
|
|
Some("logs") => logs(&dir, &args[1..]),
|
|
Some("backup") => cli::backup(&dir, args.get(1).map(String::as_str)),
|
|
Some("backups") => cli::backups(&dir),
|
|
Some("restore") => match args.get(1) {
|
|
Some(name) => cli::restore(&dir, name, args[2..].iter().any(|a| a == "--yes")),
|
|
None => Err("restore needs a backup (`horizon-server backups` lists them)".into()),
|
|
},
|
|
Some("import") => import(&dir, &args[1..]),
|
|
Some(command @ ("op" | "deop" | "whitelist")) => lists::run(&dir, command, &args[1..]),
|
|
Some(other) => Err(format!(
|
|
"unknown command {other}; `horizon-server help` lists them"
|
|
)),
|
|
};
|
|
match result {
|
|
Ok(()) => ExitCode::SUCCESS,
|
|
Err(e) => {
|
|
eprintln!("horizon-server: {e}");
|
|
ExitCode::FAILURE
|
|
}
|
|
}
|
|
}
|
|
|
|
fn logs(dir: &std::path::Path, args: &[String]) -> Result<(), String> {
|
|
let mut follow = false;
|
|
let mut count = 50;
|
|
let mut it = args.iter();
|
|
while let Some(arg) = it.next() {
|
|
match arg.as_str() {
|
|
"-f" | "--follow" => follow = true,
|
|
"-n" => {
|
|
count = it
|
|
.next()
|
|
.and_then(|n| n.parse().ok())
|
|
.ok_or("-n needs a number of lines")?;
|
|
}
|
|
other => return Err(format!("logs: unknown option {other}")),
|
|
}
|
|
}
|
|
cli::logs(dir, follow, count)
|
|
}
|
|
|
|
fn init_answers(args: &[String]) -> Result<init::Answers, String> {
|
|
let mut answers = init::Answers::default();
|
|
let mut it = args.iter();
|
|
while let Some(arg) = it.next() {
|
|
let mut value = |flag: &str| {
|
|
it.next()
|
|
.cloned()
|
|
.ok_or_else(|| format!("{flag} needs a value"))
|
|
};
|
|
match arg.as_str() {
|
|
"--eula" => answers.eula = true,
|
|
"--yes" | "-y" => answers.yes = true,
|
|
"--world" => answers.world = Some(value(arg)?),
|
|
"--seed" => answers.seed = Some(value(arg)?),
|
|
"--port" => {
|
|
answers.port = Some(
|
|
value(arg)?
|
|
.parse()
|
|
.map_err(|_| "--port needs a number from 1 to 65535")?,
|
|
);
|
|
}
|
|
"--whitelist" => answers.whitelist = Some(true),
|
|
"--no-whitelist" => answers.whitelist = Some(false),
|
|
"--player" => answers.players.push(value(arg)?),
|
|
"--op" => answers.op = Some(value(arg)?),
|
|
other => return Err(format!("init: unknown option {other}")),
|
|
}
|
|
}
|
|
Ok(answers)
|
|
}
|
|
|
|
fn import(dir: &std::path::Path, args: &[String]) -> Result<(), String> {
|
|
let [source] = args else {
|
|
return Err("import needs the server.properties to read".into());
|
|
};
|
|
let target = dir.join(config::FILE);
|
|
if target.exists() {
|
|
return Err(format!(
|
|
"{} already exists; move it away first",
|
|
target.display()
|
|
));
|
|
}
|
|
let cfg = config::Config::import(&config::read_properties(std::path::Path::new(source))?)?;
|
|
std::fs::write(&target, config::render(&cfg)).map_err(|e| e.to_string())?;
|
|
println!("Wrote {}", target.display());
|
|
Ok(())
|
|
}
|