2024-04-07 19:41:21 +01:00
|
|
|
mod actors;
|
2024-04-07 13:47:39 +01:00
|
|
|
mod config;
|
2024-04-09 10:44:01 +01:00
|
|
|
pub mod forge;
|
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
|
|
|
|
2024-04-07 11:52:47 +01:00
|
|
|
use std::path::PathBuf;
|
2024-04-06 18:42:34 +01:00
|
|
|
|
2024-04-10 11:40:42 +01:00
|
|
|
use tracing::{error, info, level_filters::LevelFilter};
|
2024-04-06 18:53:18 +01:00
|
|
|
|
2024-04-07 20:16:04 +01:00
|
|
|
use crate::{
|
|
|
|
filesystem::FileSystem,
|
2024-04-09 07:20:13 +01:00
|
|
|
server::config::{Forge, ForgeName, RepoName},
|
2024-04-07 20:16:04 +01:00
|
|
|
};
|
2024-04-07 11:52:47 +01:00
|
|
|
|
2024-04-09 07:20:13 +01:00
|
|
|
use self::{actors::repo::RepoActor, config::Repo};
|
|
|
|
|
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-07 11:52:47 +01:00
|
|
|
let path = PathBuf::from(file_name);
|
|
|
|
if fs.file_exists(&path) {
|
2024-04-07 08:56:33 +01:00
|
|
|
eprintln!(
|
|
|
|
"The configuration file already exists at {} - not overwritting it.",
|
|
|
|
file_name
|
|
|
|
);
|
|
|
|
} else {
|
2024-04-07 12:18:33 +01:00
|
|
|
match fs.write_file(file_name, 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-04-09 10:44:01 +01:00
|
|
|
pub fn start(fs: FileSystem, net: Network) {
|
2024-04-06 18:53:18 +01:00
|
|
|
let Ok(_) = init_logging() else {
|
|
|
|
eprintln!("Failed to initialize logging.");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
info!("Starting Server...");
|
2024-04-08 08:31:45 +01:00
|
|
|
let config = match config::ServerConfig::load(&fs) {
|
2024-04-07 18:37:01 +01:00
|
|
|
Ok(config) => config,
|
|
|
|
Err(err) => {
|
|
|
|
error!("Failed to load config file. Error: {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2024-04-09 07:20:13 +01:00
|
|
|
|
2024-04-07 18:37:01 +01:00
|
|
|
info!("Config loaded");
|
2024-04-09 07:20:13 +01:00
|
|
|
let actors = config
|
|
|
|
.forges()
|
2024-04-09 10:44:01 +01:00
|
|
|
.flat_map(|(forge_name, forge)| create_forge_repos(forge, forge_name, &net))
|
2024-04-09 07:20:13 +01:00
|
|
|
.collect::<Vec<_>>();
|
2024-04-07 20:16:04 +01:00
|
|
|
|
|
|
|
actix::System::new().block_on(async {
|
2024-04-09 07:20:13 +01:00
|
|
|
let addresses = actors.into_iter().map(start_actor).collect::<Vec<_>>();
|
2024-04-07 20:16:04 +01:00
|
|
|
let _ = actix_rt::signal::ctrl_c().await;
|
|
|
|
info!("Ctrl-C received, shutting down...");
|
|
|
|
drop(addresses);
|
2024-04-07 19:08:23 +01:00
|
|
|
});
|
2024-04-06 18:53:18 +01:00
|
|
|
}
|
|
|
|
|
2024-04-09 07:20:13 +01:00
|
|
|
fn create_forge_repos(
|
|
|
|
forge: &Forge,
|
|
|
|
forge_name: ForgeName,
|
2024-04-09 10:44:01 +01:00
|
|
|
net: &Network,
|
2024-04-09 07:20:13 +01:00
|
|
|
) -> Vec<(ForgeName, RepoName, RepoActor)> {
|
|
|
|
let forge = forge.clone();
|
|
|
|
let span = tracing::info_span!("Forge", %forge_name, %forge);
|
|
|
|
let _guard = span.enter();
|
|
|
|
info!("Creating Forge");
|
|
|
|
forge
|
|
|
|
.repos()
|
2024-04-09 10:44:01 +01:00
|
|
|
.map(create_actor(forge_name, forge.clone(), net))
|
2024-04-09 07:20:13 +01:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_actor(
|
|
|
|
forge_name: ForgeName,
|
|
|
|
forge: config::Forge,
|
2024-04-09 10:44:01 +01:00
|
|
|
net: &Network,
|
2024-04-09 07:20:13 +01:00
|
|
|
) -> impl Fn((RepoName, &Repo)) -> (ForgeName, RepoName, RepoActor) {
|
2024-04-09 10:44:01 +01:00
|
|
|
let net = net.clone();
|
2024-04-09 07:20:13 +01:00
|
|
|
move |(repo_name, repo)| {
|
|
|
|
let span = tracing::info_span!("Repo", %repo_name, %repo);
|
|
|
|
let _guard = span.enter();
|
|
|
|
info!("Creating Repo");
|
2024-04-09 10:44:01 +01:00
|
|
|
let actor = actors::repo::RepoActor::new(
|
|
|
|
config::RepoDetails::new(&repo_name, repo, &forge_name, &forge),
|
|
|
|
net.clone(),
|
|
|
|
);
|
2024-04-09 07:20:13 +01:00
|
|
|
info!("Created Repo");
|
|
|
|
(forge_name.clone(), repo_name, actor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_actor(
|
|
|
|
actor: (ForgeName, RepoName, actors::repo::RepoActor),
|
|
|
|
) -> Addr<actors::repo::RepoActor> {
|
|
|
|
let (forge_name, repo_name, actor) = actor;
|
|
|
|
let span = tracing::info_span!("Forge/Repo", %forge_name, %repo_name);
|
|
|
|
let _guard = span.enter();
|
|
|
|
info!("Starting");
|
|
|
|
let addr = actor.start();
|
|
|
|
addr.do_send(actors::repo::StartRepo);
|
|
|
|
info!("Started");
|
|
|
|
addr
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:53:18 +01:00
|
|
|
pub fn init_logging() -> Result<(), tracing::subscriber::SetGlobalDefaultError> {
|
|
|
|
use tracing_subscriber::prelude::*;
|
|
|
|
|
|
|
|
let subscriber = tracing_subscriber::fmt::layer()
|
|
|
|
// NOTE: set RUSTLOG in ${root}/.cargo/config
|
|
|
|
.with_target(false)
|
|
|
|
.with_file(true)
|
2024-04-10 11:40:42 +01:00
|
|
|
.with_line_number(true)
|
|
|
|
.with_filter(LevelFilter::INFO);
|
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();
|
|
|
|
Ok(())
|
|
|
|
}
|