urouter/src/structs.rs
Ivan Bushchik 886c4cf2fa
0.5.2: beautify everything
- Use serde_json::from_reader in alias list initialization
- Compile all regexes in initialization, not while recieving first
  triggering request
- Count regex compilation time
- Specify version in Server responce header

Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
2023-12-22 13:50:37 +03:00

83 lines
1.8 KiB
Rust

use clap::Parser;
use rocket::http::Status;
use rocket::request::{FromRequest, Outcome};
use rocket::response::content::RawText;
use rocket::response::Redirect;
use rocket::Request;
use serde::Deserialize;
use std::net::IpAddr;
use std::path::PathBuf;
#[derive(Parser, Debug)]
#[command(about, author)]
pub struct Args {
#[arg(long, default_value = "./alias.json")]
pub alias_file: PathBuf,
/// For internal usage
#[arg(long, default_value = "false")]
pub alias_file_is_set_not_a_list: bool,
/// Dir to lookup file alias
#[arg(long, default_value = ".")]
pub dir: PathBuf,
#[arg(short, long, default_value = "127.0.0.1")]
pub address: IpAddr,
#[arg(short, long, default_value = "8080")]
pub port: u16,
}
// For better compatability with Nix (with set on the top of alias.json instead of a list)
#[derive(Deserialize, Clone, Debug)]
pub struct NixJson {
pub alias: Vec<Alias>,
}
#[derive(Deserialize, Clone, Debug)]
pub struct Alias {
pub uri: String,
pub alias: AliasType,
pub agent: Option<Agent>,
}
#[derive(Deserialize, Clone, Debug)]
pub enum AliasType {
#[serde(alias = "url")]
Url(String),
#[serde(alias = "file")]
File(String),
#[serde(alias = "text")]
Text(String),
}
#[derive(Deserialize, Clone, Debug)]
pub struct Agent {
pub regex: String,
pub only_matching: Option<bool>,
}
#[derive(Responder)]
pub enum Response {
Text(RawText<String>),
Redirect(Box<Redirect>),
Status(Status),
}
pub struct UserAgent(pub String);
#[derive(Debug)]
pub enum UserAgentError {}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for UserAgent {
type Error = UserAgentError;
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
match req.headers().get_one("user-agent") {
Some(key) => Outcome::Success(UserAgent(key.to_string())),
_ => Outcome::Success(UserAgent("".to_string())),
}
}
}