| 1 |
use crate::prelude::*; |
| 2 |
use actix_web::{App as ActixApp, HttpServer, web}; |
| 3 |
use clap::Parser; |
| 4 |
use std::io::Error; |
| 5 |
|
| 6 |
use crate::cli::Args; |
| 7 |
|
| 8 |
pub mod app; |
| 9 |
pub mod cli; |
| 10 |
pub mod error; |
| 11 |
pub mod git; |
| 12 |
pub mod markdown; |
| 13 |
pub mod prelude; |
| 14 |
pub mod routes; |
| 15 |
pub mod syntax_highlight; |
| 16 |
pub mod time; |
| 17 |
|
| 18 |
#[actix_web::main] |
| 19 |
async fn main() -> Result<(), Error> { |
| 20 |
let args = Args::parse(); |
| 21 |
|
| 22 |
if args.install_service { |
| 23 |
return install_systemd_service(&args); |
| 24 |
} |
| 25 |
|
| 26 |
let app = web::Data::new(App::new(args.repos_dir.to_string()).await); |
| 27 |
|
| 28 |
HttpServer::new(move || { |
| 29 |
ActixApp::new() |
| 30 |
.app_data(app.clone()) |
| 31 |
.service(routes::r#static::files) |
| 32 |
.service(routes::git::git_info_refs) |
| 33 |
.service(routes::git::git_upload_pack) |
| 34 |
.service(routes::repository::diff::patch) |
| 35 |
.service(routes::repository::snapshot::download) |
| 36 |
.service(routes::page) |
| 37 |
.service(routes::about::page) |
| 38 |
.service(routes::repository::about::page) |
| 39 |
.service(routes::repository::summary::page) |
| 40 |
.service(routes::repository::refs::page) |
| 41 |
.service(routes::repository::tag::page) |
| 42 |
.service(routes::repository::log::page) |
| 43 |
.service(routes::repository::tree::page) |
| 44 |
.service(routes::repository::tree::raw) |
| 45 |
.service(routes::repository::commit::page) |
| 46 |
.service(routes::repository::diff::page) |
| 47 |
}) |
| 48 |
.bind((args.url, args.port))? |
| 49 |
.run() |
| 50 |
.await |
| 51 |
} |
| 52 |
|
| 53 |
fn install_systemd_service(args: &Args) -> Result<(), Error> { |
| 54 |
let exe = std::env::current_exe()?; |
| 55 |
let service = format!( |
| 56 |
"[Unit]\n\ |
| 57 |
Description=git-rs — Lightweight Git web frontend\n\ |
| 58 |
After=network-online.target\n\ |
| 59 |
Wants=network-online.target\n\ |
| 60 |
\n\ |
| 61 |
[Service]\n\ |
| 62 |
Type=simple\n\ |
| 63 |
User=root\n\ |
| 64 |
WorkingDirectory={repos_dir}\n\ |
| 65 |
ExecStart={exe} --url {url} --port {port} --repos-dir {repos_dir}\n\ |
| 66 |
Restart=on-failure\n\ |
| 67 |
RestartSec=5\n\ |
| 68 |
AmbientCapabilities=CAP_NET_BIND_SERVICE\n\ |
| 69 |
\n\ |
| 70 |
[Install]\n\ |
| 71 |
WantedBy=multi-user.target\n", |
| 72 |
exe = exe.display(), |
| 73 |
url = args.url, |
| 74 |
port = args.port, |
| 75 |
repos_dir = args.repos_dir, |
| 76 |
); |
| 77 |
|
| 78 |
std::fs::write("/etc/systemd/system/git-rs.service", service)?; |
| 79 |
println!("Installed /etc/systemd/system/git-rs.service"); |
| 80 |
Ok(()) |
| 81 |
} |