git-next/src/server/mod.rs

79 lines
2.2 KiB
Rust
Raw Normal View History

mod actors;
2024-04-07 13:47:39 +01:00
mod config;
pub mod gitforge;
pub mod types;
2024-04-07 13:47:39 +01:00
2024-04-07 20:16:04 +01:00
use actix::prelude::*;
2024-04-09 10:44:01 +01:00
use kxio::network::Network;
2024-04-07 20:16:04 +01:00
use std::path::PathBuf;
use tracing::{error, info, level_filters::LevelFilter};
use crate::{
fs::FileSystem,
server::{actors::server::Server, types::ServerGeneration},
};
2024-04-07 16:09:16 +01:00
pub fn init(fs: FileSystem) {
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 {
eprintln!(
"The configuration file already exists at {} - not overwritting it.",
file_name
);
} else {
2024-04-28 08:05:09 +01:00
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) {
init_logging();
let generation = ServerGeneration::new();
{
let span = tracing::info_span!("Server", %generation);
let _guard = span.enter();
info!("Starting Server...");
let server_config = match config::ServerConfig::load(&fs) {
Ok(server_config) => server_config,
Err(err) => {
error!("Failed to load config file. Error: {}", err);
return;
}
};
let server = Server::new(generation, fs.clone(), net.clone()).start();
server.do_send(server_config);
}
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();
}