2024-04-07 19:41:21 +01:00
|
|
|
mod actors;
|
2024-04-07 13:47:39 +01:00
|
|
|
mod config;
|
2024-04-16 22:21:55 +01:00
|
|
|
pub mod gitforge;
|
2024-04-12 18:45:32 +01:00
|
|
|
pub mod types;
|
2024-04-07 13:47:39 +01:00
|
|
|
|
2024-04-07 20:16:04 +01:00
|
|
|
use actix::prelude::*;
|
2024-04-16 22:21:55 +01:00
|
|
|
|
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::{
|
2024-04-28 08:05:09 +01:00
|
|
|
fs::FileSystem,
|
2024-04-12 18:45:32 +01:00
|
|
|
server::{
|
2024-04-13 16:16:09 +01:00
|
|
|
actors::webhook,
|
2024-04-23 07:09:30 +01:00
|
|
|
config::{ForgeConfig, ForgeName, RepoAlias, ServerStorage, Webhook},
|
2024-04-12 18:45:32 +01:00
|
|
|
},
|
2024-04-07 20:16:04 +01:00
|
|
|
};
|
2024-04-07 11:52:47 +01:00
|
|
|
|
2024-04-19 18:15:27 +01:00
|
|
|
use self::{actors::repo::RepoActor, config::ServerRepoConfig};
|
2024-04-09 07:20:13 +01:00
|
|
|
|
2024-04-27 15:23:42 +01:00
|
|
|
#[derive(Debug, derive_more::Display, derive_more::From)]
|
|
|
|
pub enum Error {
|
|
|
|
#[display("Failed to create data directories")]
|
|
|
|
FailedToCreateDataDirectory(kxio::fs::Error),
|
|
|
|
|
|
|
|
#[display("The forge data path is not a directory: {path:?}")]
|
|
|
|
ForgeDirIsNotDirectory { path: PathBuf },
|
|
|
|
}
|
|
|
|
type Result<T> = core::result::Result<T, Error>;
|
|
|
|
|
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-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 {
|
2024-04-07 08:56:33 +01:00
|
|
|
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")) {
|
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-16 22:21:55 +01:00
|
|
|
pub async fn start(fs: FileSystem, net: Network) {
|
2024-04-27 15:23:42 +01:00
|
|
|
init_logging();
|
2024-04-06 18:53:18 +01:00
|
|
|
info!("Starting Server...");
|
2024-04-14 06:48:26 +01:00
|
|
|
let server_config = match config::ServerConfig::load(&fs) {
|
|
|
|
Ok(server_config) => server_config,
|
2024-04-07 18:37:01 +01:00
|
|
|
Err(err) => {
|
|
|
|
error!("Failed to load config file. Error: {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2024-04-27 15:23:42 +01:00
|
|
|
// create data dir if missing
|
|
|
|
let dir = server_config.storage().path();
|
|
|
|
if !dir.exists() {
|
|
|
|
if let Err(err) = fs.dir_create(dir) {
|
|
|
|
error!(?err, ?dir, "Failed to create server storage directory");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-04-09 07:20:13 +01:00
|
|
|
|
2024-04-13 16:16:09 +01:00
|
|
|
let webhook_router = webhook::WebhookRouter::new().start();
|
2024-04-14 06:48:26 +01:00
|
|
|
let webhook = server_config.webhook();
|
2024-04-23 07:09:30 +01:00
|
|
|
let server_storage = server_config.storage();
|
|
|
|
|
2024-04-27 15:23:42 +01:00
|
|
|
if let Err(err) = create_forge_data_directories(&server_config, &fs, &dir) {
|
|
|
|
error!(?err, "Failure creating forge data directories");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-14 06:48:26 +01:00
|
|
|
server_config
|
2024-04-09 07:20:13 +01:00
|
|
|
.forges()
|
2024-04-23 07:13:16 +01:00
|
|
|
.flat_map(|(forge_name, forge_config)| {
|
|
|
|
create_forge_repos(forge_config, forge_name, server_storage, webhook, &net)
|
2024-04-23 07:09:30 +01:00
|
|
|
})
|
2024-04-12 12:58:07 +01:00
|
|
|
.map(start_actor)
|
2024-04-13 16:16:09 +01:00
|
|
|
.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();
|
2024-04-12 12:58:07 +01:00
|
|
|
let _ = actix_rt::signal::ctrl_c().await;
|
|
|
|
info!("Ctrl-C received, shutting down...");
|
2024-04-13 16:16:09 +01:00
|
|
|
drop(webhook_server);
|
2024-04-06 18:53:18 +01:00
|
|
|
}
|
|
|
|
|
2024-04-27 15:23:42 +01:00
|
|
|
fn create_forge_data_directories(
|
|
|
|
server_config: &config::ServerConfig,
|
|
|
|
fs: &FileSystem,
|
|
|
|
server_dir: &&std::path::Path,
|
|
|
|
) -> Result<()> {
|
|
|
|
for (forge_name, _forge_config) in server_config.forges() {
|
|
|
|
let forge_dir: PathBuf = (&forge_name).into();
|
|
|
|
let path = server_dir.join(&forge_dir);
|
|
|
|
if fs.path_exists(&path)? {
|
|
|
|
if !fs.path_is_dir(&path)? {
|
|
|
|
return Err(Error::ForgeDirIsNotDirectory { path });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fs.dir_create_all(&path)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-04-09 07:20:13 +01:00
|
|
|
fn create_forge_repos(
|
2024-04-23 07:13:16 +01:00
|
|
|
forge_config: &ForgeConfig,
|
2024-04-09 07:20:13 +01:00
|
|
|
forge_name: ForgeName,
|
2024-04-23 07:09:30 +01:00
|
|
|
server_storage: &ServerStorage,
|
2024-04-14 06:48:26 +01:00
|
|
|
webhook: &Webhook,
|
2024-04-09 10:44:01 +01:00
|
|
|
net: &Network,
|
2024-04-13 11:45:58 +01:00
|
|
|
) -> Vec<(ForgeName, RepoAlias, RepoActor)> {
|
2024-04-23 07:13:16 +01:00
|
|
|
let span = tracing::info_span!("Forge", %forge_name, %forge_config);
|
2024-04-09 07:20:13 +01:00
|
|
|
let _guard = span.enter();
|
|
|
|
info!("Creating Forge");
|
2024-04-23 07:13:16 +01:00
|
|
|
forge_config
|
2024-04-09 07:20:13 +01:00
|
|
|
.repos()
|
2024-04-23 07:09:30 +01:00
|
|
|
.map(create_actor(
|
|
|
|
forge_name,
|
2024-04-23 07:13:16 +01:00
|
|
|
forge_config.clone(),
|
2024-04-23 07:09:30 +01:00
|
|
|
server_storage,
|
|
|
|
webhook,
|
|
|
|
net,
|
|
|
|
))
|
2024-04-09 07:20:13 +01:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_actor(
|
|
|
|
forge_name: ForgeName,
|
2024-04-23 07:13:16 +01:00
|
|
|
forge_config: config::ForgeConfig,
|
2024-04-23 07:09:30 +01:00
|
|
|
server_storage: &ServerStorage,
|
2024-04-14 06:48:26 +01:00
|
|
|
webhook: &Webhook,
|
2024-04-09 10:44:01 +01:00
|
|
|
net: &Network,
|
2024-04-19 18:15:27 +01:00
|
|
|
) -> impl Fn((RepoAlias, &ServerRepoConfig)) -> (ForgeName, RepoAlias, RepoActor) {
|
2024-04-23 07:09:30 +01:00
|
|
|
let server_storage = server_storage.clone();
|
2024-04-14 06:48:26 +01:00
|
|
|
let webhook = webhook.clone();
|
2024-04-09 10:44:01 +01:00
|
|
|
let net = net.clone();
|
2024-04-20 20:50:50 +01:00
|
|
|
move |(repo_name, server_repo_config)| {
|
|
|
|
let span = tracing::info_span!("Repo", %repo_name, %server_repo_config);
|
2024-04-09 07:20:13 +01:00
|
|
|
let _guard = span.enter();
|
|
|
|
info!("Creating Repo");
|
2024-04-09 10:44:01 +01:00
|
|
|
let actor = actors::repo::RepoActor::new(
|
2024-04-23 07:09:30 +01:00
|
|
|
config::RepoDetails::new(
|
|
|
|
&repo_name,
|
|
|
|
server_repo_config,
|
|
|
|
&forge_name,
|
2024-04-23 07:13:16 +01:00
|
|
|
&forge_config,
|
2024-04-23 07:09:30 +01:00
|
|
|
&server_storage,
|
|
|
|
),
|
2024-04-14 06:48:26 +01:00
|
|
|
webhook.clone(),
|
2024-04-09 10:44:01 +01:00
|
|
|
net.clone(),
|
|
|
|
);
|
2024-04-09 07:20:13 +01:00
|
|
|
info!("Created Repo");
|
|
|
|
(forge_name.clone(), repo_name, actor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_actor(
|
2024-04-13 11:45:58 +01:00
|
|
|
actor: (ForgeName, RepoAlias, actors::repo::RepoActor),
|
2024-04-13 16:16:09 +01:00
|
|
|
) -> (RepoAlias, Addr<actors::repo::RepoActor>) {
|
|
|
|
let (forge_name, repo_alias, actor) = actor;
|
|
|
|
let span = tracing::info_span!("Forge/Repo", %forge_name, %repo_alias);
|
2024-04-09 07:20:13 +01:00
|
|
|
let _guard = span.enter();
|
|
|
|
info!("Starting");
|
|
|
|
let addr = actor.start();
|
2024-04-23 07:09:30 +01:00
|
|
|
addr.do_send(actors::repo::CloneRepo);
|
2024-04-09 07:20:13 +01:00
|
|
|
info!("Started");
|
2024-04-13 16:16:09 +01:00
|
|
|
(repo_alias, addr)
|
2024-04-09 07:20:13 +01:00
|
|
|
}
|
|
|
|
|
2024-04-27 15:23:42 +01:00
|
|
|
pub fn init_logging() {
|
2024-04-06 18:53:18 +01:00
|
|
|
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();
|
|
|
|
}
|