index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff

Diffstat

-rw-r--r-- build.rs 7
-rw-r--r-- src/cli.rs 6
-rw-r--r-- src/main.rs 35
 3 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/build.rs b/build.rs
index fe7e164..70178a3 100644
--- a/build.rs
+++ b/build.rs
@@ -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");
    }
}
diff --git a/src/cli.rs b/src/cli.rs
index e409188..172b22e 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -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,
}
diff --git a/src/main.rs b/src/main.rs
index d6754e0..dea7035 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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(())
}