forked from kemitix/git-next
136 lines
4 KiB
Rust
136 lines
4 KiB
Rust
mod actors;
|
|
mod config;
|
|
pub mod forge;
|
|
pub mod git;
|
|
pub mod types;
|
|
|
|
use actix::prelude::*;
|
|
use kxio::network::Network;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use tracing::{error, info, level_filters::LevelFilter};
|
|
|
|
use crate::{
|
|
filesystem::FileSystem,
|
|
server::{
|
|
actors::webhook,
|
|
config::{Forge, ForgeName, RepoAlias},
|
|
git::Git,
|
|
},
|
|
};
|
|
|
|
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 async fn start(fs: FileSystem, net: Network, git: Git) {
|
|
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 webhook_router = webhook::WebhookRouter::new().start();
|
|
config
|
|
.forges()
|
|
.flat_map(|(forge_name, forge)| create_forge_repos(forge, forge_name, &net, &git))
|
|
.map(start_actor)
|
|
.map(|(alias, addr)| webhook::AddWebhookRecipient(alias, addr.recipient()))
|
|
.for_each(|msg| webhook_router.do_send(msg));
|
|
let webhook_server = webhook::WebhookActor::new(webhook_router.recipient()).start();
|
|
let _ = actix_rt::signal::ctrl_c().await;
|
|
info!("Ctrl-C received, shutting down...");
|
|
drop(webhook_server);
|
|
}
|
|
|
|
fn create_forge_repos(
|
|
forge: &Forge,
|
|
forge_name: ForgeName,
|
|
net: &Network,
|
|
git: &Git,
|
|
) -> Vec<(ForgeName, RepoAlias, 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, git))
|
|
.collect::<Vec<_>>()
|
|
}
|
|
|
|
fn create_actor(
|
|
forge_name: ForgeName,
|
|
forge: config::Forge,
|
|
net: &Network,
|
|
git: &Git,
|
|
) -> impl Fn((RepoAlias, &Repo)) -> (ForgeName, RepoAlias, RepoActor) {
|
|
let net = net.clone();
|
|
let git = git.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(),
|
|
git.clone(),
|
|
);
|
|
info!("Created Repo");
|
|
(forge_name.clone(), repo_name, actor)
|
|
}
|
|
}
|
|
|
|
fn start_actor(
|
|
actor: (ForgeName, RepoAlias, actors::repo::RepoActor),
|
|
) -> (RepoAlias, Addr<actors::repo::RepoActor>) {
|
|
let (forge_name, repo_alias, actor) = actor;
|
|
let span = tracing::info_span!("Forge/Repo", %forge_name, %repo_alias);
|
|
let _guard = span.enter();
|
|
info!("Starting");
|
|
let addr = actor.start();
|
|
addr.do_send(actors::repo::StartRepo);
|
|
info!("Started");
|
|
(repo_alias, addr)
|
|
}
|
|
|
|
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)
|
|
.with_line_number(true)
|
|
.with_filter(LevelFilter::INFO);
|
|
tracing_subscriber::registry()
|
|
.with(console_subscriber::ConsoleLayer::builder().spawn())
|
|
.with(subscriber)
|
|
.init();
|
|
Ok(())
|
|
}
|