3 files changed, 47 insertions(+), 1 deletion(-)
@@ -4,4 +4,11 @@
println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed=templates");
println!("cargo:rerun-if-changed=static");
println!("cargo:rerun-if-changed=git-rs.service");
// When running `cargo install` (release profile) with permissions to write to
// /etc/systemd/system, automatically install git-rs.service.
if std::env::var("PROFILE").as_deref() == Ok("release") {
let _ = std::fs::copy("git-rs.service", "/etc/systemd/system/git-rs.service");
}
}
@@ -16,7 +16,11 @@
#[arg(short, long, value_name = "PORT", default_value_t = 80)]
pub port: u16,
// Repositories directory
/// Repositories directory
#[arg(short, long, value_name = "DIR", default_value = "/srv/git")]
pub repos_dir: String,
/// Install and enable the systemd service (/etc/systemd/system/git-rs.service)
#[arg(long)]
pub install_service: bool,
}
@@ -18,6 +18,11 @@
#[actix_web::main]
async fn main() -> Result<(), Error> {
let args = Args::parse();
if args.install_service {
return install_systemd_service(&args);
}
let app = web::Data::new(App::new(args.repos_dir.to_string()).await);
HttpServer::new(move || {
@@ -43,4 +48,34 @@
.bind((args.url, args.port))?
.run()
.await
}
fn install_systemd_service(args: &Args) -> Result<(), Error> {
let exe = std::env::current_exe()?;
let service = format!(
"[Unit]\n\
Description=git-rs — Lightweight Git web frontend\n\
After=network-online.target\n\
Wants=network-online.target\n\
\n\
[Service]\n\
Type=simple\n\
User=root\n\
WorkingDirectory={repos_dir}\n\
ExecStart={exe} --url {url} --port {port} --repos-dir {repos_dir}\n\
Restart=on-failure\n\
RestartSec=5\n\
AmbientCapabilities=CAP_NET_BIND_SERVICE\n\
\n\
[Install]\n\
WantedBy=multi-user.target\n",
exe = exe.display(),
url = args.url,
port = args.port,
repos_dir = args.repos_dir,
);
std::fs::write("/etc/systemd/system/git-rs.service", service)?;
println!("Installed /etc/systemd/system/git-rs.service");
Ok(())
}