ef0b83a
use std::sync::Arc;
use std::time::Duration;
use export::AppState;
use proto::KenShellClient;
use sea_orm::DatabaseConnection;
use crate::context::facade::GitContext;
use crate::rpcgit::GitBaby;
#[derive(Clone)]
pub struct GitService {
pub state: AppState,
ks: Arc<KenShellClient>,
}
impl GitService {
pub fn new(state: AppState) -> Self {
let cfg = &state.config.git;
let timeout = Duration::from_secs(cfg.kenshell_timeout_secs.max(1));
let ks = KenShellClient::connect_with_key(
&cfg.kenshell_endpoint,
&cfg.kenshell_rpc_key,
timeout,
)
.unwrap_or_else(|error| {
panic!(
"invalid git.kenshell_endpoint `{}`: {error}",
cfg.kenshell_endpoint
)
});
Self {
state,
ks: Arc::new(ks),
}
}
pub fn ks(&self) -> &KenShellClient {
&self.ks
}
pub fn baby(&self, ctx: &GitContext) -> GitBaby {
GitBaby::new(Arc::new(ctx.clone()))
}
pub fn db_write(&self) -> &DatabaseConnection {
self.state.database.write()
}
pub fn db_read(&self) -> &DatabaseConnection {
self.state.database.read()
}
}