mirror of
https://github.com/ivabus/urouter
synced 2024-11-22 00:15:11 +03:00
Add --alias-file option (for packaging and systemd service)
Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
parent
c83d762b76
commit
a7b4a9a147
3 changed files with 22 additions and 8 deletions
|
@ -1,10 +1,10 @@
|
||||||
[package]
|
[package]
|
||||||
name = "urouter"
|
name = "urouter"
|
||||||
version = "0.2.1"
|
version = "0.3.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
repository = "https://github.com/ivabus/urouter"
|
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]
|
[dependencies]
|
||||||
|
@ -13,3 +13,4 @@ serde = { version = "1.0.163", features = ["derive"] }
|
||||||
serde_json = "1.0.96"
|
serde_json = "1.0.96"
|
||||||
url-escape = "0.1.1"
|
url-escape = "0.1.1"
|
||||||
smurf = "0.3.0"
|
smurf = "0.3.0"
|
||||||
|
clap = { version = "4.4.11", features = ["derive"] }
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# urouter
|
# 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
|
## Installation
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ git clone https://github.com/ivabus/urouter
|
||||||
cd 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
|
## `alias.json` example
|
||||||
|
|
||||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -35,10 +35,18 @@ use rocket::response::{Redirect, Responder};
|
||||||
use rocket::Request;
|
use rocket::Request;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
const _ALIAS: &'static str = include_str!("../alias.json");
|
use clap::Parser;
|
||||||
|
|
||||||
static mut ALIAS: OnceCell<Vec<Alias>> = OnceCell::new();
|
static mut ALIAS: OnceCell<Vec<Alias>> = 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 {
|
struct Alias {
|
||||||
uri: String,
|
uri: String,
|
||||||
alias: String,
|
alias: String,
|
||||||
|
@ -112,8 +120,13 @@ async fn index(user_agent: UserAgent) -> Response {
|
||||||
|
|
||||||
#[rocket::main]
|
#[rocket::main]
|
||||||
async fn main() -> Result<(), rocket::Error> {
|
async fn main() -> Result<(), rocket::Error> {
|
||||||
|
let args = Args::parse();
|
||||||
unsafe {
|
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?;
|
let _rocket = rocket::build().mount("/", routes![get_page, index]).launch().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue