2024-04-07 19:41:21 +01:00
|
|
|
mod actors;
|
2024-04-07 13:47:39 +01:00
|
|
|
mod config;
|
|
|
|
|
2024-04-07 20:16:04 +01:00
|
|
|
use actix::prelude::*;
|
|
|
|
|
2024-04-07 11:52:47 +01:00
|
|
|
use std::path::PathBuf;
|
2024-04-06 18:42:34 +01:00
|
|
|
|
2024-04-07 18:37:01 +01:00
|
|
|
use tracing::{error, info};
|
2024-04-06 18:53:18 +01:00
|
|
|
|
2024-04-07 20:16:04 +01:00
|
|
|
use crate::{
|
|
|
|
filesystem::FileSystem,
|
|
|
|
server::config::{ForgeName, RepoName},
|
|
|
|
};
|
2024-04-07 11:52:47 +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-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-07 16:09:16 +01:00
|
|
|
pub fn start(fs: FileSystem) {
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
info!("Config loaded");
|
2024-04-07 19:08:23 +01:00
|
|
|
info!("Loaded config");
|
2024-04-07 20:16:04 +01:00
|
|
|
let mut actors: Vec<(ForgeName, RepoName, actors::repo::RepoActor)> = vec![];
|
|
|
|
config.forges().for_each(|(forge_name, forge)| {
|
|
|
|
let span = tracing::info_span!("Forge", %forge_name);
|
2024-04-07 19:08:23 +01:00
|
|
|
let _guard = span.enter();
|
2024-04-07 20:16:04 +01:00
|
|
|
info!("Forge: {}", forge_name);
|
2024-04-07 19:08:23 +01:00
|
|
|
info!("Forge Type: {}", forge.forge_type());
|
|
|
|
info!("Hostname: {}", forge.hostname());
|
|
|
|
info!("User: {}", forge.user());
|
2024-04-08 08:05:13 +01:00
|
|
|
forge.repos().for_each(|(repo_name, repo)| {
|
|
|
|
let span = tracing::info_span!("Repo", %repo_name, %repo);
|
2024-04-07 20:16:04 +01:00
|
|
|
let _guard = span.enter();
|
2024-04-08 08:05:13 +01:00
|
|
|
info!("Creating Repo");
|
2024-04-07 20:16:04 +01:00
|
|
|
let actor = actors::repo::RepoActor {
|
2024-04-08 08:05:13 +01:00
|
|
|
details: config::RepoDetails::new(&repo_name, repo, &forge_name, forge),
|
2024-04-07 20:16:04 +01:00
|
|
|
};
|
2024-04-08 08:05:13 +01:00
|
|
|
actors.push((forge_name.clone(), repo_name, actor));
|
|
|
|
info!("Created Repo");
|
2024-04-07 20:16:04 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
info!("Sending StartRepo to all actors...");
|
|
|
|
|
|
|
|
actix::System::new().block_on(async {
|
|
|
|
let mut addresses: Vec<Addr<actors::repo::RepoActor>> = vec![];
|
|
|
|
actors.into_iter().for_each(|actor| {
|
|
|
|
let (forge_name, repo_name, actor) = actor;
|
|
|
|
let span = tracing::info_span!("Forge/Repo", %forge_name, %repo_name);
|
2024-04-07 19:08:23 +01:00
|
|
|
let _guard = span.enter();
|
2024-04-07 20:16:04 +01:00
|
|
|
info!("Starting actor");
|
|
|
|
let addr = actor.start();
|
|
|
|
info!("Sending StartRepo to actor");
|
|
|
|
addr.do_send(actors::repo::StartRepo);
|
|
|
|
addresses.push(addr);
|
|
|
|
info!("Sent StartRepo to actor");
|
2024-04-07 19:08:23 +01:00
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init_logging() -> Result<(), tracing::subscriber::SetGlobalDefaultError> {
|
|
|
|
use tracing_subscriber::prelude::*;
|
|
|
|
|
|
|
|
let console_layer = console_subscriber::ConsoleLayer::builder().spawn();
|
|
|
|
|
|
|
|
let subscriber = tracing_subscriber::fmt::layer()
|
|
|
|
// NOTE: set RUSTLOG in ${root}/.cargo/config
|
|
|
|
.with_target(false)
|
|
|
|
.with_file(true)
|
|
|
|
.with_line_number(true);
|
|
|
|
tracing_subscriber::registry()
|
|
|
|
.with(console_layer)
|
|
|
|
.with(subscriber)
|
|
|
|
.init();
|
|
|
|
Ok(())
|
|
|
|
}
|