From 2220d69ccc3db78e32d11d0e24baec99a975dee8 Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Fri, 8 Dec 2023 14:14:55 +0100 Subject: [PATCH] feat: add loading config data --- Cargo.lock | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- Cargo.toml | 1 + rgit.conf.example | 30 ++++++++++++++++++++++++++++++ src/configuration/mod.rs | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 9 +++++++++ 5 files changed, 211 insertions(+), 4 deletions(-) create mode 100644 rgit.conf.example create mode 100644 src/configuration/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 9af2619..881531e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -446,9 +446,9 @@ checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[package]] name = "bytemuck" -version = "1.15.0" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d6d68c57235a3a081186990eca2867354726650f42f7516ca50c28d6281fd15" +checksum = "78834c15cb5d5efe3452d58b1e8ba890dd62d21907f867f383358198e56ebca5" [[package]] name = "byteorder" @@ -605,6 +605,18 @@ dependencies = [ ] [[package]] +name = "confy" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e37668cb35145dcfaa1931a5f37fde375eeae8068b4c0d2f289da28a270b2d2c" +dependencies = [ + "directories", + "serde", + "thiserror", + "toml", +] + +[[package]] name = "console" version = "0.15.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -820,9 +832,9 @@ dependencies = [ [[package]] name = "deunicode" -version = "1.4.4" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322ef0094744e63628e6f0eb2295517f79276a5b342a4c2ff3042566ca181d4e" +checksum = "339544cc9e2c4dc3fc7149fd630c5f22263a4fdf18a98afd0075784968b5cf00" [[package]] name = "digest" @@ -835,6 +847,26 @@ dependencies = [ ] [[package]] +name = "directories" +version = "4.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f51c5d4ddabd36886dd3e1438cb358cdcb0d7c499cb99cb4ac2e38e18b5cb210" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] name = "dlv-list" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1476,6 +1508,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" [[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags 2.5.0", + "libc", +] + +[[package]] name = "librocksdb-sys" version = "0.16.0+8.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2043,6 +2085,17 @@ dependencies = [ ] [[package]] +name = "redox_users" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + +[[package]] name = "regex" version = "1.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2108,6 +2161,7 @@ dependencies = [ "bytes", "clap", "comrak", + "confy", "console-subscriber", "flate2", "futures", @@ -2668,6 +2722,15 @@ dependencies = [ ] [[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + +[[package]] name = "tonic" version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index a8d1d6f..c449f0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ uuid = { version = "1.7", features = ["v4"] } httparse = "1.7" yoke = { version = "0.7.1", features = ["derive"] } rand = "0.8.5" +confy = "0.5.1" [build-dependencies] anyhow = "1.0" diff --git a/rgit.conf.example b/rgit.conf.example new file mode 100644 index 0000000..575dfb9 --- /dev/null +++ b/rgit.conf.example @@ -0,0 +1,30 @@ +# Set the title and heading of the repository index page +root_title = 'Git repository browser' +# Set a subtitle for the repository index page +root_description = 'Open-source projects available at mydomain.tld' +# Include some more info about example.com on the index page +root_readme = '/config/rgit_about.md' +# Allow download of source tarballs +snapshots = [ + 'tar.gz', + 'tar.bz2', + 'zip', +] +# Add a favicon +favicon = '/config/favicon.ico' +# Use a custom logo image +logo = '/config/logo.png' +# Alt text for the custom logo image +logo_alt = '🏔️' +# Url loaded when clicking on the logo +logo_link = '/' +# Allow http transport git clone +enable_http_clone = true +# Show extra links for each repository on the index page +enable_index_links = false +# Show owner on the index page +enable_index_owner = true +# Generate HTTPS clone urls +clone_prefix = 'https://git.mydomain.tld' +# Generate SSH clone urls +ssh_clone_prefix = 'ssh://git.mydomain.tld:/srv/git' \ No newline at end of file diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs new file mode 100644 index 0000000..a107a92 --- /dev/null +++ b/src/configuration/mod.rs @@ -0,0 +1,104 @@ +use std::vec; + +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(default)] +pub struct AppConfig { + /// Set the title and heading of the repository index page + /// + /// Default value: "Git repository browser" + pub root_title: String, + + /// Set a subtitle for the repository index page + /// + /// Default value: `unset` + pub root_description: String, + + /// Include some more info about example.com on the index page + /// + /// Default value: `unset` + pub root_readme: String, + + /// Allow download of source tarballs + /// + /// Default value: `["tar.bz", "tar.bz2", "zip"]` + pub snapshots: Vec, + + /// Add a favicon + /// + /// Default value: `unset` + pub favicon: String, + + /// Use a custom logo image + /// + /// Default value: `unset` + pub logo: String, + + /// Alt text for the custom logo image + /// + /// Used as a fallback when `logo` is unset + /// + /// Default value: "🏡" + pub logo_alt: String, + + /// Url loaded when clicking on the logo + /// + /// Default value: "/" + pub logo_link: String, + + /// Allow http transport git clone + /// + /// Default value: `true` + pub enable_http_clone: bool, + + /// Show extra links for each repository on the index page + /// + /// Default value: `false` + pub enable_index_links: bool, + + /// Show owner on the index page + /// + /// Default value: `true` + pub enable_index_owner: bool, + + /// Generate HTTPS clone urls + /// + /// Default value: `unset` + pub clone_prefix: String, + + /// Generate SSH clone urls + /// + /// Default value: `unset` + pub ssh_clone_prefix: String, +} + +impl ::std::default::Default for AppConfig { + fn default() -> Self { + Self { + root_title: String::from("Git repository browser"), + root_description: String::new(), + root_readme: String::new(), + snapshots: vec![ + String::from("tar.gz"), + String::from("tar.bz2"), + String::from("zip"), + ], + favicon: String::new(), + logo: String::new(), + logo_alt: String::from("🏡"), + logo_link: String::from("/"), + enable_http_clone: true, + enable_index_links: false, + enable_index_owner: true, + clone_prefix: String::new(), + ssh_clone_prefix: String::new(), + } + } +} + +impl AppConfig { + pub fn load(path: String) -> Self { + confy::load_path(path).unwrap_or_default() + } +} diff --git a/src/main.rs b/src/main.rs index 05dc0f7..4d5f538 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,7 @@ use crate::{ layers::logger::LoggingMiddleware, }; +mod configuration; mod database; mod git; mod layers; @@ -79,6 +80,13 @@ pub struct Args { /// Configures the request timeout. #[clap(long, default_value_t = Duration::from_secs(10).into())] request_timeout: humantime::Duration, + /// The path to the file containing configuration data + /// + /// If the filename does not exist, it will be created if possible + /// + /// If no filename is provided, default settings will be used + #[clap(long, default_value_t = String::new())] + config: String, } #[derive(Debug, Clone, Copy)] @@ -212,6 +220,7 @@ async fn main() -> Result<(), anyhow::Error> { .layer(Extension(Arc::new(Git::new(syntax_set)))) .layer(Extension(db)) .layer(Extension(Arc::new(args.scan_path))) + .layer(Extension(configuration::AppConfig::load(args.config))) .layer(CorsLayer::new()); let listener = TcpListener::bind(&args.bind_address).await?; -- libgit2 1.7.2