feat(server): add repository prefix path configuration and service struct

- Add REPO_PREFIX_PATH environment variable support in Dockerfile and main.rs
- Introduce GitksService struct with repo_prefix field to manage repository paths
- Implement resolve and resolve_for_init methods for repository path handling
- Add path traversal protection and validation for repository operations
- Update all service implementations to use self.resolve instead of global resolve
- Modify serve function to accept repo_prefix parameter and pass to GitksService
- Remove global resolve functions and integrate them into GitksService struct
- Add proper initialization of repo directory from environment variable
This commit is contained in:
zhenyi
2026-06-04 14:18:12 +08:00
parent 4a87ea475d
commit 729604f13b
13 changed files with 166 additions and 115 deletions
+23 -2
View File
@@ -1,3 +1,5 @@
use std::path::PathBuf;
use gitks::server::serve;
const DEFAULT_HOST: &str = "0.0.0.0";
@@ -7,10 +9,29 @@ const DEFAULT_PORT: &str = "50051";
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenvy::dotenv().ok();
tracing_subscriber::fmt::init();
let host = std::env::var("GITKS_HOST").unwrap_or_else(|_| DEFAULT_HOST.into());
let port = std::env::var("GITKS_PORT").unwrap_or_else(|_| DEFAULT_PORT.into());
let repo_prefix = std::env::var("REPO_PREFIX_PATH").map_err(|_| {
"REPO_PREFIX_PATH environment variable is required (e.g. /data/repos)"
})?;
let repo_prefix = PathBuf::from(&repo_prefix);
if !repo_prefix.is_absolute() {
return Err("REPO_PREFIX_PATH must be an absolute path".into());
}
if !repo_prefix.exists() {
std::fs::create_dir_all(&repo_prefix)?;
}
let addr: std::net::SocketAddr = format!("{host}:{port}").parse()?;
tracing::info!("starting gitks gRPC server on {addr}");
serve(addr).await?;
tracing::info!(
"starting gitks gRPC server on {addr}, repo prefix: {}",
repo_prefix.display()
);
serve(addr, repo_prefix).await?;
Ok(())
}