git-next/crates/cli/src/server/mod.rs
Paul Campbell f6bc2e1283
All checks were successful
Rust / build (push) Successful in 1m20s
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
feat: terminate process if config file is invalid
2024-07-30 16:27:24 +01:00

80 lines
2.3 KiB
Rust

//
mod actor;
#[cfg(test)]
mod tests;
use actix::prelude::*;
use crate::file_watcher::{watch_file, FileUpdated};
use actor::ServerActor;
use git_next_core::git::RepositoryFactory;
use anyhow::{Context, Result};
use kxio::{fs::FileSystem, network::Network};
use tracing::info;
use std::path::PathBuf;
pub fn init(fs: FileSystem) -> Result<()> {
let file_name = "git-next-server.toml";
let pathbuf = PathBuf::from(file_name);
if fs
.path_exists(&pathbuf)
.with_context(|| format!("Checking for existing file: {pathbuf:?}"))?
{
eprintln!("The configuration file already exists at {pathbuf:?} - not overwritting it.",);
} else {
fs.file_write(&pathbuf, include_str!("server-default.toml"))
.with_context(|| format!("Writing file: {pathbuf:?}"))?;
println!("Created a default configuration file at {pathbuf:?}",);
}
Ok(())
}
pub fn start(
fs: FileSystem,
net: Network,
repo: Box<dyn RepositoryFactory>,
sleep_duration: std::time::Duration,
) -> Result<()> {
init_logging();
let execution = async move {
info!("Starting Server...");
let server = ServerActor::new(fs.clone(), net.clone(), repo, sleep_duration).start();
server.do_send(FileUpdated);
info!("Starting File Watcher...");
#[allow(clippy::expect_used)]
watch_file("git-next-server.toml".into(), server.clone().recipient())
.await
.expect("file watcher");
info!("Server running - Press Ctrl-C to stop...");
let _ = actix_rt::signal::ctrl_c().await;
info!("Ctrl-C received, shutting down...");
server.do_send(crate::server::actor::messages::Shutdown);
actix_rt::time::sleep(std::time::Duration::from_millis(200)).await;
System::current().stop();
};
let system = System::new();
Arbiter::current().spawn(execution);
system.run()?;
Ok(())
}
pub fn init_logging() {
use tracing::Level;
use tracing_subscriber::FmtSubscriber;
let subscriber = FmtSubscriber::builder()
.with_target(false)
.with_file(true)
.with_line_number(true)
.with_max_level(Level::INFO)
.finish();
#[allow(clippy::expect_used)]
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
}