2024-05-22 08:41:30 +01:00
|
|
|
//
|
2024-04-07 20:16:04 +01:00
|
|
|
use actix::prelude::*;
|
2024-06-29 10:57:18 +01:00
|
|
|
use git_next_file_watcher_actor::{FileUpdated, FileWatcher};
|
2024-06-29 11:14:09 +01:00
|
|
|
use git_next_server_actor::Server;
|
2024-05-11 18:57:18 +01:00
|
|
|
use kxio::{fs::FileSystem, network::Network};
|
2024-04-07 11:52:47 +01:00
|
|
|
use std::path::PathBuf;
|
2024-07-05 07:23:40 +01:00
|
|
|
use tracing::{error, info, level_filters::LevelFilter};
|
2024-04-06 18:53:18 +01:00
|
|
|
|
2024-07-03 07:42:11 +01:00
|
|
|
pub use git_next_server_actor::{repository_factory, RepositoryFactory};
|
2024-07-03 07:35:01 +01:00
|
|
|
|
2024-04-07 16:09:16 +01:00
|
|
|
pub fn init(fs: FileSystem) {
|
2024-04-06 18:42:34 +01:00
|
|
|
let file_name = "git-next-server.toml";
|
2024-04-28 08:05:09 +01:00
|
|
|
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 {
|
2024-04-07 08:56:33 +01:00
|
|
|
eprintln!(
|
|
|
|
"The configuration file already exists at {} - not overwritting it.",
|
|
|
|
file_name
|
|
|
|
);
|
|
|
|
} else {
|
2024-05-11 18:57:18 +01:00
|
|
|
match fs.file_write(&pathbuf, include_str!("../../../server-default.toml")) {
|
2024-04-07 11:52:47 +01:00
|
|
|
Ok(_) => println!("Created a default configuration file at {}", file_name),
|
2024-04-06 18:42:34 +01:00
|
|
|
Err(e) => {
|
2024-04-07 11:52:47 +01:00
|
|
|
eprintln!("Failed to write to the configuration file: {}", e)
|
2024-04-06 18:42:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-06 18:53:18 +01:00
|
|
|
|
2024-07-01 06:54:07 +01:00
|
|
|
pub fn start(
|
2024-06-19 07:03:08 +01:00
|
|
|
fs: FileSystem,
|
|
|
|
net: Network,
|
|
|
|
repo: Box<dyn RepositoryFactory>,
|
|
|
|
sleep_duration: std::time::Duration,
|
|
|
|
) {
|
2024-04-27 15:23:42 +01:00
|
|
|
init_logging();
|
2024-05-04 12:37:35 +01:00
|
|
|
|
2024-05-07 19:32:15 +01:00
|
|
|
info!("Starting Server...");
|
2024-07-01 06:54:07 +01:00
|
|
|
let execution = async move {
|
|
|
|
let server = Server::new(fs.clone(), net.clone(), repo, sleep_duration).start();
|
|
|
|
server.do_send(FileUpdated);
|
2024-05-07 19:32:15 +01:00
|
|
|
|
2024-07-01 06:54:07 +01:00
|
|
|
info!("Starting File Watcher...");
|
2024-07-11 19:17:33 +01:00
|
|
|
let fw = match FileWatcher::new("git-next-server.toml".into(), server.clone().recipient()) {
|
2024-07-01 06:54:07 +01:00
|
|
|
Ok(fw) => fw,
|
|
|
|
Err(err) => {
|
|
|
|
error!(?err, "Failed to start file watcher");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
fw.start();
|
2024-05-04 12:37:35 +01:00
|
|
|
|
2024-07-01 06:54:07 +01:00
|
|
|
info!("Server running - Press Ctrl-C to stop...");
|
|
|
|
let _ = actix_rt::signal::ctrl_c().await;
|
|
|
|
info!("Ctrl-C received, shutting down...");
|
2024-07-11 19:17:33 +01:00
|
|
|
server.do_send(git_next_server_actor::messages::Shutdown);
|
2024-07-01 06:54:07 +01:00
|
|
|
System::current().stop();
|
|
|
|
};
|
|
|
|
let system = System::new();
|
|
|
|
Arbiter::current().spawn(execution);
|
|
|
|
if let Err(err) = system.run() {
|
|
|
|
tracing::error!(?err, "")
|
|
|
|
};
|
2024-04-09 07:20:13 +01:00
|
|
|
}
|
|
|
|
|
2024-04-27 15:23:42 +01:00
|
|
|
pub fn init_logging() {
|
2024-04-06 18:53:18 +01:00
|
|
|
use tracing_subscriber::prelude::*;
|
2024-07-05 07:23:40 +01:00
|
|
|
use tracing_subscriber::EnvFilter;
|
2024-04-06 18:53:18 +01:00
|
|
|
|
|
|
|
let subscriber = tracing_subscriber::fmt::layer()
|
|
|
|
.with_target(false)
|
|
|
|
.with_file(true)
|
2024-04-10 11:40:42 +01:00
|
|
|
.with_line_number(true)
|
2024-07-05 07:23:40 +01:00
|
|
|
.with_filter(
|
|
|
|
EnvFilter::builder()
|
|
|
|
.with_default_directive(LevelFilter::INFO.into())
|
|
|
|
.from_env_lossy(),
|
|
|
|
);
|
2024-04-06 18:53:18 +01:00
|
|
|
tracing_subscriber::registry()
|
2024-04-10 11:40:42 +01:00
|
|
|
.with(console_subscriber::ConsoleLayer::builder().spawn())
|
2024-04-06 18:53:18 +01:00
|
|
|
.with(subscriber)
|
|
|
|
.init();
|
|
|
|
}
|