0.4.0: Edit alias type

Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
Ivan Bushchik 2023-12-21 07:00:31 +03:00
parent 4903a4e2e3
commit b7c96c8b4e
No known key found for this signature in database
GPG key ID: 2F16FBF3262E090C
3 changed files with 28 additions and 15 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "urouter" name = "urouter"
version = "0.3.5" version = "0.4.0"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/ivabus/urouter" repository = "https://github.com/ivabus/urouter"

View file

@ -17,17 +17,21 @@ Edit `alias.json` (or any other JSON file, check `--alias-file` option) and `car
[ [
{ {
"uri": "uri", "uri": "uri",
"alias": "file" "alias": {
"file": "somefile"
}
}, },
{ {
"uri": "uri2", "uri": "uri2",
"alias": "http://example.com", "alias": {
"is_url": true "url": "http://example.com"
}
}, },
{ {
"uri": "/", "uri": "/",
"alias": "https://somecoolscript.sh", "alias": {
"is_url": true, "url": "https://somecoolscript.sh"
},
"curl_only": true "curl_only": true
} }
] ]

View file

@ -27,6 +27,7 @@ extern crate rocket;
use rocket::http::Status; use rocket::http::Status;
use std::cell::OnceCell; use std::cell::OnceCell;
use std::ffi::OsStr;
use std::net::IpAddr; use std::net::IpAddr;
use std::path::PathBuf; use std::path::PathBuf;
@ -71,11 +72,20 @@ struct NixJson {
#[derive(Deserialize, Clone, Debug)] #[derive(Deserialize, Clone, Debug)]
struct Alias { struct Alias {
uri: String, uri: String,
alias: String, alias: AliasType,
is_url: Option<bool>,
curl_only: Option<bool>, curl_only: Option<bool>,
} }
#[derive(Deserialize, Clone, Debug)]
enum AliasType {
#[serde(alias = "url")]
Url(String),
#[serde(alias = "file")]
File(String),
#[serde(alias = "text")]
Text(String),
}
#[derive(Responder)] #[derive(Responder)]
enum Response { enum Response {
Text(RawText<String>), Text(RawText<String>),
@ -103,12 +113,13 @@ impl<'r> FromRequest<'r> for UserAgent {
fn get_return(alias: &Alias) -> Response { fn get_return(alias: &Alias) -> Response {
let args = Args::parse(); let args = Args::parse();
let mut dir = args.dir.clone(); let mut dir = args.dir.clone();
return match alias.is_url { return match &alias.alias {
Some(true) => Response::Redirect(Redirect::to(alias.alias.clone())), AliasType::Url(url) => Response::Redirect(Redirect::to(url.clone())),
_ => { AliasType::File(path) => {
dir.push(&PathBuf::from(&alias.alias)); dir.push(&PathBuf::from(&path));
Response::Text(RawText(smurf::io::read_file_str(&dir).unwrap())) Response::Text(RawText(smurf::io::read_file_str(&dir).unwrap()))
} }
AliasType::Text(text) => Response::Text(RawText(text.clone())),
}; };
} }
@ -121,9 +132,7 @@ fn get_page(page: String, user_agent: UserAgent) -> Response {
let curl_check = user_agent.0.contains("curl"); let curl_check = user_agent.0.contains("curl");
for i in alias { for i in alias {
if i.uri == decoded_page { if i.uri == decoded_page {
if (i.curl_only == Some(true) && curl_check.clone()) if (i.curl_only == Some(true)) == curl_check.clone() {
|| (i.curl_only != Some(true) && !curl_check.clone())
{
return get_return(i); return get_return(i);
}; };
pages.push(i); pages.push(i);