0.6.1: Add "html" alias type, fix --alias-file

Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
Ivan Bushchik 2024-01-21 08:36:30 +03:00
parent 9cfa1cf950
commit 653b41a67f
No known key found for this signature in database
GPG key ID: 2F16FBF3262E090C
5 changed files with 36 additions and 22 deletions

View file

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

View file

@ -21,6 +21,7 @@ Each set contains 2 necessary elements and 1 optional.
- `url` (string) - redirect to URL with HTTP 303 See Other - `url` (string) - redirect to URL with HTTP 303 See Other
- `file` (string) - read file from path `--dir/file` where `--dir` is option (default: `.`, see `--help`) and respond with HTTP 200 OK with `content-type: text/plain` - `file` (string) - read file from path `--dir/file` where `--dir` is option (default: `.`, see `--help`) and respond with HTTP 200 OK with `content-type: text/plain`
- `text` (string) - plain text with HTTP 200 OK with `content-type: text/plain` - `text` (string) - plain text with HTTP 200 OK with `content-type: text/plain`
- `html` (string) - plain text with HTTP 200 OK with `content-type: text/html`
- `external` (set) - download (every time) file using `ureq` HTTP library and response with contents of downloaded resource with HTTP 200 OK and extracted `content-type` from response - `external` (set) - download (every time) file using `ureq` HTTP library and response with contents of downloaded resource with HTTP 200 OK and extracted `content-type` from response
- `url` (string) - URL to download - `url` (string) - URL to download
- `headers` (set, optional) - headers to include with request - `headers` (set, optional) - headers to include with request

View file

@ -17,7 +17,7 @@
{ {
"uri":"text", "uri":"text",
"alias": { "alias": {
"text": "sometext" "html": "sometext"
}, },
"agent": { "agent": {
"regex": "^curl/[0-9].[0-9].[0-9]$", "regex": "^curl/[0-9].[0-9].[0-9]$",

View file

@ -13,6 +13,7 @@ use structs::*;
extern crate rocket; extern crate rocket;
use rocket::http::{ContentType, Status}; use rocket::http::{ContentType, Status};
use std::io::Write;
use std::{ use std::{
cell::OnceCell, collections::HashMap, hint::unreachable_unchecked, path::PathBuf, time::Instant, cell::OnceCell, collections::HashMap, hint::unreachable_unchecked, path::PathBuf, time::Instant,
}; };
@ -24,6 +25,7 @@ use rocket::{
use clap::Parser; use clap::Parser;
use regex::Regex; use regex::Regex;
use rocket::response::content::RawHtml;
static mut ALIAS: OnceCell<Vec<Alias>> = OnceCell::new(); static mut ALIAS: OnceCell<Vec<Alias>> = OnceCell::new();
static mut COMPILED_REGEXES: OnceCell<HashMap<String, Regex>> = OnceCell::new(); static mut COMPILED_REGEXES: OnceCell<HashMap<String, Regex>> = OnceCell::new();
@ -38,6 +40,7 @@ fn get_return(alias: &Alias) -> Response {
Response::Text(Box::new(RawText(smurf::io::read_file_str(&dir).unwrap()))) Response::Text(Box::new(RawText(smurf::io::read_file_str(&dir).unwrap())))
} }
AliasType::Text(text) => Response::Text(Box::new(RawText(text.clone()))), AliasType::Text(text) => Response::Text(Box::new(RawText(text.clone()))),
AliasType::Html(html) => Response::Html(Box::new(RawHtml(html.clone()))),
AliasType::External(source) => { AliasType::External(source) => {
let mut request = ureq::get(&source.url); let mut request = ureq::get(&source.url);
for (header, value) in &source.headers { for (header, value) in &source.headers {
@ -95,27 +98,34 @@ async fn index(user_agent: UserAgent) -> Response {
get_page("/", user_agent) get_page("/", user_agent)
} }
fn get_config_file_location() -> PathBuf {
if users::get_effective_uid() == 0 {
return "/etc/urouter/alias.json".parse().unwrap();
}
if let Ok(config_home) = std::env::var("XDG_CONFIG_HOME") {
return format!("{}/urouter/alias.json", config_home).parse().unwrap();
}
if let Ok(home) = std::env::var("HOME") {
return format!("{}/.config/urouter/alias.json", home).parse().unwrap();
}
if !std::path::Path::new("alias.json").exists() {
let mut file = std::fs::File::create("alias.json").unwrap();
file.write_all(b"[]").unwrap();
}
"alias.json".parse().unwrap()
}
#[rocket::main] #[rocket::main]
async fn main() -> Result<(), rocket::Error> { async fn main() -> Result<(), rocket::Error> {
let mut args = Args::parse(); let mut args = Args::parse();
args.alias_file = match args.alias_file {
Some(f) => Some(f), if args.alias_file.is_none() {
None => Some( args.alias_file = Some(get_config_file_location());
match users::get_effective_uid() { }
0 => "/etc/urouter/alias.json".to_string(),
_ => match std::env::var("XDG_CONFIG_HOME") {
Ok(config_home) => format!("{}/urouter/alias.json", config_home),
Err(_) => match std::env::var("HOME") {
Ok(home) => format!("{}/.config/urouter/alias.json", home),
Err(_) => {
panic!("Could not get config location, see README")
}
},
},
}
.into(),
),
};
let file = std::fs::File::open(args.alias_file.unwrap()).unwrap(); let file = std::fs::File::open(args.alias_file.unwrap()).unwrap();
let alias: Vec<Alias> = if args.alias_file_is_set_not_a_list { let alias: Vec<Alias> = if args.alias_file_is_set_not_a_list {
serde_json::from_reader::<std::fs::File, NixJson>(file).unwrap().alias serde_json::from_reader::<std::fs::File, NixJson>(file).unwrap().alias

View file

@ -3,7 +3,7 @@
use clap::Parser; use clap::Parser;
use rocket::http::{ContentType, Status}; use rocket::http::{ContentType, Status};
use rocket::request::{FromRequest, Outcome}; use rocket::request::{FromRequest, Outcome};
use rocket::response::content::RawText; use rocket::response::content::{RawHtml, RawText};
use rocket::response::{Redirect, Responder}; use rocket::response::{Redirect, Responder};
use rocket::Request; use rocket::Request;
use serde::Deserialize; use serde::Deserialize;
@ -53,6 +53,8 @@ pub enum AliasType {
File(String), File(String),
#[serde(alias = "text")] #[serde(alias = "text")]
Text(String), Text(String),
#[serde(alias = "html")]
Html(String),
#[serde(alias = "external")] #[serde(alias = "external")]
External(External), External(External),
} }
@ -73,6 +75,7 @@ pub struct External {
#[derive(Responder)] #[derive(Responder)]
pub enum Response { pub enum Response {
Text(Box<RawText<String>>), Text(Box<RawText<String>>),
Html(Box<RawHtml<String>>),
Redirect(Box<Redirect>), Redirect(Box<Redirect>),
Status(Status), Status(Status),
Custom(Box<(ContentType, RawText<String>)>), Custom(Box<(ContentType, RawText<String>)>),