From a7b4a9a14765c18466119efee0a6b7a929f5e493 Mon Sep 17 00:00:00 2001 From: Ivan Bushchik Date: Mon, 18 Dec 2023 13:50:15 +0300 Subject: [PATCH] Add --alias-file option (for packaging and systemd service) Signed-off-by: Ivan Bushchik --- Cargo.toml | 7 ++++--- README.md | 4 ++-- src/main.rs | 19 ++++++++++++++++--- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9815ab6..73b5eaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,10 @@ [package] name = "urouter" -version = "0.2.1" +version = "0.3.0" edition = "2021" license = "MIT" repository = "https://github.com/ivabus/urouter" -description = "Small router for (kinda) short domains (fork of ivabus/aliurl for static routing)" +description = "Small router for (kinda) short domains (fork of ivabus/aliurl without REST API)" [dependencies] @@ -12,4 +12,5 @@ rocket = "0.5.0-rc.3" serde = { version = "1.0.163", features = ["derive"] } serde_json = "1.0.96" url-escape = "0.1.1" -smurf = "0.3.0" \ No newline at end of file +smurf = "0.3.0" +clap = { version = "4.4.11", features = ["derive"] } diff --git a/README.md b/README.md index 20e5a36..c9927cc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # urouter -Static (list of routes precompiled) http router for routing small domains. +Static (list of routes read once) http router for routing small domains. ## Installation @@ -9,7 +9,7 @@ git clone https://github.com/ivabus/urouter cd urouter ``` -Edit `alias.json` and `cargo run` +Edit `alias.json` (or any other JSON file, check `--alias-file` option) and `cargo run` ## `alias.json` example diff --git a/src/main.rs b/src/main.rs index 808418b..b3eccb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,10 +35,18 @@ use rocket::response::{Redirect, Responder}; use rocket::Request; use serde::Deserialize; -const _ALIAS: &'static str = include_str!("../alias.json"); +use clap::Parser; + static mut ALIAS: OnceCell> = OnceCell::new(); -#[derive(Deserialize, Clone)] +#[derive(Parser, Debug)] +#[command(about, author)] +struct Args { + #[arg(short, long, default_value = "./alias.json")] + alias_file: PathBuf, +} + +#[derive(Deserialize, Clone, Debug)] struct Alias { uri: String, alias: String, @@ -112,8 +120,13 @@ async fn index(user_agent: UserAgent) -> Response { #[rocket::main] async fn main() -> Result<(), rocket::Error> { + let args = Args::parse(); unsafe { - ALIAS.set(serde_json::from_str(_ALIAS).unwrap()).unwrap_unchecked(); + ALIAS + .set( + serde_json::from_str(&smurf::io::read_file_str(&args.alias_file).unwrap()).unwrap(), + ) + .unwrap(); } let _rocket = rocket::build().mount("/", routes![get_page, index]).launch().await?; Ok(())