Compare commits
No commits in common. "72e672bc733f62c22de021897f622c8c9ee4542d" and "15ab316963f1d7dfa6633594967c3abcd3fed3b7" have entirely different histories.
72e672bc73
...
15ab316963
9 changed files with 2410 additions and 1384 deletions
1895
Cargo.lock
generated
1895
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
21
Cargo.toml
21
Cargo.toml
|
@ -2,7 +2,7 @@
|
|||
name = "bunbun"
|
||||
version = "0.8.0"
|
||||
authors = ["Edward Shen <code@eddie.sh>"]
|
||||
edition = "2021"
|
||||
edition = "2018"
|
||||
description = "Re-implementation of bunny1 in Rust"
|
||||
license = "AGPL-3.0"
|
||||
readme = "README.md"
|
||||
|
@ -10,25 +10,20 @@ repository = "https://github.com/edward-shen/bunbun"
|
|||
exclude = ["/aux/"]
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
arc-swap = "1"
|
||||
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
||||
axum = "0.5"
|
||||
clap = { version = "3", features = ["wrap_help", "derive", "cargo"] }
|
||||
dirs = "4"
|
||||
handlebars = "4"
|
||||
actix-web = "3"
|
||||
clap = { version = "3.0.0-beta.2", features = ["wrap_help"] }
|
||||
dirs = "3"
|
||||
handlebars = "3"
|
||||
hotwatch = "0.4"
|
||||
log = "0.4"
|
||||
percent-encoding = "2"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde = "1"
|
||||
serde_yaml = "0.8"
|
||||
serde_json = "1"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
simple_logger = "1"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
strip = true
|
||||
|
|
3
rustfmt.toml
Normal file
3
rustfmt.toml
Normal file
|
@ -0,0 +1,3 @@
|
|||
tab_spaces = 2
|
||||
use_field_init_shorthand = true
|
||||
max_width = 80
|
14
src/cli.rs
14
src/cli.rs
|
@ -1,13 +1,15 @@
|
|||
use clap::{crate_authors, crate_version, Parser};
|
||||
use clap::{crate_authors, crate_version, Clap};
|
||||
use std::path::PathBuf;
|
||||
use tracing_subscriber::filter::Directive;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[derive(Clap)]
|
||||
#[clap(version = crate_version!(), author = crate_authors!())]
|
||||
pub struct Opts {
|
||||
/// Set the logging directives
|
||||
#[clap(long, default_value = "info")]
|
||||
pub log: Vec<Directive>,
|
||||
/// Increases the log level to info, debug, and trace, respectively.
|
||||
#[clap(short, long, parse(from_occurrences), conflicts_with("quiet"))]
|
||||
pub verbose: u8,
|
||||
/// Decreases the log level to error or no logging at all, respectively.
|
||||
#[clap(short, long, parse(from_occurrences), conflicts_with("verbose"))]
|
||||
pub quiet: u8,
|
||||
/// Specify the location of the config file to read from. Needs read/write permissions.
|
||||
#[clap(short, long)]
|
||||
pub config: Option<PathBuf>,
|
||||
|
|
172
src/config.rs
172
src/config.rs
|
@ -1,5 +1,6 @@
|
|||
use crate::BunBunError;
|
||||
use dirs::{config_dir, home_dir};
|
||||
use log::{debug, info, trace};
|
||||
use serde::{
|
||||
de::{self, Deserializer, MapAccess, Unexpected, Visitor},
|
||||
Deserialize, Serialize,
|
||||
|
@ -9,7 +10,7 @@ use std::fmt;
|
|||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::PathBuf;
|
||||
use tracing::{debug, info, trace};
|
||||
use std::str::FromStr;
|
||||
|
||||
const CONFIG_FILENAME: &str = "bunbun.yaml";
|
||||
const DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.yaml");
|
||||
|
@ -45,29 +46,17 @@ pub struct Route {
|
|||
pub max_args: Option<usize>,
|
||||
}
|
||||
|
||||
impl From<String> for Route {
|
||||
fn from(s: String) -> Self {
|
||||
Self {
|
||||
route_type: get_route_type(&s),
|
||||
path: s,
|
||||
hidden: false,
|
||||
description: None,
|
||||
min_args: None,
|
||||
max_args: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'static str> for Route {
|
||||
fn from(s: &'static str) -> Self {
|
||||
Self {
|
||||
impl FromStr for Route {
|
||||
type Err = std::convert::Infallible;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(Self {
|
||||
route_type: get_route_type(s),
|
||||
path: s.to_string(),
|
||||
hidden: false,
|
||||
description: None,
|
||||
min_args: None,
|
||||
max_args: None,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,7 +66,7 @@ impl From<&'static str> for Route {
|
|||
/// web path. This incurs a disk check operation, but since users shouldn't be
|
||||
/// updating the config that frequently, it should be fine.
|
||||
impl<'de> Deserialize<'de> for Route {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
fn deserialize<D>(deserializer: D) -> Result<Route, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
|
@ -104,7 +93,8 @@ impl<'de> Deserialize<'de> for Route {
|
|||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
Ok(Self::Value::from(path.to_owned()))
|
||||
// This is infallible
|
||||
Ok(Self::Value::from_str(path).unwrap())
|
||||
}
|
||||
|
||||
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
|
||||
|
@ -157,7 +147,8 @@ impl<'de> Deserialize<'de> for Route {
|
|||
{
|
||||
return Err(de::Error::invalid_value(
|
||||
Unexpected::Other(&format!(
|
||||
"argument count range {min_args} to {max_args}",
|
||||
"argument count range {} to {}",
|
||||
min_args, max_args
|
||||
)),
|
||||
&"a valid argument count range",
|
||||
));
|
||||
|
@ -188,12 +179,12 @@ impl std::fmt::Display for Route {
|
|||
route_type: RouteType::External,
|
||||
path,
|
||||
..
|
||||
} => write!(f, "raw ({path})"),
|
||||
} => write!(f, "raw ({})", path),
|
||||
Self {
|
||||
route_type: RouteType::Internal,
|
||||
path,
|
||||
..
|
||||
} => write!(f, "file ({path})"),
|
||||
} => write!(f, "file ({})", path),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -201,10 +192,10 @@ impl std::fmt::Display for Route {
|
|||
/// Classifies the path depending on if the there exists a local file.
|
||||
fn get_route_type(path: &str) -> RouteType {
|
||||
if std::path::Path::new(path).exists() {
|
||||
debug!("Parsed {path} as a valid local path.");
|
||||
debug!("Parsed {} as a valid local path.", path);
|
||||
RouteType::Internal
|
||||
} else {
|
||||
debug!("{path} does not exist on disk, assuming web path.");
|
||||
debug!("{} does not exist on disk, assuming web path.", path);
|
||||
RouteType::External
|
||||
}
|
||||
}
|
||||
|
@ -217,7 +208,7 @@ pub enum RouteType {
|
|||
Internal,
|
||||
}
|
||||
|
||||
pub struct FileData {
|
||||
pub struct ConfigData {
|
||||
pub path: PathBuf,
|
||||
pub file: File,
|
||||
}
|
||||
|
@ -226,19 +217,19 @@ pub struct FileData {
|
|||
/// locations for a place to write a config file to. In order, it checks the
|
||||
/// system-wide config location (`/etc/`, in Linux), followed by the config
|
||||
/// folder, followed by the user's home folder.
|
||||
pub fn get_config_data() -> Result<FileData, BunBunError> {
|
||||
pub fn get_config_data() -> Result<ConfigData, BunBunError> {
|
||||
// Locations to check, with highest priority first
|
||||
let locations: Vec<_> = {
|
||||
let mut folders = vec![PathBuf::from("/etc/")];
|
||||
|
||||
// Config folder
|
||||
if let Some(folder) = config_dir() {
|
||||
folders.push(folder);
|
||||
folders.push(folder)
|
||||
}
|
||||
|
||||
// Home folder
|
||||
if let Some(folder) = home_dir() {
|
||||
folders.push(folder);
|
||||
folders.push(folder)
|
||||
}
|
||||
|
||||
folders
|
||||
|
@ -251,18 +242,19 @@ pub fn get_config_data() -> Result<FileData, BunBunError> {
|
|||
debug!("Checking locations for config file: {:?}", &locations);
|
||||
|
||||
for location in &locations {
|
||||
let file = OpenOptions::new().read(true).open(location);
|
||||
let file = OpenOptions::new().read(true).open(location.clone());
|
||||
match file {
|
||||
Ok(file) => {
|
||||
debug!("Found file at {location:?}.");
|
||||
return Ok(FileData {
|
||||
debug!("Found file at {:?}.", location);
|
||||
return Ok(ConfigData {
|
||||
path: location.clone(),
|
||||
file,
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("Tried to read '{location:?}' but failed due to error: {e}");
|
||||
}
|
||||
Err(e) => debug!(
|
||||
"Tried to read '{:?}' but failed due to error: {}",
|
||||
location, e
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -278,18 +270,19 @@ pub fn get_config_data() -> Result<FileData, BunBunError> {
|
|||
.open(location.clone());
|
||||
match file {
|
||||
Ok(mut file) => {
|
||||
info!("Creating new config file at {location:?}.");
|
||||
info!("Creating new config file at {:?}.", location);
|
||||
file.write_all(DEFAULT_CONFIG)?;
|
||||
|
||||
let file = OpenOptions::new().read(true).open(location.clone())?;
|
||||
return Ok(FileData {
|
||||
return Ok(ConfigData {
|
||||
path: location,
|
||||
file,
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("Tried to open a new file at '{location:?}' but failed due to error: {e}",)
|
||||
}
|
||||
Err(e) => debug!(
|
||||
"Tried to open a new file at '{:?}' but failed due to error: {}",
|
||||
location, e
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -298,17 +291,22 @@ pub fn get_config_data() -> Result<FileData, BunBunError> {
|
|||
|
||||
/// Assumes that the user knows what they're talking about and will only try
|
||||
/// to load the config at the given path.
|
||||
pub fn load_custom_file(path: impl Into<PathBuf>) -> Result<FileData, BunBunError> {
|
||||
pub fn load_custom_path_config(
|
||||
path: impl Into<PathBuf>,
|
||||
) -> Result<ConfigData, BunBunError> {
|
||||
let path = path.into();
|
||||
let file = OpenOptions::new()
|
||||
.read(true)
|
||||
.open(&path)
|
||||
.map_err(|e| BunBunError::InvalidConfigPath(path.clone(), e))?;
|
||||
|
||||
Ok(FileData { path, file })
|
||||
Ok(ConfigData { file, path })
|
||||
}
|
||||
|
||||
pub fn load_file(mut config_file: File, large_config: bool) -> Result<Config, BunBunError> {
|
||||
pub fn read_config(
|
||||
mut config_file: File,
|
||||
large_config: bool,
|
||||
) -> Result<Config, BunBunError> {
|
||||
trace!("Loading config file.");
|
||||
let file_size = config_file.metadata()?.len();
|
||||
|
||||
|
@ -331,96 +329,90 @@ pub fn load_file(mut config_file: File, large_config: bool) -> Result<Config, Bu
|
|||
#[cfg(test)]
|
||||
mod route {
|
||||
use super::*;
|
||||
use anyhow::{Context, Result};
|
||||
use serde_yaml::{from_str, to_string};
|
||||
use std::path::Path;
|
||||
use tempfile::NamedTempFile;
|
||||
|
||||
#[test]
|
||||
fn deserialize_relative_path() -> Result<()> {
|
||||
let tmpfile = NamedTempFile::new_in(".")?;
|
||||
let path = tmpfile.path().display().to_string();
|
||||
let path = path
|
||||
.get(path.rfind(".").context("While finding .")?..)
|
||||
.context("While getting the path")?;
|
||||
let path = Path::new(path);
|
||||
fn deserialize_relative_path() {
|
||||
let tmpfile = NamedTempFile::new_in(".").unwrap();
|
||||
let path = format!("{}", tmpfile.path().display());
|
||||
let path = path.get(path.rfind(".").unwrap()..).unwrap();
|
||||
let path = std::path::Path::new(path);
|
||||
assert!(path.is_relative());
|
||||
let path = path.to_str().context("While stringifying path")?;
|
||||
assert_eq!(from_str::<Route>(path)?, Route::from(path.to_owned()));
|
||||
Ok(())
|
||||
let path = path.to_str().unwrap();
|
||||
assert_eq!(
|
||||
from_str::<Route>(path).unwrap(),
|
||||
Route::from_str(path).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_absolute_path() -> Result<()> {
|
||||
let tmpfile = NamedTempFile::new()?;
|
||||
fn deserialize_absolute_path() {
|
||||
let tmpfile = NamedTempFile::new().unwrap();
|
||||
let path = format!("{}", tmpfile.path().display());
|
||||
assert!(tmpfile.path().is_absolute());
|
||||
assert_eq!(from_str::<Route>(&path)?, Route::from(path));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_http_path() -> Result<()> {
|
||||
assert_eq!(
|
||||
from_str::<Route>("http://google.com")?,
|
||||
Route::from("http://google.com")
|
||||
from_str::<Route>(&path).unwrap(),
|
||||
Route::from_str(&path).unwrap()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_https_path() -> Result<()> {
|
||||
fn deserialize_http_path() {
|
||||
assert_eq!(
|
||||
from_str::<Route>("https://google.com")?,
|
||||
Route::from("https://google.com")
|
||||
from_str::<Route>("http://google.com").unwrap(),
|
||||
Route::from_str("http://google.com").unwrap()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize() -> Result<()> {
|
||||
fn deserialize_https_path() {
|
||||
assert_eq!(
|
||||
&to_string(&Route::from("hello world"))?,
|
||||
from_str::<Route>("https://google.com").unwrap(),
|
||||
Route::from_str("https://google.com").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize() {
|
||||
assert_eq!(
|
||||
&to_string(&Route::from_str("hello world").unwrap()).unwrap(),
|
||||
"---\nroute_type: External\npath: hello world\nhidden: false\ndescription: ~\nmin_args: ~\nmax_args: ~\n"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod read_config {
|
||||
use super::*;
|
||||
use anyhow::Result;
|
||||
|
||||
#[test]
|
||||
fn empty_file() -> Result<()> {
|
||||
let config_file = tempfile::tempfile()?;
|
||||
fn empty_file() {
|
||||
let config_file = tempfile::tempfile().unwrap();
|
||||
assert!(matches!(
|
||||
load_file(config_file, false),
|
||||
read_config(config_file, false),
|
||||
Err(BunBunError::ZeroByteConfig)
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_too_large() -> Result<()> {
|
||||
let mut config_file = tempfile::tempfile()?;
|
||||
fn config_too_large() {
|
||||
let mut config_file = tempfile::tempfile().unwrap();
|
||||
let size_to_write = (LARGE_FILE_SIZE_THRESHOLD + 1) as usize;
|
||||
config_file.write(&[0].repeat(size_to_write))?;
|
||||
match load_file(config_file, false) {
|
||||
Err(BunBunError::ConfigTooLarge(size)) if size as usize == size_to_write => {}
|
||||
config_file.write(&[0].repeat(size_to_write)).unwrap();
|
||||
match read_config(config_file, false) {
|
||||
Err(BunBunError::ConfigTooLarge(size))
|
||||
if size as usize == size_to_write => {}
|
||||
Err(BunBunError::ConfigTooLarge(size)) => {
|
||||
panic!("Mismatched size: {size} != {size_to_write}")
|
||||
panic!("Mismatched size: {} != {}", size, size_to_write)
|
||||
}
|
||||
res => panic!("Wrong result, got {res:#?}"),
|
||||
res => panic!("Wrong result, got {:#?}", res),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_config() -> Result<()> {
|
||||
assert!(load_file(File::open("bunbun.default.yaml")?, false).is_ok());
|
||||
Ok(())
|
||||
fn valid_config() {
|
||||
let config_file = File::open("bunbun.default.yaml").unwrap();
|
||||
assert!(read_config(config_file, false).is_ok());
|
||||
}
|
||||
}
|
||||
|
|
10
src/error.rs
10
src/error.rs
|
@ -2,11 +2,11 @@ use std::error::Error;
|
|||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(clippy::module_name_repetitions)]
|
||||
pub enum BunBunError {
|
||||
Io(std::io::Error),
|
||||
Parse(serde_yaml::Error),
|
||||
Watch(hotwatch::Error),
|
||||
LoggerInit(log::SetLoggerError),
|
||||
CustomProgram(String),
|
||||
NoValidConfigPath,
|
||||
InvalidConfigPath(std::path::PathBuf, std::io::Error),
|
||||
|
@ -23,12 +23,13 @@ impl fmt::Display for BunBunError {
|
|||
Self::Io(e) => e.fmt(f),
|
||||
Self::Parse(e) => e.fmt(f),
|
||||
Self::Watch(e) => e.fmt(f),
|
||||
Self::CustomProgram(msg) => msg.fmt(f),
|
||||
Self::LoggerInit(e) => e.fmt(f),
|
||||
Self::CustomProgram(msg) => write!(f, "{}", msg),
|
||||
Self::NoValidConfigPath => write!(f, "No valid config path was found!"),
|
||||
Self::InvalidConfigPath(path, reason) => {
|
||||
write!(f, "Failed to access {path:?}: {reason}")
|
||||
write!(f, "Failed to access {:?}: {}", path, reason)
|
||||
}
|
||||
Self::ConfigTooLarge(size) => write!(f, "The config file was too large ({size} bytes)! Pass in --large-config to bypass this check."),
|
||||
Self::ConfigTooLarge(size) => write!(f, "The config file was too large ({} bytes)! Pass in --large-config to bypass this check.", size),
|
||||
Self::ZeroByteConfig => write!(f, "The config provided reported a size of 0 bytes. Please check your config path!"),
|
||||
Self::JsonParse(e) => e.fmt(f),
|
||||
}
|
||||
|
@ -50,4 +51,5 @@ macro_rules! from_error {
|
|||
from_error!(std::io::Error, Io);
|
||||
from_error!(serde_yaml::Error, Parse);
|
||||
from_error!(hotwatch::Error, Watch);
|
||||
from_error!(log::SetLoggerError, LoggerInit);
|
||||
from_error!(serde_json::Error, JsonParse);
|
||||
|
|
220
src/main.rs
220
src/main.rs
|
@ -1,26 +1,25 @@
|
|||
#![forbid(unsafe_code)]
|
||||
#![deny(missing_docs)]
|
||||
#![warn(clippy::nursery, clippy::pedantic)]
|
||||
|
||||
//! Bunbun is a pure-Rust implementation of bunny1 that provides a customizable
|
||||
//! search engine and quick-jump tool in one small binary. For information on
|
||||
//! usage, please take a look at the readme.
|
||||
|
||||
use crate::config::{get_config_data, load_custom_file, load_file, FileData, Route, RouteGroup};
|
||||
use anyhow::Result;
|
||||
use arc_swap::ArcSwap;
|
||||
use axum::routing::get;
|
||||
use axum::{Extension, Router};
|
||||
use clap::Parser;
|
||||
use crate::config::{
|
||||
get_config_data, load_custom_path_config, read_config, ConfigData, Route,
|
||||
RouteGroup,
|
||||
};
|
||||
use actix_web::{middleware::Logger, App, HttpServer};
|
||||
use clap::Clap;
|
||||
use error::BunBunError;
|
||||
use handlebars::Handlebars;
|
||||
use handlebars::{Handlebars, TemplateError};
|
||||
use hotwatch::{Event, Hotwatch};
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use simple_logger::SimpleLogger;
|
||||
use std::cmp::min;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::time::Duration;
|
||||
use tracing::{debug, info, trace, warn};
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
|
||||
mod cli;
|
||||
mod config;
|
||||
|
@ -40,70 +39,100 @@ pub struct State {
|
|||
routes: HashMap<String, Route>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
#[actix_web::main]
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
async fn main() -> Result<()> {
|
||||
use tracing_subscriber::EnvFilter;
|
||||
async fn main() {
|
||||
std::process::exit(match run().await {
|
||||
Ok(_) => 0,
|
||||
Err(e) => {
|
||||
error!("{}", e);
|
||||
1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
async fn run() -> Result<(), BunBunError> {
|
||||
let opts = cli::Opts::parse();
|
||||
|
||||
let mut env_filter = EnvFilter::from_default_env();
|
||||
for directive in opts.log {
|
||||
env_filter = env_filter.add_directive(directive);
|
||||
}
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.with(env_filter)
|
||||
.init();
|
||||
init_logger(opts.verbose, opts.quiet)?;
|
||||
|
||||
let conf_data = match opts.config {
|
||||
Some(file_name) => load_custom_file(file_name),
|
||||
Some(file_name) => load_custom_path_config(file_name),
|
||||
None => get_config_data(),
|
||||
}?;
|
||||
|
||||
let conf = load_file(conf_data.file.try_clone()?, opts.large_config)?;
|
||||
let state = Arc::from(ArcSwap::from_pointee(State {
|
||||
let conf = read_config(conf_data.file.try_clone()?, opts.large_config)?;
|
||||
let state = Arc::from(RwLock::new(State {
|
||||
public_address: conf.public_address,
|
||||
default_route: conf.default_route,
|
||||
routes: cache_routes(conf.groups.clone()),
|
||||
routes: cache_routes(&conf.groups),
|
||||
groups: conf.groups,
|
||||
}));
|
||||
|
||||
// Cannot be named _ or Rust will immediately drop it.
|
||||
let _watch = start_watch(Arc::clone(&state), conf_data, opts.large_config);
|
||||
let _watch = start_watch(Arc::clone(&state), conf_data, opts.large_config)?;
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(routes::index))
|
||||
.route("/bunbunsearch.xml", get(routes::opensearch))
|
||||
.route("/ls", get(routes::list))
|
||||
.route("/hop", get(routes::hop))
|
||||
.layer(Extension(compile_templates()?))
|
||||
.layer(Extension(state));
|
||||
|
||||
let bind_addr = conf.bind_address.parse()?;
|
||||
|
||||
info!("Starting server at {bind_addr}");
|
||||
|
||||
axum::Server::bind(&bind_addr)
|
||||
.serve(app.into_make_service())
|
||||
HttpServer::new(move || {
|
||||
let templates = match compile_templates() {
|
||||
Ok(templates) => templates,
|
||||
// This implies a template error, which should be a compile time error. If
|
||||
// we reach here then the release is very broken.
|
||||
Err(e) => unreachable!("Failed to compile templates: {}", e),
|
||||
};
|
||||
App::new()
|
||||
.data(Arc::clone(&state))
|
||||
.app_data(templates)
|
||||
.wrap(Logger::default())
|
||||
.service(routes::hop)
|
||||
.service(routes::list)
|
||||
.service(routes::index)
|
||||
.service(routes::opensearch)
|
||||
})
|
||||
.bind(&conf.bind_address)?
|
||||
.run()
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Initializes the logger based on the number of quiet and verbose flags passed
|
||||
/// in. Usually, these values are mutually exclusive, that is, if the number of
|
||||
/// verbose flags is non-zero then the quiet flag is zero, and vice versa.
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
fn init_logger(
|
||||
num_verbose_flags: u8,
|
||||
num_quiet_flags: u8,
|
||||
) -> Result<(), BunBunError> {
|
||||
let log_level =
|
||||
match min(num_verbose_flags, 3) as i8 - min(num_quiet_flags, 2) as i8 {
|
||||
-2 => None,
|
||||
-1 => Some(log::LevelFilter::Error),
|
||||
0 => Some(log::LevelFilter::Warn),
|
||||
1 => Some(log::LevelFilter::Info),
|
||||
2 => Some(log::LevelFilter::Debug),
|
||||
3 => Some(log::LevelFilter::Trace),
|
||||
_ => unreachable!(), // values are clamped to [0, 3] - [0, 2]
|
||||
};
|
||||
|
||||
if let Some(level) = log_level {
|
||||
SimpleLogger::new().with_level(level).init()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Generates a hashmap of routes from the data structure created by the config
|
||||
/// file. This should improve runtime performance and is a better solution than
|
||||
/// just iterating over the config object for every hop resolution.
|
||||
fn cache_routes(groups: Vec<RouteGroup>) -> HashMap<String, Route> {
|
||||
fn cache_routes(groups: &[RouteGroup]) -> HashMap<String, Route> {
|
||||
let mut mapping = HashMap::new();
|
||||
for group in groups {
|
||||
for (kw, dest) in group.routes {
|
||||
// This function isn't called often enough to not be a performance issue.
|
||||
for (kw, dest) in &group.routes {
|
||||
match mapping.insert(kw.clone(), dest.clone()) {
|
||||
None => trace!("Inserting {kw} into mapping."),
|
||||
None => trace!("Inserting {} into mapping.", kw),
|
||||
Some(old_value) => {
|
||||
trace!("Overriding {kw} route from {old_value} to {dest}.");
|
||||
trace!("Overriding {} route from {} to {}.", kw, old_value, dest)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -114,7 +143,7 @@ fn cache_routes(groups: Vec<RouteGroup>) -> HashMap<String, Route> {
|
|||
/// Returns an instance with all pre-generated templates included into the
|
||||
/// binary. This allows for users to have a portable binary without needed the
|
||||
/// templates at runtime.
|
||||
fn compile_templates() -> Result<Handlebars<'static>> {
|
||||
fn compile_templates() -> Result<Handlebars<'static>, TemplateError> {
|
||||
let mut handlebars = Handlebars::new();
|
||||
handlebars.set_strict_mode(true);
|
||||
handlebars.register_partial("bunbun_version", env!("CARGO_PKG_VERSION"))?;
|
||||
|
@ -147,68 +176,106 @@ fn compile_templates() -> Result<Handlebars<'static>> {
|
|||
/// watches.
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
fn start_watch(
|
||||
state: Arc<ArcSwap<State>>,
|
||||
config_data: FileData,
|
||||
state: Arc<RwLock<State>>,
|
||||
config_data: ConfigData,
|
||||
large_config: bool,
|
||||
) -> Result<Hotwatch> {
|
||||
) -> Result<Hotwatch, BunBunError> {
|
||||
let mut watch = Hotwatch::new_with_custom_delay(Duration::from_millis(500))?;
|
||||
let FileData { path, mut file } = config_data;
|
||||
let ConfigData { path, mut file } = config_data;
|
||||
let watch_result = watch.watch(&path, move |e: Event| {
|
||||
if let Event::Create(ref path) = e {
|
||||
file = load_custom_file(path).expect("file to exist at path").file;
|
||||
file = load_custom_path_config(path)
|
||||
.expect("file to exist at path")
|
||||
.file;
|
||||
trace!("Getting new file handler as file was recreated.");
|
||||
}
|
||||
|
||||
match e {
|
||||
Event::Write(_) | Event::Create(_) => {
|
||||
trace!("Grabbing writer lock on state...");
|
||||
let mut state =
|
||||
state.write().expect("Failed to get write lock on state");
|
||||
trace!("Obtained writer lock on state!");
|
||||
match load_file(
|
||||
match read_config(
|
||||
file.try_clone().expect("Failed to clone file handle"),
|
||||
large_config,
|
||||
) {
|
||||
Ok(conf) => {
|
||||
state.store(Arc::new(State {
|
||||
public_address: conf.public_address,
|
||||
default_route: conf.default_route,
|
||||
routes: cache_routes(conf.groups.clone()),
|
||||
groups: conf.groups,
|
||||
}));
|
||||
state.public_address = conf.public_address;
|
||||
state.default_route = conf.default_route;
|
||||
state.routes = cache_routes(&conf.groups);
|
||||
state.groups = conf.groups;
|
||||
info!("Successfully updated active state");
|
||||
}
|
||||
Err(e) => warn!("Failed to update config file: {e}"),
|
||||
Err(e) => warn!("Failed to update config file: {}", e),
|
||||
}
|
||||
}
|
||||
_ => debug!("Saw event {e:#?} but ignored it"),
|
||||
_ => debug!("Saw event {:#?} but ignored it", e),
|
||||
}
|
||||
});
|
||||
|
||||
match watch_result {
|
||||
Ok(_) => info!("Watcher is now watching {path:?}"),
|
||||
Err(e) => {
|
||||
warn!("Couldn't watch {path:?}: {e}. Changes to this file won't be seen!");
|
||||
}
|
||||
Ok(_) => info!("Watcher is now watching {:?}", &path),
|
||||
Err(e) => warn!(
|
||||
"Couldn't watch {:?}: {}. Changes to this file won't be seen!",
|
||||
&path, e
|
||||
),
|
||||
}
|
||||
|
||||
Ok(watch)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod init_logger {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn defaults_to_warn() -> Result<(), BunBunError> {
|
||||
init_logger(0, 0)?;
|
||||
assert_eq!(log::max_level(), log::Level::Warn);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// The following tests work but because the log crate is global, initializing
|
||||
// the logger more than once (read: testing it more than once) leads to a
|
||||
// panic. These ignored tests must be manually tested.
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn caps_to_2_when_log_level_is_lt_2() -> Result<(), BunBunError> {
|
||||
init_logger(0, 3)?;
|
||||
assert_eq!(log::max_level(), log::LevelFilter::Off);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn caps_to_3_when_log_level_is_gt_3() -> Result<(), BunBunError> {
|
||||
init_logger(4, 0)?;
|
||||
assert_eq!(log::max_level(), log::Level::Trace);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod cache_routes {
|
||||
use super::*;
|
||||
use std::iter::FromIterator;
|
||||
use std::str::FromStr;
|
||||
|
||||
fn generate_external_routes(routes: &[(&'static str, &'static str)]) -> HashMap<String, Route> {
|
||||
fn generate_external_routes(
|
||||
routes: &[(&str, &str)],
|
||||
) -> HashMap<String, Route> {
|
||||
HashMap::from_iter(
|
||||
routes
|
||||
.into_iter()
|
||||
.map(|(key, value)| ((*key).to_owned(), Route::from(*value))),
|
||||
.map(|kv| (kv.0.into(), Route::from_str(kv.1).unwrap())),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_groups_yield_empty_routes() {
|
||||
assert_eq!(cache_routes(Vec::new()), HashMap::new());
|
||||
assert_eq!(cache_routes(&[]), HashMap::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -228,8 +295,13 @@ mod cache_routes {
|
|||
};
|
||||
|
||||
assert_eq!(
|
||||
cache_routes(vec![group1, group2]),
|
||||
generate_external_routes(&[("a", "b"), ("c", "d"), ("1", "2"), ("3", "4")])
|
||||
cache_routes(&[group1, group2]),
|
||||
generate_external_routes(&[
|
||||
("a", "b"),
|
||||
("c", "d"),
|
||||
("1", "2"),
|
||||
("3", "4")
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -250,7 +322,7 @@ mod cache_routes {
|
|||
};
|
||||
|
||||
assert_eq!(
|
||||
cache_routes(vec![group1.clone(), group2]),
|
||||
cache_routes(&[group1.clone(), group2]),
|
||||
generate_external_routes(&[("a", "1"), ("c", "2")])
|
||||
);
|
||||
|
||||
|
@ -262,7 +334,7 @@ mod cache_routes {
|
|||
};
|
||||
|
||||
assert_eq!(
|
||||
cache_routes(vec![group1, group3]),
|
||||
cache_routes(&[group1, group3]),
|
||||
generate_external_routes(&[("a", "1"), ("b", "2"), ("c", "d")])
|
||||
);
|
||||
}
|
||||
|
|
250
src/routes.rs
250
src/routes.rs
|
@ -1,21 +1,18 @@
|
|||
use crate::config::{Route as ConfigRoute, RouteType};
|
||||
use crate::{template_args, BunBunError, Route, State};
|
||||
use arc_swap::ArcSwap;
|
||||
use axum::body::{boxed, Bytes, Full};
|
||||
use axum::extract::Query;
|
||||
use axum::http::{header, StatusCode};
|
||||
use axum::response::{Html, IntoResponse, Response};
|
||||
use axum::Extension;
|
||||
use actix_web::web::{Data, Query};
|
||||
use actix_web::{get, http::header};
|
||||
use actix_web::{HttpRequest, HttpResponse, Responder};
|
||||
use handlebars::Handlebars;
|
||||
use log::{debug, error};
|
||||
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use std::sync::Arc;
|
||||
use tracing::{debug, error};
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
// https://url.spec.whatwg.org/#fragment-percent-encode-set
|
||||
/// https://url.spec.whatwg.org/#fragment-percent-encode-set
|
||||
const FRAGMENT_ENCODE_SET: &AsciiSet = &CONTROLS
|
||||
.add(b' ')
|
||||
.add(b'"')
|
||||
|
@ -24,69 +21,73 @@ const FRAGMENT_ENCODE_SET: &AsciiSet = &CONTROLS
|
|||
.add(b'`')
|
||||
.add(b'+')
|
||||
.add(b'&') // Interpreted as a GET query
|
||||
.add(b'#') // Interpreted as a hyperlink section target
|
||||
.add(b'\'');
|
||||
.add(b'#'); // Interpreted as a hyperlink section target
|
||||
|
||||
#[allow(clippy::unused_async)]
|
||||
pub async fn index(
|
||||
Extension(data): Extension<Arc<ArcSwap<State>>>,
|
||||
Extension(handlebars): Extension<Handlebars<'static>>,
|
||||
) -> impl IntoResponse {
|
||||
handlebars
|
||||
type StateData = Data<Arc<RwLock<State>>>;
|
||||
|
||||
#[get("/")]
|
||||
pub async fn index(data: StateData, req: HttpRequest) -> impl Responder {
|
||||
let data = data.read().unwrap();
|
||||
HttpResponse::Ok()
|
||||
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
||||
.body(
|
||||
req
|
||||
.app_data::<Handlebars>()
|
||||
.unwrap()
|
||||
.render(
|
||||
"index",
|
||||
&template_args::hostname(&data.load().public_address),
|
||||
&template_args::hostname(data.public_address.clone()),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.map(Html)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
|
||||
#[allow(clippy::unused_async)]
|
||||
pub async fn opensearch(
|
||||
Extension(data): Extension<Arc<ArcSwap<State>>>,
|
||||
Extension(handlebars): Extension<Handlebars<'static>>,
|
||||
) -> impl IntoResponse {
|
||||
handlebars
|
||||
.render(
|
||||
"opensearch",
|
||||
&template_args::hostname(&data.load().public_address),
|
||||
)
|
||||
.map(|body| {
|
||||
(
|
||||
StatusCode::OK,
|
||||
[(
|
||||
#[get("/bunbunsearch.xml")]
|
||||
pub async fn opensearch(data: StateData, req: HttpRequest) -> impl Responder {
|
||||
let data = data.read().unwrap();
|
||||
HttpResponse::Ok()
|
||||
.header(
|
||||
header::CONTENT_TYPE,
|
||||
"application/opensearchdescription+xml",
|
||||
)],
|
||||
body,
|
||||
)
|
||||
})
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(
|
||||
req
|
||||
.app_data::<Handlebars>()
|
||||
.unwrap()
|
||||
.render(
|
||||
"opensearch",
|
||||
&template_args::hostname(data.public_address.clone()),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::unused_async)]
|
||||
pub async fn list(
|
||||
Extension(data): Extension<Arc<ArcSwap<State>>>,
|
||||
Extension(handlebars): Extension<Handlebars<'static>>,
|
||||
) -> impl IntoResponse {
|
||||
handlebars
|
||||
.render("list", &data.load().groups)
|
||||
.map(Html)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
||||
#[get("/ls")]
|
||||
pub async fn list(data: StateData, req: HttpRequest) -> impl Responder {
|
||||
let data = data.read().unwrap();
|
||||
HttpResponse::Ok()
|
||||
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
||||
.body(
|
||||
req
|
||||
.app_data::<Handlebars>()
|
||||
.unwrap()
|
||||
.render("list", &data.groups)
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[derive(Deserialize)]
|
||||
pub struct SearchQuery {
|
||||
to: String,
|
||||
}
|
||||
|
||||
#[allow(clippy::unused_async)]
|
||||
#[get("/hop")]
|
||||
pub async fn hop(
|
||||
Extension(data): Extension<Arc<ArcSwap<State>>>,
|
||||
Extension(handlebars): Extension<Handlebars<'static>>,
|
||||
Query(query): Query<SearchQuery>,
|
||||
) -> impl IntoResponse {
|
||||
let data = data.load();
|
||||
data: StateData,
|
||||
req: HttpRequest,
|
||||
query: Query<SearchQuery>,
|
||||
) -> impl Responder {
|
||||
let data = data.read().unwrap();
|
||||
|
||||
match resolve_hop(&query.to, &data.routes, &data.default_route) {
|
||||
RouteResolution::Resolved { route: path, args } => {
|
||||
|
@ -95,7 +96,7 @@ pub async fn hop(
|
|||
route_type: RouteType::Internal,
|
||||
path,
|
||||
..
|
||||
} => resolve_path(Path::new(path), &args),
|
||||
} => resolve_path(PathBuf::from(path), &args),
|
||||
ConfigRoute {
|
||||
route_type: RouteType::External,
|
||||
path,
|
||||
|
@ -104,34 +105,30 @@ pub async fn hop(
|
|||
};
|
||||
|
||||
match resolved_template {
|
||||
Ok(HopAction::Redirect(path)) => {
|
||||
let rendered = handlebars
|
||||
Ok(HopAction::Redirect(path)) => HttpResponse::Found()
|
||||
.header(
|
||||
header::LOCATION,
|
||||
req
|
||||
.app_data::<Handlebars>()
|
||||
.unwrap()
|
||||
.render_template(
|
||||
&path,
|
||||
&template_args::query(utf8_percent_encode(&args, FRAGMENT_ENCODE_SET)),
|
||||
std::str::from_utf8(path.as_bytes()).unwrap(),
|
||||
&template_args::query(
|
||||
utf8_percent_encode(&args, FRAGMENT_ENCODE_SET).to_string(),
|
||||
),
|
||||
)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Response::builder()
|
||||
.status(StatusCode::FOUND)
|
||||
.header(header::LOCATION, &path)
|
||||
.body(boxed(Full::from(rendered)))
|
||||
}
|
||||
Ok(HopAction::Body(body)) => Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body(boxed(Full::new(Bytes::from(body)))),
|
||||
.unwrap(),
|
||||
)
|
||||
.finish(),
|
||||
Ok(HopAction::Body(body)) => HttpResponse::Ok().body(body),
|
||||
Err(e) => {
|
||||
error!("Failed to redirect user for {path}: {e}");
|
||||
Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(boxed(Full::from("Something went wrong :(\n")))
|
||||
error!("Failed to redirect user for {}: {}", path, e);
|
||||
HttpResponse::InternalServerError().body("Something went wrong :(\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
RouteResolution::Unresolved => Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(boxed(Full::from("not found\n"))),
|
||||
RouteResolution::Unresolved => HttpResponse::NotFound().body("not found"),
|
||||
}
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
|
@ -170,7 +167,7 @@ fn resolve_hop<'a>(
|
|||
let args = if args.is_empty() { &[] } else { &args[1..] }.join(" ");
|
||||
let arg_count = arg_count - 1;
|
||||
if check_route(route, arg_count) {
|
||||
debug!("Resolved {route} with args {args}");
|
||||
debug!("Resolved {} with args {}", route, args);
|
||||
return RouteResolution::Resolved { route, args };
|
||||
}
|
||||
}
|
||||
|
@ -180,7 +177,7 @@ fn resolve_hop<'a>(
|
|||
if let Some(route) = routes.get(route) {
|
||||
if check_route(route, arg_count) {
|
||||
let args = args.join(" ");
|
||||
debug!("Using default route {route} with args {args}");
|
||||
debug!("Using default route {} with args {}", route, args);
|
||||
return RouteResolution::Resolved { route, args };
|
||||
}
|
||||
}
|
||||
|
@ -191,7 +188,7 @@ fn resolve_hop<'a>(
|
|||
|
||||
/// Checks if the user provided string has the correct properties required by
|
||||
/// the route to be successfully matched.
|
||||
const fn check_route(route: &Route, arg_count: usize) -> bool {
|
||||
fn check_route(route: &Route, arg_count: usize) -> bool {
|
||||
if let Some(min_args) = route.min_args {
|
||||
if arg_count < min_args {
|
||||
return false;
|
||||
|
@ -218,7 +215,7 @@ enum HopAction {
|
|||
/// so long as the executable was successfully executed. Returns an Error if the
|
||||
/// file doesn't exist or bunbun did not have permission to read and execute the
|
||||
/// file.
|
||||
fn resolve_path(path: &Path, args: &str) -> Result<HopAction, BunBunError> {
|
||||
fn resolve_path(path: PathBuf, args: &str) -> Result<HopAction, BunBunError> {
|
||||
let output = Command::new(path.canonicalize()?)
|
||||
.args(args.split(' '))
|
||||
.output()?;
|
||||
|
@ -238,9 +235,12 @@ fn resolve_path(path: &Path, args: &str) -> Result<HopAction, BunBunError> {
|
|||
#[cfg(test)]
|
||||
mod resolve_hop {
|
||||
use super::*;
|
||||
use anyhow::Result;
|
||||
use std::str::FromStr;
|
||||
|
||||
fn generate_route_result<'a>(keyword: &'a Route, args: &str) -> RouteResolution<'a> {
|
||||
fn generate_route_result<'a>(
|
||||
keyword: &'a Route,
|
||||
args: &str,
|
||||
) -> RouteResolution<'a> {
|
||||
RouteResolution::Resolved {
|
||||
route: keyword,
|
||||
args: String::from(args),
|
||||
|
@ -268,36 +268,51 @@ mod resolve_hop {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn only_default_routes_some_default_yields_default_hop() -> Result<()> {
|
||||
fn only_default_routes_some_default_yields_default_hop() {
|
||||
let mut map: HashMap<String, Route> = HashMap::new();
|
||||
map.insert("google".into(), Route::from("https://example.com"));
|
||||
map.insert(
|
||||
"google".into(),
|
||||
Route::from_str("https://example.com").unwrap(),
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_hop("hello world", &map, &Some(String::from("google"))),
|
||||
generate_route_result(&Route::from("https://example.com"), "hello world"),
|
||||
generate_route_result(
|
||||
&Route::from_str("https://example.com").unwrap(),
|
||||
"hello world"
|
||||
),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_default_routes_some_default_yields_non_default_hop() -> Result<()> {
|
||||
fn non_default_routes_some_default_yields_non_default_hop() {
|
||||
let mut map: HashMap<String, Route> = HashMap::new();
|
||||
map.insert("google".into(), Route::from("https://example.com"));
|
||||
map.insert(
|
||||
"google".into(),
|
||||
Route::from_str("https://example.com").unwrap(),
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_hop("google hello world", &map, &Some(String::from("a"))),
|
||||
generate_route_result(&Route::from("https://example.com"), "hello world"),
|
||||
generate_route_result(
|
||||
&Route::from_str("https://example.com").unwrap(),
|
||||
"hello world"
|
||||
),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_default_routes_no_default_yields_non_default_hop() -> Result<()> {
|
||||
fn non_default_routes_no_default_yields_non_default_hop() {
|
||||
let mut map: HashMap<String, Route> = HashMap::new();
|
||||
map.insert("google".into(), Route::from("https://example.com"));
|
||||
map.insert(
|
||||
"google".into(),
|
||||
Route::from_str("https://example.com").unwrap(),
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_hop("google hello world", &map, &None),
|
||||
generate_route_result(&Route::from("https://example.com"), "hello world"),
|
||||
generate_route_result(
|
||||
&Route::from_str("https://example.com").unwrap(),
|
||||
"hello world"
|
||||
),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -354,65 +369,62 @@ mod check_route {
|
|||
|
||||
#[cfg(test)]
|
||||
mod resolve_path {
|
||||
use crate::error::BunBunError;
|
||||
|
||||
use super::{resolve_path, HopAction};
|
||||
use anyhow::Result;
|
||||
use std::env::current_dir;
|
||||
use std::io::ErrorKind;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[test]
|
||||
fn invalid_path_returns_err() {
|
||||
assert!(resolve_path(&Path::new("/bin/aaaa"), "aaaa").is_err());
|
||||
assert!(resolve_path(PathBuf::from("/bin/aaaa"), "aaaa").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_path_returns_ok() {
|
||||
assert!(resolve_path(&Path::new("/bin/echo"), r#"{"body": "a"}"#).is_ok());
|
||||
assert!(
|
||||
resolve_path(PathBuf::from("/bin/echo"), r#"{"body": "a"}"#).is_ok()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relative_path_returns_ok() -> Result<()> {
|
||||
fn relative_path_returns_ok() {
|
||||
// How many ".." needed to get to /
|
||||
let nest_level = current_dir()?.ancestors().count() - 1;
|
||||
let nest_level = current_dir().unwrap().ancestors().count() - 1;
|
||||
let mut rel_path = PathBuf::from("../".repeat(nest_level));
|
||||
rel_path.push("./bin/echo");
|
||||
assert!(resolve_path(&rel_path, r#"{"body": "a"}"#).is_ok());
|
||||
Ok(())
|
||||
assert!(resolve_path(rel_path, r#"{"body": "a"}"#).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_permissions_returns_err() {
|
||||
let result = match resolve_path(&Path::new("/root/some_exec"), "") {
|
||||
Err(BunBunError::Io(e)) => e.kind() == ErrorKind::PermissionDenied,
|
||||
_ => false,
|
||||
};
|
||||
assert!(result);
|
||||
assert!(
|
||||
// Trying to run a command without permission
|
||||
format!(
|
||||
"{}",
|
||||
resolve_path(PathBuf::from("/root/some_exec"), "").unwrap_err()
|
||||
)
|
||||
.contains("Permission denied")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_success_exit_code_yields_err() {
|
||||
// cat-ing a folder always returns exit code 1
|
||||
assert!(resolve_path(&Path::new("/bin/cat"), "/").is_err());
|
||||
assert!(resolve_path(PathBuf::from("/bin/cat"), "/").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn return_body() -> Result<()> {
|
||||
fn return_body() {
|
||||
assert_eq!(
|
||||
resolve_path(&Path::new("/bin/echo"), r#"{"body": "a"}"#)?,
|
||||
resolve_path(PathBuf::from("/bin/echo"), r#"{"body": "a"}"#).unwrap(),
|
||||
HopAction::Body("a".to_string())
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn return_redirect() -> Result<()> {
|
||||
fn return_redirect() {
|
||||
assert_eq!(
|
||||
resolve_path(&Path::new("/bin/echo"), r#"{"redirect": "a"}"#)?,
|
||||
resolve_path(PathBuf::from("/bin/echo"), r#"{"redirect": "a"}"#).unwrap(),
|
||||
HopAction::Redirect("a".to_string())
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +1,17 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use percent_encoding::PercentEncode;
|
||||
use serde::Serialize;
|
||||
|
||||
pub fn query(query: PercentEncode<'_>) -> impl Serialize + '_ {
|
||||
pub fn query(query: String) -> impl Serialize {
|
||||
#[derive(Serialize)]
|
||||
struct TemplateArgs<'a> {
|
||||
query: Cow<'a, str>,
|
||||
}
|
||||
TemplateArgs {
|
||||
query: query.into(),
|
||||
struct TemplateArgs {
|
||||
query: String,
|
||||
}
|
||||
TemplateArgs { query }
|
||||
}
|
||||
|
||||
pub fn hostname(hostname: &'_ str) -> impl Serialize + '_ {
|
||||
pub fn hostname(hostname: String) -> impl Serialize {
|
||||
#[derive(Serialize)]
|
||||
pub struct TemplateArgs<'a> {
|
||||
pub hostname: &'a str,
|
||||
pub struct TemplateArgs {
|
||||
pub hostname: String,
|
||||
}
|
||||
TemplateArgs { hostname }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue