git-next/crates/server/src/lib.rs
Paul Campbell 94ad2c441c
All checks were successful
Rust / build (push) Successful in 1m14s
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
refactor: create a RepositoryFactory trait
2024-06-20 18:28:01 +01:00

76 lines
2.1 KiB
Rust

mod actors;
mod config;
//
use actix::prelude::*;
use git_next_git::repository::RepositoryFactory;
use kxio::{fs::FileSystem, network::Network};
use std::path::PathBuf;
use tracing::{error, info, level_filters::LevelFilter};
use crate::actors::{
file_watcher::{self, FileUpdated},
server::Server,
};
pub fn init(fs: FileSystem) {
let file_name = "git-next-server.toml";
let pathbuf = PathBuf::from(file_name);
let Ok(exists) = fs.path_exists(&pathbuf) else {
eprintln!("Could not check if file exist: {}", file_name);
return;
};
if exists {
eprintln!(
"The configuration file already exists at {} - not overwritting it.",
file_name
);
} else {
match fs.file_write(&pathbuf, include_str!("../../../server-default.toml")) {
Ok(_) => println!("Created a default configuration file at {}", file_name),
Err(e) => {
eprintln!("Failed to write to the configuration file: {}", e)
}
}
}
}
pub async fn start(fs: FileSystem, net: Network, repo: Box<dyn RepositoryFactory>) {
init_logging();
info!("Starting Server...");
let server = Server::new(fs.clone(), net.clone(), repo).start();
server.do_send(FileUpdated);
info!("Starting File Watcher...");
let fw = match file_watcher::FileWatcher::new("git-next-server.toml".into(), server.recipient())
{
Ok(fw) => fw,
Err(err) => {
error!(?err, "Failed to start file watcher");
return;
}
};
fw.start();
info!("Server running - Press Ctrl-C to stop...");
let _ = actix_rt::signal::ctrl_c().await;
info!("Ctrl-C received, shutting down...");
}
pub fn init_logging() {
use tracing_subscriber::prelude::*;
let subscriber = tracing_subscriber::fmt::layer()
// NOTE: set RUSTLOG in ${root}/.cargo/config
.with_target(false)
.with_file(true)
.with_line_number(true)
.with_filter(LevelFilter::INFO);
tracing_subscriber::registry()
.with(console_subscriber::ConsoleLayer::builder().spawn())
.with(subscriber)
.init();
}