git-next/src/server/mod.rs
Paul Campbell b093c002d4
All checks were successful
ci/woodpecker/push/todo-check Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/docker Pipeline was successful
ci/woodpecker/push/release Pipeline was successful
feat(server/forgejo): load repo config
2024-04-09 11:09:07 +01:00

127 lines
3.6 KiB
Rust

mod actors;
mod config;
pub mod forge;
use actix::prelude::*;
use kxio::network::Network;
use std::path::PathBuf;
use tracing::{error, info};
use crate::{
filesystem::FileSystem,
server::config::{Forge, ForgeName, RepoName},
};
use self::{actors::repo::RepoActor, config::Repo};
pub fn init(fs: FileSystem) {
let file_name = "git-next-server.toml";
let path = PathBuf::from(file_name);
if fs.file_exists(&path) {
eprintln!(
"The configuration file already exists at {} - not overwritting it.",
file_name
);
} else {
match fs.write_file(file_name, 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 fn start(fs: FileSystem, net: Network) {
let Ok(_) = init_logging() else {
eprintln!("Failed to initialize logging.");
return;
};
info!("Starting Server...");
let config = match config::ServerConfig::load(&fs) {
Ok(config) => config,
Err(err) => {
error!("Failed to load config file. Error: {}", err);
return;
}
};
info!("Config loaded");
let actors = config
.forges()
.flat_map(|(forge_name, forge)| create_forge_repos(forge, forge_name, &net))
.collect::<Vec<_>>();
actix::System::new().block_on(async {
let addresses = actors.into_iter().map(start_actor).collect::<Vec<_>>();
let _ = actix_rt::signal::ctrl_c().await;
info!("Ctrl-C received, shutting down...");
drop(addresses);
});
}
fn create_forge_repos(
forge: &Forge,
forge_name: ForgeName,
net: &Network,
) -> 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()
.map(create_actor(forge_name, forge.clone(), net))
.collect::<Vec<_>>()
}
fn create_actor(
forge_name: ForgeName,
forge: config::Forge,
net: &Network,
) -> impl Fn((RepoName, &Repo)) -> (ForgeName, RepoName, RepoActor) {
let net = net.clone();
move |(repo_name, repo)| {
let span = tracing::info_span!("Repo", %repo_name, %repo);
let _guard = span.enter();
info!("Creating Repo");
let actor = actors::repo::RepoActor::new(
config::RepoDetails::new(&repo_name, repo, &forge_name, &forge),
net.clone(),
);
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
}
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(())
}