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]
name = "urouter"
version = "0.3.5"
version = "0.4.0"
edition = "2021"
license = "MIT"
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",
"alias": "file"
"alias": {
"file": "somefile"
}
},
{
"uri": "uri2",
"alias": "http://example.com",
"is_url": true
"alias": {
"url": "http://example.com"
}
},
{
"uri": "/",
"alias": "https://somecoolscript.sh",
"is_url": true,
"alias": {
"url": "https://somecoolscript.sh"
},
"curl_only": true
}
]

View file

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