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"
|
name = "bunbun"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
authors = ["Edward Shen <code@eddie.sh>"]
|
authors = ["Edward Shen <code@eddie.sh>"]
|
||||||
edition = "2021"
|
edition = "2018"
|
||||||
description = "Re-implementation of bunny1 in Rust"
|
description = "Re-implementation of bunny1 in Rust"
|
||||||
license = "AGPL-3.0"
|
license = "AGPL-3.0"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
@ -10,25 +10,20 @@ repository = "https://github.com/edward-shen/bunbun"
|
||||||
exclude = ["/aux/"]
|
exclude = ["/aux/"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1"
|
actix-web = "3"
|
||||||
arc-swap = "1"
|
clap = { version = "3.0.0-beta.2", features = ["wrap_help"] }
|
||||||
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
dirs = "3"
|
||||||
axum = "0.5"
|
handlebars = "3"
|
||||||
clap = { version = "3", features = ["wrap_help", "derive", "cargo"] }
|
|
||||||
dirs = "4"
|
|
||||||
handlebars = "4"
|
|
||||||
hotwatch = "0.4"
|
hotwatch = "0.4"
|
||||||
|
log = "0.4"
|
||||||
percent-encoding = "2"
|
percent-encoding = "2"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = "1"
|
||||||
serde_yaml = "0.8"
|
serde_yaml = "0.8"
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
tracing = "0.1"
|
simple_logger = "1"
|
||||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
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
|
26
src/cli.rs
26
src/cli.rs
|
@ -1,17 +1,19 @@
|
||||||
use clap::{crate_authors, crate_version, Parser};
|
use clap::{crate_authors, crate_version, Clap};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use tracing_subscriber::filter::Directive;
|
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Clap)]
|
||||||
#[clap(version = crate_version!(), author = crate_authors!())]
|
#[clap(version = crate_version!(), author = crate_authors!())]
|
||||||
pub struct Opts {
|
pub struct Opts {
|
||||||
/// Set the logging directives
|
/// Increases the log level to info, debug, and trace, respectively.
|
||||||
#[clap(long, default_value = "info")]
|
#[clap(short, long, parse(from_occurrences), conflicts_with("quiet"))]
|
||||||
pub log: Vec<Directive>,
|
pub verbose: u8,
|
||||||
/// Specify the location of the config file to read from. Needs read/write permissions.
|
/// Decreases the log level to error or no logging at all, respectively.
|
||||||
#[clap(short, long)]
|
#[clap(short, long, parse(from_occurrences), conflicts_with("verbose"))]
|
||||||
pub config: Option<PathBuf>,
|
pub quiet: u8,
|
||||||
/// Allow config sizes larger than 100MB.
|
/// Specify the location of the config file to read from. Needs read/write permissions.
|
||||||
#[clap(long)]
|
#[clap(short, long)]
|
||||||
pub large_config: bool,
|
pub config: Option<PathBuf>,
|
||||||
|
/// Allow config sizes larger than 100MB.
|
||||||
|
#[clap(long)]
|
||||||
|
pub large_config: bool,
|
||||||
}
|
}
|
||||||
|
|
658
src/config.rs
658
src/config.rs
|
@ -1,15 +1,16 @@
|
||||||
use crate::BunBunError;
|
use crate::BunBunError;
|
||||||
use dirs::{config_dir, home_dir};
|
use dirs::{config_dir, home_dir};
|
||||||
|
use log::{debug, info, trace};
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{self, Deserializer, MapAccess, Unexpected, Visitor},
|
de::{self, Deserializer, MapAccess, Unexpected, Visitor},
|
||||||
Deserialize, Serialize,
|
Deserialize, Serialize,
|
||||||
};
|
};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fs::{File, OpenOptions};
|
use std::fs::{File, OpenOptions};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use tracing::{debug, info, trace};
|
use std::str::FromStr;
|
||||||
|
|
||||||
const CONFIG_FILENAME: &str = "bunbun.yaml";
|
const CONFIG_FILENAME: &str = "bunbun.yaml";
|
||||||
const DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.yaml");
|
const DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.yaml");
|
||||||
|
@ -20,55 +21,43 @@ const LARGE_FILE_SIZE_THRESHOLD: u64 = 1_000_000;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, PartialEq)]
|
#[derive(Deserialize, Debug, PartialEq)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub bind_address: String,
|
pub bind_address: String,
|
||||||
pub public_address: String,
|
pub public_address: String,
|
||||||
pub default_route: Option<String>,
|
pub default_route: Option<String>,
|
||||||
pub groups: Vec<RouteGroup>,
|
pub groups: Vec<RouteGroup>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
|
#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
|
||||||
pub struct RouteGroup {
|
pub struct RouteGroup {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub hidden: bool,
|
pub hidden: bool,
|
||||||
pub routes: HashMap<String, Route>,
|
pub routes: HashMap<String, Route>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone, Serialize)]
|
#[derive(Debug, PartialEq, Clone, Serialize)]
|
||||||
pub struct Route {
|
pub struct Route {
|
||||||
pub route_type: RouteType,
|
pub route_type: RouteType,
|
||||||
pub path: String,
|
pub path: String,
|
||||||
pub hidden: bool,
|
pub hidden: bool,
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
pub min_args: Option<usize>,
|
pub min_args: Option<usize>,
|
||||||
pub max_args: Option<usize>,
|
pub max_args: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<String> for Route {
|
impl FromStr for Route {
|
||||||
fn from(s: String) -> Self {
|
type Err = std::convert::Infallible;
|
||||||
Self {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
route_type: get_route_type(&s),
|
Ok(Self {
|
||||||
path: s,
|
route_type: get_route_type(s),
|
||||||
hidden: false,
|
path: s.to_string(),
|
||||||
description: None,
|
hidden: false,
|
||||||
min_args: None,
|
description: None,
|
||||||
max_args: None,
|
min_args: None,
|
||||||
}
|
max_args: None,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&'static str> for Route {
|
|
||||||
fn from(s: &'static str) -> Self {
|
|
||||||
Self {
|
|
||||||
route_type: get_route_type(s),
|
|
||||||
path: s.to_string(),
|
|
||||||
hidden: false,
|
|
||||||
description: None,
|
|
||||||
min_args: None,
|
|
||||||
max_args: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialization of the route string into the enum requires us to figure out
|
/// Deserialization of the route string into the enum requires us to figure out
|
||||||
|
@ -77,350 +66,353 @@ impl From<&'static str> for Route {
|
||||||
/// web path. This incurs a disk check operation, but since users shouldn't be
|
/// web path. This incurs a disk check operation, but since users shouldn't be
|
||||||
/// updating the config that frequently, it should be fine.
|
/// updating the config that frequently, it should be fine.
|
||||||
impl<'de> Deserialize<'de> for Route {
|
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
|
where
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
#[serde(field_identifier, rename_all = "snake_case")]
|
#[serde(field_identifier, rename_all = "snake_case")]
|
||||||
enum Field {
|
enum Field {
|
||||||
Path,
|
Path,
|
||||||
Hidden,
|
Hidden,
|
||||||
Description,
|
Description,
|
||||||
MinArgs,
|
MinArgs,
|
||||||
MaxArgs,
|
MaxArgs,
|
||||||
}
|
|
||||||
|
|
||||||
struct RouteVisitor;
|
|
||||||
|
|
||||||
impl<'de> Visitor<'de> for RouteVisitor {
|
|
||||||
type Value = Route;
|
|
||||||
|
|
||||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
formatter.write_str("string")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_str<E>(self, path: &str) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: serde::de::Error,
|
|
||||||
{
|
|
||||||
Ok(Self::Value::from(path.to_owned()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
|
|
||||||
where
|
|
||||||
M: MapAccess<'de>,
|
|
||||||
{
|
|
||||||
let mut path = None;
|
|
||||||
let mut hidden = None;
|
|
||||||
let mut description = None;
|
|
||||||
let mut min_args = None;
|
|
||||||
let mut max_args = None;
|
|
||||||
|
|
||||||
while let Some(key) = map.next_key()? {
|
|
||||||
match key {
|
|
||||||
Field::Path => {
|
|
||||||
if path.is_some() {
|
|
||||||
return Err(de::Error::duplicate_field("path"));
|
|
||||||
}
|
|
||||||
path = Some(map.next_value::<String>()?);
|
|
||||||
}
|
|
||||||
Field::Hidden => {
|
|
||||||
if hidden.is_some() {
|
|
||||||
return Err(de::Error::duplicate_field("hidden"));
|
|
||||||
}
|
|
||||||
hidden = map.next_value()?;
|
|
||||||
}
|
|
||||||
Field::Description => {
|
|
||||||
if description.is_some() {
|
|
||||||
return Err(de::Error::duplicate_field("description"));
|
|
||||||
}
|
|
||||||
description = Some(map.next_value()?);
|
|
||||||
}
|
|
||||||
Field::MinArgs => {
|
|
||||||
if min_args.is_some() {
|
|
||||||
return Err(de::Error::duplicate_field("min_args"));
|
|
||||||
}
|
|
||||||
min_args = Some(map.next_value()?);
|
|
||||||
}
|
|
||||||
Field::MaxArgs => {
|
|
||||||
if max_args.is_some() {
|
|
||||||
return Err(de::Error::duplicate_field("max_args"));
|
|
||||||
}
|
|
||||||
max_args = Some(map.next_value()?);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let (Some(min_args), Some(max_args)) = (min_args, max_args) {
|
|
||||||
if min_args > max_args {
|
|
||||||
{
|
|
||||||
return Err(de::Error::invalid_value(
|
|
||||||
Unexpected::Other(&format!(
|
|
||||||
"argument count range {min_args} to {max_args}",
|
|
||||||
)),
|
|
||||||
&"a valid argument count range",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let path = path.ok_or_else(|| de::Error::missing_field("path"))?;
|
|
||||||
Ok(Route {
|
|
||||||
route_type: get_route_type(&path),
|
|
||||||
path,
|
|
||||||
hidden: hidden.unwrap_or_default(),
|
|
||||||
description,
|
|
||||||
min_args,
|
|
||||||
max_args,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deserializer.deserialize_any(RouteVisitor)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct RouteVisitor;
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for RouteVisitor {
|
||||||
|
type Value = Route;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
formatter.write_str("string")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<E>(self, path: &str) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: serde::de::Error,
|
||||||
|
{
|
||||||
|
// This is infallible
|
||||||
|
Ok(Self::Value::from_str(path).unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
|
||||||
|
where
|
||||||
|
M: MapAccess<'de>,
|
||||||
|
{
|
||||||
|
let mut path = None;
|
||||||
|
let mut hidden = None;
|
||||||
|
let mut description = None;
|
||||||
|
let mut min_args = None;
|
||||||
|
let mut max_args = None;
|
||||||
|
|
||||||
|
while let Some(key) = map.next_key()? {
|
||||||
|
match key {
|
||||||
|
Field::Path => {
|
||||||
|
if path.is_some() {
|
||||||
|
return Err(de::Error::duplicate_field("path"));
|
||||||
|
}
|
||||||
|
path = Some(map.next_value::<String>()?);
|
||||||
|
}
|
||||||
|
Field::Hidden => {
|
||||||
|
if hidden.is_some() {
|
||||||
|
return Err(de::Error::duplicate_field("hidden"));
|
||||||
|
}
|
||||||
|
hidden = map.next_value()?;
|
||||||
|
}
|
||||||
|
Field::Description => {
|
||||||
|
if description.is_some() {
|
||||||
|
return Err(de::Error::duplicate_field("description"));
|
||||||
|
}
|
||||||
|
description = Some(map.next_value()?);
|
||||||
|
}
|
||||||
|
Field::MinArgs => {
|
||||||
|
if min_args.is_some() {
|
||||||
|
return Err(de::Error::duplicate_field("min_args"));
|
||||||
|
}
|
||||||
|
min_args = Some(map.next_value()?);
|
||||||
|
}
|
||||||
|
Field::MaxArgs => {
|
||||||
|
if max_args.is_some() {
|
||||||
|
return Err(de::Error::duplicate_field("max_args"));
|
||||||
|
}
|
||||||
|
max_args = Some(map.next_value()?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let (Some(min_args), Some(max_args)) = (min_args, max_args) {
|
||||||
|
if min_args > max_args {
|
||||||
|
{
|
||||||
|
return Err(de::Error::invalid_value(
|
||||||
|
Unexpected::Other(&format!(
|
||||||
|
"argument count range {} to {}",
|
||||||
|
min_args, max_args
|
||||||
|
)),
|
||||||
|
&"a valid argument count range",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let path = path.ok_or_else(|| de::Error::missing_field("path"))?;
|
||||||
|
Ok(Route {
|
||||||
|
route_type: get_route_type(&path),
|
||||||
|
path,
|
||||||
|
hidden: hidden.unwrap_or_default(),
|
||||||
|
description,
|
||||||
|
min_args,
|
||||||
|
max_args,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deserializer.deserialize_any(RouteVisitor)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Route {
|
impl std::fmt::Display for Route {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self {
|
Self {
|
||||||
route_type: RouteType::External,
|
route_type: RouteType::External,
|
||||||
path,
|
path,
|
||||||
..
|
..
|
||||||
} => write!(f, "raw ({path})"),
|
} => write!(f, "raw ({})", path),
|
||||||
Self {
|
Self {
|
||||||
route_type: RouteType::Internal,
|
route_type: RouteType::Internal,
|
||||||
path,
|
path,
|
||||||
..
|
..
|
||||||
} => write!(f, "file ({path})"),
|
} => write!(f, "file ({})", path),
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Classifies the path depending on if the there exists a local file.
|
/// Classifies the path depending on if the there exists a local file.
|
||||||
fn get_route_type(path: &str) -> RouteType {
|
fn get_route_type(path: &str) -> RouteType {
|
||||||
if std::path::Path::new(path).exists() {
|
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
|
RouteType::Internal
|
||||||
} else {
|
} else {
|
||||||
debug!("{path} does not exist on disk, assuming web path.");
|
debug!("{} does not exist on disk, assuming web path.", path);
|
||||||
RouteType::External
|
RouteType::External
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// There exists two route types: an external path (e.g. a URL) or an internal
|
/// There exists two route types: an external path (e.g. a URL) or an internal
|
||||||
/// path (to a file).
|
/// path (to a file).
|
||||||
#[derive(Debug, PartialEq, Clone, Serialize)]
|
#[derive(Debug, PartialEq, Clone, Serialize)]
|
||||||
pub enum RouteType {
|
pub enum RouteType {
|
||||||
External,
|
External,
|
||||||
Internal,
|
Internal,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FileData {
|
pub struct ConfigData {
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
pub file: File,
|
pub file: File,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// If a provided config path isn't found, this function checks known good
|
/// If a provided config path isn't found, this function checks known good
|
||||||
/// locations for a place to write a config file to. In order, it checks the
|
/// 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
|
/// system-wide config location (`/etc/`, in Linux), followed by the config
|
||||||
/// folder, followed by the user's home folder.
|
/// 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
|
// Locations to check, with highest priority first
|
||||||
let locations: Vec<_> = {
|
let locations: Vec<_> = {
|
||||||
let mut folders = vec![PathBuf::from("/etc/")];
|
let mut folders = vec![PathBuf::from("/etc/")];
|
||||||
|
|
||||||
// Config folder
|
// Config folder
|
||||||
if let Some(folder) = config_dir() {
|
if let Some(folder) = config_dir() {
|
||||||
folders.push(folder);
|
folders.push(folder)
|
||||||
}
|
|
||||||
|
|
||||||
// Home folder
|
|
||||||
if let Some(folder) = home_dir() {
|
|
||||||
folders.push(folder);
|
|
||||||
}
|
|
||||||
|
|
||||||
folders
|
|
||||||
.iter_mut()
|
|
||||||
.for_each(|folder| folder.push(CONFIG_FILENAME));
|
|
||||||
|
|
||||||
folders
|
|
||||||
};
|
|
||||||
|
|
||||||
debug!("Checking locations for config file: {:?}", &locations);
|
|
||||||
|
|
||||||
for location in &locations {
|
|
||||||
let file = OpenOptions::new().read(true).open(location);
|
|
||||||
match file {
|
|
||||||
Ok(file) => {
|
|
||||||
debug!("Found file at {location:?}.");
|
|
||||||
return Ok(FileData {
|
|
||||||
path: location.clone(),
|
|
||||||
file,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
debug!("Tried to read '{location:?}' but failed due to error: {e}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!("Failed to find any config. Now trying to find first writable path");
|
// Home folder
|
||||||
|
if let Some(folder) = home_dir() {
|
||||||
// If we got here, we failed to read any file paths, meaning no config exists
|
folders.push(folder)
|
||||||
// yet. In that case, try to return the first location that we can write to,
|
|
||||||
// after writing the default config
|
|
||||||
for location in locations {
|
|
||||||
let file = OpenOptions::new()
|
|
||||||
.write(true)
|
|
||||||
.create_new(true)
|
|
||||||
.open(location.clone());
|
|
||||||
match file {
|
|
||||||
Ok(mut file) => {
|
|
||||||
info!("Creating new config file at {location:?}.");
|
|
||||||
file.write_all(DEFAULT_CONFIG)?;
|
|
||||||
|
|
||||||
let file = OpenOptions::new().read(true).open(location.clone())?;
|
|
||||||
return Ok(FileData {
|
|
||||||
path: location,
|
|
||||||
file,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
debug!("Tried to open a new file at '{location:?}' but failed due to error: {e}",)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(BunBunError::NoValidConfigPath)
|
folders
|
||||||
|
.iter_mut()
|
||||||
|
.for_each(|folder| folder.push(CONFIG_FILENAME));
|
||||||
|
|
||||||
|
folders
|
||||||
|
};
|
||||||
|
|
||||||
|
debug!("Checking locations for config file: {:?}", &locations);
|
||||||
|
|
||||||
|
for location in &locations {
|
||||||
|
let file = OpenOptions::new().read(true).open(location.clone());
|
||||||
|
match file {
|
||||||
|
Ok(file) => {
|
||||||
|
debug!("Found file at {:?}.", location);
|
||||||
|
return Ok(ConfigData {
|
||||||
|
path: location.clone(),
|
||||||
|
file,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Err(e) => debug!(
|
||||||
|
"Tried to read '{:?}' but failed due to error: {}",
|
||||||
|
location, e
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("Failed to find any config. Now trying to find first writable path");
|
||||||
|
|
||||||
|
// If we got here, we failed to read any file paths, meaning no config exists
|
||||||
|
// yet. In that case, try to return the first location that we can write to,
|
||||||
|
// after writing the default config
|
||||||
|
for location in locations {
|
||||||
|
let file = OpenOptions::new()
|
||||||
|
.write(true)
|
||||||
|
.create_new(true)
|
||||||
|
.open(location.clone());
|
||||||
|
match file {
|
||||||
|
Ok(mut file) => {
|
||||||
|
info!("Creating new config file at {:?}.", location);
|
||||||
|
file.write_all(DEFAULT_CONFIG)?;
|
||||||
|
|
||||||
|
let file = OpenOptions::new().read(true).open(location.clone())?;
|
||||||
|
return Ok(ConfigData {
|
||||||
|
path: location,
|
||||||
|
file,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Err(e) => debug!(
|
||||||
|
"Tried to open a new file at '{:?}' but failed due to error: {}",
|
||||||
|
location, e
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(BunBunError::NoValidConfigPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Assumes that the user knows what they're talking about and will only try
|
/// Assumes that the user knows what they're talking about and will only try
|
||||||
/// to load the config at the given path.
|
/// 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(
|
||||||
let path = path.into();
|
path: impl Into<PathBuf>,
|
||||||
let file = OpenOptions::new()
|
) -> Result<ConfigData, BunBunError> {
|
||||||
.read(true)
|
let path = path.into();
|
||||||
.open(&path)
|
let file = OpenOptions::new()
|
||||||
.map_err(|e| BunBunError::InvalidConfigPath(path.clone(), e))?;
|
.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(
|
||||||
trace!("Loading config file.");
|
mut config_file: File,
|
||||||
let file_size = config_file.metadata()?.len();
|
large_config: bool,
|
||||||
|
) -> Result<Config, BunBunError> {
|
||||||
|
trace!("Loading config file.");
|
||||||
|
let file_size = config_file.metadata()?.len();
|
||||||
|
|
||||||
// 100 MB
|
// 100 MB
|
||||||
if file_size > LARGE_FILE_SIZE_THRESHOLD && !large_config {
|
if file_size > LARGE_FILE_SIZE_THRESHOLD && !large_config {
|
||||||
return Err(BunBunError::ConfigTooLarge(file_size));
|
return Err(BunBunError::ConfigTooLarge(file_size));
|
||||||
}
|
}
|
||||||
|
|
||||||
if file_size == 0 {
|
if file_size == 0 {
|
||||||
return Err(BunBunError::ZeroByteConfig);
|
return Err(BunBunError::ZeroByteConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut config_data = String::new();
|
let mut config_data = String::new();
|
||||||
config_file.read_to_string(&mut config_data)?;
|
config_file.read_to_string(&mut config_data)?;
|
||||||
// Reading from memory is faster than reading directly from a reader for some
|
// Reading from memory is faster than reading directly from a reader for some
|
||||||
// reason; see https://github.com/serde-rs/json/issues/160
|
// reason; see https://github.com/serde-rs/json/issues/160
|
||||||
Ok(serde_yaml::from_str(&config_data)?)
|
Ok(serde_yaml::from_str(&config_data)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod route {
|
mod route {
|
||||||
use super::*;
|
use super::*;
|
||||||
use anyhow::{Context, Result};
|
use serde_yaml::{from_str, to_string};
|
||||||
use serde_yaml::{from_str, to_string};
|
use tempfile::NamedTempFile;
|
||||||
use std::path::Path;
|
|
||||||
use tempfile::NamedTempFile;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deserialize_relative_path() -> Result<()> {
|
fn deserialize_relative_path() {
|
||||||
let tmpfile = NamedTempFile::new_in(".")?;
|
let tmpfile = NamedTempFile::new_in(".").unwrap();
|
||||||
let path = tmpfile.path().display().to_string();
|
let path = format!("{}", tmpfile.path().display());
|
||||||
let path = path
|
let path = path.get(path.rfind(".").unwrap()..).unwrap();
|
||||||
.get(path.rfind(".").context("While finding .")?..)
|
let path = std::path::Path::new(path);
|
||||||
.context("While getting the path")?;
|
assert!(path.is_relative());
|
||||||
let path = Path::new(path);
|
let path = path.to_str().unwrap();
|
||||||
assert!(path.is_relative());
|
assert_eq!(
|
||||||
let path = path.to_str().context("While stringifying path")?;
|
from_str::<Route>(path).unwrap(),
|
||||||
assert_eq!(from_str::<Route>(path)?, Route::from(path.to_owned()));
|
Route::from_str(path).unwrap()
|
||||||
Ok(())
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deserialize_absolute_path() -> Result<()> {
|
fn deserialize_absolute_path() {
|
||||||
let tmpfile = NamedTempFile::new()?;
|
let tmpfile = NamedTempFile::new().unwrap();
|
||||||
let path = format!("{}", tmpfile.path().display());
|
let path = format!("{}", tmpfile.path().display());
|
||||||
assert!(tmpfile.path().is_absolute());
|
assert!(tmpfile.path().is_absolute());
|
||||||
assert_eq!(from_str::<Route>(&path)?, Route::from(path));
|
assert_eq!(
|
||||||
|
from_str::<Route>(&path).unwrap(),
|
||||||
|
Route::from_str(&path).unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
#[test]
|
||||||
}
|
fn deserialize_http_path() {
|
||||||
|
assert_eq!(
|
||||||
|
from_str::<Route>("http://google.com").unwrap(),
|
||||||
|
Route::from_str("http://google.com").unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deserialize_http_path() -> Result<()> {
|
fn deserialize_https_path() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
from_str::<Route>("http://google.com")?,
|
from_str::<Route>("https://google.com").unwrap(),
|
||||||
Route::from("http://google.com")
|
Route::from_str("https://google.com").unwrap()
|
||||||
);
|
);
|
||||||
Ok(())
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deserialize_https_path() -> Result<()> {
|
fn serialize() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
from_str::<Route>("https://google.com")?,
|
&to_string(&Route::from_str("hello world").unwrap()).unwrap(),
|
||||||
Route::from("https://google.com")
|
"---\nroute_type: External\npath: hello world\nhidden: false\ndescription: ~\nmin_args: ~\nmax_args: ~\n"
|
||||||
);
|
);
|
||||||
Ok(())
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn serialize() -> Result<()> {
|
|
||||||
assert_eq!(
|
|
||||||
&to_string(&Route::from("hello world"))?,
|
|
||||||
"---\nroute_type: External\npath: hello world\nhidden: false\ndescription: ~\nmin_args: ~\nmax_args: ~\n"
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod read_config {
|
mod read_config {
|
||||||
use super::*;
|
use super::*;
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_file() -> Result<()> {
|
fn empty_file() {
|
||||||
let config_file = tempfile::tempfile()?;
|
let config_file = tempfile::tempfile().unwrap();
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
load_file(config_file, false),
|
read_config(config_file, false),
|
||||||
Err(BunBunError::ZeroByteConfig)
|
Err(BunBunError::ZeroByteConfig)
|
||||||
));
|
));
|
||||||
Ok(())
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn config_too_large() -> Result<()> {
|
fn config_too_large() {
|
||||||
let mut config_file = tempfile::tempfile()?;
|
let mut config_file = tempfile::tempfile().unwrap();
|
||||||
let size_to_write = (LARGE_FILE_SIZE_THRESHOLD + 1) as usize;
|
let size_to_write = (LARGE_FILE_SIZE_THRESHOLD + 1) as usize;
|
||||||
config_file.write(&[0].repeat(size_to_write))?;
|
config_file.write(&[0].repeat(size_to_write)).unwrap();
|
||||||
match load_file(config_file, false) {
|
match read_config(config_file, false) {
|
||||||
Err(BunBunError::ConfigTooLarge(size)) if size as usize == size_to_write => {}
|
Err(BunBunError::ConfigTooLarge(size))
|
||||||
Err(BunBunError::ConfigTooLarge(size)) => {
|
if size as usize == size_to_write => {}
|
||||||
panic!("Mismatched size: {size} != {size_to_write}")
|
Err(BunBunError::ConfigTooLarge(size)) => {
|
||||||
}
|
panic!("Mismatched size: {} != {}", size, size_to_write)
|
||||||
res => panic!("Wrong result, got {res:#?}"),
|
}
|
||||||
}
|
res => panic!("Wrong result, got {:#?}", res),
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn valid_config() -> Result<()> {
|
fn valid_config() {
|
||||||
assert!(load_file(File::open("bunbun.default.yaml")?, false).is_ok());
|
let config_file = File::open("bunbun.default.yaml").unwrap();
|
||||||
Ok(())
|
assert!(read_config(config_file, false).is_ok());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
64
src/error.rs
64
src/error.rs
|
@ -2,52 +2,54 @@ use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[allow(clippy::module_name_repetitions)]
|
|
||||||
pub enum BunBunError {
|
pub enum BunBunError {
|
||||||
Io(std::io::Error),
|
Io(std::io::Error),
|
||||||
Parse(serde_yaml::Error),
|
Parse(serde_yaml::Error),
|
||||||
Watch(hotwatch::Error),
|
Watch(hotwatch::Error),
|
||||||
CustomProgram(String),
|
LoggerInit(log::SetLoggerError),
|
||||||
NoValidConfigPath,
|
CustomProgram(String),
|
||||||
InvalidConfigPath(std::path::PathBuf, std::io::Error),
|
NoValidConfigPath,
|
||||||
ConfigTooLarge(u64),
|
InvalidConfigPath(std::path::PathBuf, std::io::Error),
|
||||||
ZeroByteConfig,
|
ConfigTooLarge(u64),
|
||||||
JsonParse(serde_json::Error),
|
ZeroByteConfig,
|
||||||
|
JsonParse(serde_json::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for BunBunError {}
|
impl Error for BunBunError {}
|
||||||
|
|
||||||
impl fmt::Display for BunBunError {
|
impl fmt::Display for BunBunError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Io(e) => e.fmt(f),
|
Self::Io(e) => e.fmt(f),
|
||||||
Self::Parse(e) => e.fmt(f),
|
Self::Parse(e) => e.fmt(f),
|
||||||
Self::Watch(e) => e.fmt(f),
|
Self::Watch(e) => e.fmt(f),
|
||||||
Self::CustomProgram(msg) => msg.fmt(f),
|
Self::LoggerInit(e) => e.fmt(f),
|
||||||
Self::NoValidConfigPath => write!(f, "No valid config path was found!"),
|
Self::CustomProgram(msg) => write!(f, "{}", msg),
|
||||||
Self::InvalidConfigPath(path, reason) => {
|
Self::NoValidConfigPath => write!(f, "No valid config path was found!"),
|
||||||
write!(f, "Failed to access {path:?}: {reason}")
|
Self::InvalidConfigPath(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::ZeroByteConfig => write!(f, "The config provided reported a size of 0 bytes. Please check your config path!"),
|
Self::ConfigTooLarge(size) => write!(f, "The config file was too large ({} bytes)! Pass in --large-config to bypass this check.", size),
|
||||||
Self::JsonParse(e) => e.fmt(f),
|
Self::ZeroByteConfig => write!(f, "The config provided reported a size of 0 bytes. Please check your config path!"),
|
||||||
}
|
Self::JsonParse(e) => e.fmt(f),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a from implementation from the specified type to the provided
|
/// Generates a from implementation from the specified type to the provided
|
||||||
/// bunbun error.
|
/// bunbun error.
|
||||||
macro_rules! from_error {
|
macro_rules! from_error {
|
||||||
($from:ty, $to:ident) => {
|
($from:ty, $to:ident) => {
|
||||||
impl From<$from> for BunBunError {
|
impl From<$from> for BunBunError {
|
||||||
fn from(e: $from) -> Self {
|
fn from(e: $from) -> Self {
|
||||||
Self::$to(e)
|
Self::$to(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
from_error!(std::io::Error, Io);
|
from_error!(std::io::Error, Io);
|
||||||
from_error!(serde_yaml::Error, Parse);
|
from_error!(serde_yaml::Error, Parse);
|
||||||
from_error!(hotwatch::Error, Watch);
|
from_error!(hotwatch::Error, Watch);
|
||||||
|
from_error!(log::SetLoggerError, LoggerInit);
|
||||||
from_error!(serde_json::Error, JsonParse);
|
from_error!(serde_json::Error, JsonParse);
|
||||||
|
|
448
src/main.rs
448
src/main.rs
|
@ -1,26 +1,25 @@
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
#![deny(missing_docs)]
|
#![deny(missing_docs)]
|
||||||
#![warn(clippy::nursery, clippy::pedantic)]
|
|
||||||
|
|
||||||
//! Bunbun is a pure-Rust implementation of bunny1 that provides a customizable
|
//! 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
|
//! search engine and quick-jump tool in one small binary. For information on
|
||||||
//! usage, please take a look at the readme.
|
//! usage, please take a look at the readme.
|
||||||
|
|
||||||
use crate::config::{get_config_data, load_custom_file, load_file, FileData, Route, RouteGroup};
|
use crate::config::{
|
||||||
use anyhow::Result;
|
get_config_data, load_custom_path_config, read_config, ConfigData, Route,
|
||||||
use arc_swap::ArcSwap;
|
RouteGroup,
|
||||||
use axum::routing::get;
|
};
|
||||||
use axum::{Extension, Router};
|
use actix_web::{middleware::Logger, App, HttpServer};
|
||||||
use clap::Parser;
|
use clap::Clap;
|
||||||
use error::BunBunError;
|
use error::BunBunError;
|
||||||
use handlebars::Handlebars;
|
use handlebars::{Handlebars, TemplateError};
|
||||||
use hotwatch::{Event, Hotwatch};
|
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::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, RwLock};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tracing::{debug, info, trace, warn};
|
|
||||||
use tracing_subscriber::layer::SubscriberExt;
|
|
||||||
use tracing_subscriber::util::SubscriberInitExt;
|
|
||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
mod config;
|
mod config;
|
||||||
|
@ -33,93 +32,123 @@ mod template_args;
|
||||||
/// Dynamic variables that either need to be present at runtime, or can be
|
/// Dynamic variables that either need to be present at runtime, or can be
|
||||||
/// changed during runtime.
|
/// changed during runtime.
|
||||||
pub struct State {
|
pub struct State {
|
||||||
public_address: String,
|
public_address: String,
|
||||||
default_route: Option<String>,
|
default_route: Option<String>,
|
||||||
groups: Vec<RouteGroup>,
|
groups: Vec<RouteGroup>,
|
||||||
/// Cached, flattened mapping of all routes and their destinations.
|
/// Cached, flattened mapping of all routes and their destinations.
|
||||||
routes: HashMap<String, Route>,
|
routes: HashMap<String, Route>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[actix_web::main]
|
||||||
#[cfg(not(tarpaulin_include))]
|
#[cfg(not(tarpaulin_include))]
|
||||||
async fn main() -> Result<()> {
|
async fn main() {
|
||||||
use tracing_subscriber::EnvFilter;
|
std::process::exit(match run().await {
|
||||||
|
Ok(_) => 0,
|
||||||
let opts = cli::Opts::parse();
|
Err(e) => {
|
||||||
|
error!("{}", e);
|
||||||
let mut env_filter = EnvFilter::from_default_env();
|
1
|
||||||
for directive in opts.log {
|
|
||||||
env_filter = env_filter.add_directive(directive);
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
tracing_subscriber::registry()
|
#[cfg(not(tarpaulin_include))]
|
||||||
.with(tracing_subscriber::fmt::layer())
|
async fn run() -> Result<(), BunBunError> {
|
||||||
.with(env_filter)
|
let opts = cli::Opts::parse();
|
||||||
.init();
|
|
||||||
|
|
||||||
let conf_data = match opts.config {
|
init_logger(opts.verbose, opts.quiet)?;
|
||||||
Some(file_name) => load_custom_file(file_name),
|
|
||||||
None => get_config_data(),
|
|
||||||
}?;
|
|
||||||
|
|
||||||
let conf = load_file(conf_data.file.try_clone()?, opts.large_config)?;
|
let conf_data = match opts.config {
|
||||||
let state = Arc::from(ArcSwap::from_pointee(State {
|
Some(file_name) => load_custom_path_config(file_name),
|
||||||
public_address: conf.public_address,
|
None => get_config_data(),
|
||||||
default_route: conf.default_route,
|
}?;
|
||||||
routes: cache_routes(conf.groups.clone()),
|
|
||||||
groups: conf.groups,
|
|
||||||
}));
|
|
||||||
|
|
||||||
// Cannot be named _ or Rust will immediately drop it.
|
let conf = read_config(conf_data.file.try_clone()?, opts.large_config)?;
|
||||||
let _watch = start_watch(Arc::clone(&state), conf_data, 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),
|
||||||
|
groups: conf.groups,
|
||||||
|
}));
|
||||||
|
|
||||||
let app = Router::new()
|
// Cannot be named _ or Rust will immediately drop it.
|
||||||
.route("/", get(routes::index))
|
let _watch = start_watch(Arc::clone(&state), conf_data, opts.large_config)?;
|
||||||
.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()?;
|
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?;
|
||||||
|
|
||||||
info!("Starting server at {bind_addr}");
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
axum::Server::bind(&bind_addr)
|
/// Initializes the logger based on the number of quiet and verbose flags passed
|
||||||
.serve(app.into_make_service())
|
/// in. Usually, these values are mutually exclusive, that is, if the number of
|
||||||
.await?;
|
/// 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]
|
||||||
|
};
|
||||||
|
|
||||||
Ok(())
|
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
|
/// 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
|
/// file. This should improve runtime performance and is a better solution than
|
||||||
/// just iterating over the config object for every hop resolution.
|
/// 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();
|
let mut mapping = HashMap::new();
|
||||||
for group in groups {
|
for group in groups {
|
||||||
for (kw, dest) in group.routes {
|
for (kw, dest) in &group.routes {
|
||||||
// This function isn't called often enough to not be a performance issue.
|
match mapping.insert(kw.clone(), dest.clone()) {
|
||||||
match mapping.insert(kw.clone(), dest.clone()) {
|
None => trace!("Inserting {} into mapping.", kw),
|
||||||
None => trace!("Inserting {kw} into mapping."),
|
Some(old_value) => {
|
||||||
Some(old_value) => {
|
trace!("Overriding {} route from {} to {}.", kw, old_value, dest)
|
||||||
trace!("Overriding {kw} route from {old_value} to {dest}.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
mapping
|
}
|
||||||
|
mapping
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an instance with all pre-generated templates included into the
|
/// Returns an instance with all pre-generated templates included into the
|
||||||
/// binary. This allows for users to have a portable binary without needed the
|
/// binary. This allows for users to have a portable binary without needed the
|
||||||
/// templates at runtime.
|
/// templates at runtime.
|
||||||
fn compile_templates() -> Result<Handlebars<'static>> {
|
fn compile_templates() -> Result<Handlebars<'static>, TemplateError> {
|
||||||
let mut handlebars = Handlebars::new();
|
let mut handlebars = Handlebars::new();
|
||||||
handlebars.set_strict_mode(true);
|
handlebars.set_strict_mode(true);
|
||||||
handlebars.register_partial("bunbun_version", env!("CARGO_PKG_VERSION"))?;
|
handlebars.register_partial("bunbun_version", env!("CARGO_PKG_VERSION"))?;
|
||||||
handlebars.register_partial("bunbun_src", env!("CARGO_PKG_REPOSITORY"))?;
|
handlebars.register_partial("bunbun_src", env!("CARGO_PKG_REPOSITORY"))?;
|
||||||
macro_rules! register_template {
|
macro_rules! register_template {
|
||||||
[ $( $template:expr ),* ] => {
|
[ $( $template:expr ),* ] => {
|
||||||
$(
|
$(
|
||||||
handlebars
|
handlebars
|
||||||
|
@ -132,8 +161,8 @@ fn compile_templates() -> Result<Handlebars<'static>> {
|
||||||
)*
|
)*
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
register_template!["index", "list", "opensearch"];
|
register_template!["index", "list", "opensearch"];
|
||||||
Ok(handlebars)
|
Ok(handlebars)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Starts the watch on a file, if possible. This will only return an Error if
|
/// Starts the watch on a file, if possible. This will only return an Error if
|
||||||
|
@ -147,137 +176,180 @@ fn compile_templates() -> Result<Handlebars<'static>> {
|
||||||
/// watches.
|
/// watches.
|
||||||
#[cfg(not(tarpaulin_include))]
|
#[cfg(not(tarpaulin_include))]
|
||||||
fn start_watch(
|
fn start_watch(
|
||||||
state: Arc<ArcSwap<State>>,
|
state: Arc<RwLock<State>>,
|
||||||
config_data: FileData,
|
config_data: ConfigData,
|
||||||
large_config: bool,
|
large_config: bool,
|
||||||
) -> Result<Hotwatch> {
|
) -> Result<Hotwatch, BunBunError> {
|
||||||
let mut watch = Hotwatch::new_with_custom_delay(Duration::from_millis(500))?;
|
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| {
|
let watch_result = watch.watch(&path, move |e: Event| {
|
||||||
if let Event::Create(ref path) = e {
|
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)
|
||||||
trace!("Getting new file handler as file was recreated.");
|
.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...");
|
|
||||||
trace!("Obtained writer lock on state!");
|
|
||||||
match load_file(
|
|
||||||
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,
|
|
||||||
}));
|
|
||||||
info!("Successfully updated active state");
|
|
||||||
}
|
|
||||||
Err(e) => warn!("Failed to update config file: {e}"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => debug!("Saw event {e:#?} but ignored it"),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
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(watch)
|
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 read_config(
|
||||||
|
file.try_clone().expect("Failed to clone file handle"),
|
||||||
|
large_config,
|
||||||
|
) {
|
||||||
|
Ok(conf) => {
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => debug!("Saw event {:#?} but ignored it", e),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
match watch_result {
|
||||||
|
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)]
|
#[cfg(test)]
|
||||||
mod cache_routes {
|
mod cache_routes {
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
fn generate_external_routes(routes: &[(&'static str, &'static str)]) -> HashMap<String, Route> {
|
fn generate_external_routes(
|
||||||
HashMap::from_iter(
|
routes: &[(&str, &str)],
|
||||||
routes
|
) -> HashMap<String, Route> {
|
||||||
.into_iter()
|
HashMap::from_iter(
|
||||||
.map(|(key, value)| ((*key).to_owned(), Route::from(*value))),
|
routes
|
||||||
)
|
.into_iter()
|
||||||
}
|
.map(|kv| (kv.0.into(), Route::from_str(kv.1).unwrap())),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_groups_yield_empty_routes() {
|
fn empty_groups_yield_empty_routes() {
|
||||||
assert_eq!(cache_routes(Vec::new()), HashMap::new());
|
assert_eq!(cache_routes(&[]), HashMap::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn disjoint_groups_yield_summed_routes() {
|
fn disjoint_groups_yield_summed_routes() {
|
||||||
let group1 = RouteGroup {
|
let group1 = RouteGroup {
|
||||||
name: String::from("x"),
|
name: String::from("x"),
|
||||||
description: Some(String::from("y")),
|
description: Some(String::from("y")),
|
||||||
routes: generate_external_routes(&[("a", "b"), ("c", "d")]),
|
routes: generate_external_routes(&[("a", "b"), ("c", "d")]),
|
||||||
hidden: false,
|
hidden: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let group2 = RouteGroup {
|
let group2 = RouteGroup {
|
||||||
name: String::from("5"),
|
name: String::from("5"),
|
||||||
description: Some(String::from("6")),
|
description: Some(String::from("6")),
|
||||||
routes: generate_external_routes(&[("1", "2"), ("3", "4")]),
|
routes: generate_external_routes(&[("1", "2"), ("3", "4")]),
|
||||||
hidden: false,
|
hidden: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
cache_routes(vec![group1, group2]),
|
cache_routes(&[group1, group2]),
|
||||||
generate_external_routes(&[("a", "b"), ("c", "d"), ("1", "2"), ("3", "4")])
|
generate_external_routes(&[
|
||||||
);
|
("a", "b"),
|
||||||
}
|
("c", "d"),
|
||||||
|
("1", "2"),
|
||||||
|
("3", "4")
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn overlapping_groups_use_latter_routes() {
|
fn overlapping_groups_use_latter_routes() {
|
||||||
let group1 = RouteGroup {
|
let group1 = RouteGroup {
|
||||||
name: String::from("x"),
|
name: String::from("x"),
|
||||||
description: Some(String::from("y")),
|
description: Some(String::from("y")),
|
||||||
routes: generate_external_routes(&[("a", "b"), ("c", "d")]),
|
routes: generate_external_routes(&[("a", "b"), ("c", "d")]),
|
||||||
hidden: false,
|
hidden: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let group2 = RouteGroup {
|
let group2 = RouteGroup {
|
||||||
name: String::from("5"),
|
name: String::from("5"),
|
||||||
description: Some(String::from("6")),
|
description: Some(String::from("6")),
|
||||||
routes: generate_external_routes(&[("a", "1"), ("c", "2")]),
|
routes: generate_external_routes(&[("a", "1"), ("c", "2")]),
|
||||||
hidden: false,
|
hidden: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
cache_routes(vec![group1.clone(), group2]),
|
cache_routes(&[group1.clone(), group2]),
|
||||||
generate_external_routes(&[("a", "1"), ("c", "2")])
|
generate_external_routes(&[("a", "1"), ("c", "2")])
|
||||||
);
|
);
|
||||||
|
|
||||||
let group3 = RouteGroup {
|
let group3 = RouteGroup {
|
||||||
name: String::from("5"),
|
name: String::from("5"),
|
||||||
description: Some(String::from("6")),
|
description: Some(String::from("6")),
|
||||||
routes: generate_external_routes(&[("a", "1"), ("b", "2")]),
|
routes: generate_external_routes(&[("a", "1"), ("b", "2")]),
|
||||||
hidden: false,
|
hidden: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
cache_routes(vec![group1, group3]),
|
cache_routes(&[group1, group3]),
|
||||||
generate_external_routes(&[("a", "1"), ("b", "2"), ("c", "d")])
|
generate_external_routes(&[("a", "1"), ("b", "2"), ("c", "d")])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod compile_templates {
|
mod compile_templates {
|
||||||
use super::compile_templates;
|
use super::compile_templates;
|
||||||
|
|
||||||
/// Successful compilation of the binary guarantees that the templates will be
|
/// Successful compilation of the binary guarantees that the templates will be
|
||||||
/// present to be registered to. Thus, we only really need to see that
|
/// present to be registered to. Thus, we only really need to see that
|
||||||
/// compilation of the templates don't panic, which is just making sure that
|
/// compilation of the templates don't panic, which is just making sure that
|
||||||
/// the function can be successfully called.
|
/// the function can be successfully called.
|
||||||
#[test]
|
#[test]
|
||||||
fn templates_compile() {
|
fn templates_compile() {
|
||||||
let _ = compile_templates();
|
let _ = compile_templates();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
650
src/routes.rs
650
src/routes.rs
|
@ -1,143 +1,140 @@
|
||||||
use crate::config::{Route as ConfigRoute, RouteType};
|
use crate::config::{Route as ConfigRoute, RouteType};
|
||||||
use crate::{template_args, BunBunError, Route, State};
|
use crate::{template_args, BunBunError, Route, State};
|
||||||
use arc_swap::ArcSwap;
|
use actix_web::web::{Data, Query};
|
||||||
use axum::body::{boxed, Bytes, Full};
|
use actix_web::{get, http::header};
|
||||||
use axum::extract::Query;
|
use actix_web::{HttpRequest, HttpResponse, Responder};
|
||||||
use axum::http::{header, StatusCode};
|
|
||||||
use axum::response::{Html, IntoResponse, Response};
|
|
||||||
use axum::Extension;
|
|
||||||
use handlebars::Handlebars;
|
use handlebars::Handlebars;
|
||||||
|
use log::{debug, error};
|
||||||
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
|
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::Path;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, RwLock};
|
||||||
use tracing::{debug, error};
|
|
||||||
|
|
||||||
// https://url.spec.whatwg.org/#fragment-percent-encode-set
|
/// https://url.spec.whatwg.org/#fragment-percent-encode-set
|
||||||
const FRAGMENT_ENCODE_SET: &AsciiSet = &CONTROLS
|
const FRAGMENT_ENCODE_SET: &AsciiSet = &CONTROLS
|
||||||
.add(b' ')
|
.add(b' ')
|
||||||
.add(b'"')
|
.add(b'"')
|
||||||
.add(b'<')
|
.add(b'<')
|
||||||
.add(b'>')
|
.add(b'>')
|
||||||
.add(b'`')
|
.add(b'`')
|
||||||
.add(b'+')
|
.add(b'+')
|
||||||
.add(b'&') // Interpreted as a GET query
|
.add(b'&') // Interpreted as a GET query
|
||||||
.add(b'#') // Interpreted as a hyperlink section target
|
.add(b'#'); // Interpreted as a hyperlink section target
|
||||||
.add(b'\'');
|
|
||||||
|
|
||||||
#[allow(clippy::unused_async)]
|
type StateData = Data<Arc<RwLock<State>>>;
|
||||||
pub async fn index(
|
|
||||||
Extension(data): Extension<Arc<ArcSwap<State>>>,
|
#[get("/")]
|
||||||
Extension(handlebars): Extension<Handlebars<'static>>,
|
pub async fn index(data: StateData, req: HttpRequest) -> impl Responder {
|
||||||
) -> impl IntoResponse {
|
let data = data.read().unwrap();
|
||||||
handlebars
|
HttpResponse::Ok()
|
||||||
|
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
||||||
|
.body(
|
||||||
|
req
|
||||||
|
.app_data::<Handlebars>()
|
||||||
|
.unwrap()
|
||||||
.render(
|
.render(
|
||||||
"index",
|
"index",
|
||||||
&template_args::hostname(&data.load().public_address),
|
&template_args::hostname(data.public_address.clone()),
|
||||||
)
|
)
|
||||||
.map(Html)
|
.unwrap(),
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::unused_async)]
|
#[get("/bunbunsearch.xml")]
|
||||||
pub async fn opensearch(
|
pub async fn opensearch(data: StateData, req: HttpRequest) -> impl Responder {
|
||||||
Extension(data): Extension<Arc<ArcSwap<State>>>,
|
let data = data.read().unwrap();
|
||||||
Extension(handlebars): Extension<Handlebars<'static>>,
|
HttpResponse::Ok()
|
||||||
) -> impl IntoResponse {
|
.header(
|
||||||
handlebars
|
header::CONTENT_TYPE,
|
||||||
|
"application/opensearchdescription+xml",
|
||||||
|
)
|
||||||
|
.body(
|
||||||
|
req
|
||||||
|
.app_data::<Handlebars>()
|
||||||
|
.unwrap()
|
||||||
.render(
|
.render(
|
||||||
"opensearch",
|
"opensearch",
|
||||||
&template_args::hostname(&data.load().public_address),
|
&template_args::hostname(data.public_address.clone()),
|
||||||
)
|
)
|
||||||
.map(|body| {
|
.unwrap(),
|
||||||
(
|
)
|
||||||
StatusCode::OK,
|
|
||||||
[(
|
|
||||||
header::CONTENT_TYPE,
|
|
||||||
"application/opensearchdescription+xml",
|
|
||||||
)],
|
|
||||||
body,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::unused_async)]
|
#[get("/ls")]
|
||||||
pub async fn list(
|
pub async fn list(data: StateData, req: HttpRequest) -> impl Responder {
|
||||||
Extension(data): Extension<Arc<ArcSwap<State>>>,
|
let data = data.read().unwrap();
|
||||||
Extension(handlebars): Extension<Handlebars<'static>>,
|
HttpResponse::Ok()
|
||||||
) -> impl IntoResponse {
|
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
||||||
handlebars
|
.body(
|
||||||
.render("list", &data.load().groups)
|
req
|
||||||
.map(Html)
|
.app_data::<Handlebars>()
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
.unwrap()
|
||||||
|
.render("list", &data.groups)
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize)]
|
||||||
pub struct SearchQuery {
|
pub struct SearchQuery {
|
||||||
to: String,
|
to: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::unused_async)]
|
#[get("/hop")]
|
||||||
pub async fn hop(
|
pub async fn hop(
|
||||||
Extension(data): Extension<Arc<ArcSwap<State>>>,
|
data: StateData,
|
||||||
Extension(handlebars): Extension<Handlebars<'static>>,
|
req: HttpRequest,
|
||||||
Query(query): Query<SearchQuery>,
|
query: Query<SearchQuery>,
|
||||||
) -> impl IntoResponse {
|
) -> impl Responder {
|
||||||
let data = data.load();
|
let data = data.read().unwrap();
|
||||||
|
|
||||||
match resolve_hop(&query.to, &data.routes, &data.default_route) {
|
match resolve_hop(&query.to, &data.routes, &data.default_route) {
|
||||||
RouteResolution::Resolved { route: path, args } => {
|
RouteResolution::Resolved { route: path, args } => {
|
||||||
let resolved_template = match path {
|
let resolved_template = match path {
|
||||||
ConfigRoute {
|
ConfigRoute {
|
||||||
route_type: RouteType::Internal,
|
route_type: RouteType::Internal,
|
||||||
path,
|
path,
|
||||||
..
|
..
|
||||||
} => resolve_path(Path::new(path), &args),
|
} => resolve_path(PathBuf::from(path), &args),
|
||||||
ConfigRoute {
|
ConfigRoute {
|
||||||
route_type: RouteType::External,
|
route_type: RouteType::External,
|
||||||
path,
|
path,
|
||||||
..
|
..
|
||||||
} => Ok(HopAction::Redirect(path.clone())),
|
} => Ok(HopAction::Redirect(path.clone())),
|
||||||
};
|
};
|
||||||
|
|
||||||
match resolved_template {
|
match resolved_template {
|
||||||
Ok(HopAction::Redirect(path)) => {
|
Ok(HopAction::Redirect(path)) => HttpResponse::Found()
|
||||||
let rendered = handlebars
|
.header(
|
||||||
.render_template(
|
header::LOCATION,
|
||||||
&path,
|
req
|
||||||
&template_args::query(utf8_percent_encode(&args, FRAGMENT_ENCODE_SET)),
|
.app_data::<Handlebars>()
|
||||||
)
|
.unwrap()
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
.render_template(
|
||||||
Response::builder()
|
std::str::from_utf8(path.as_bytes()).unwrap(),
|
||||||
.status(StatusCode::FOUND)
|
&template_args::query(
|
||||||
.header(header::LOCATION, &path)
|
utf8_percent_encode(&args, FRAGMENT_ENCODE_SET).to_string(),
|
||||||
.body(boxed(Full::from(rendered)))
|
),
|
||||||
}
|
)
|
||||||
Ok(HopAction::Body(body)) => Response::builder()
|
.unwrap(),
|
||||||
.status(StatusCode::OK)
|
)
|
||||||
.body(boxed(Full::new(Bytes::from(body)))),
|
.finish(),
|
||||||
Err(e) => {
|
Ok(HopAction::Body(body)) => HttpResponse::Ok().body(body),
|
||||||
error!("Failed to redirect user for {path}: {e}");
|
Err(e) => {
|
||||||
Response::builder()
|
error!("Failed to redirect user for {}: {}", path, e);
|
||||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
HttpResponse::InternalServerError().body("Something went wrong :(\n")
|
||||||
.body(boxed(Full::from("Something went wrong :(\n")))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
RouteResolution::Unresolved => Response::builder()
|
}
|
||||||
.status(StatusCode::NOT_FOUND)
|
|
||||||
.body(boxed(Full::from("not found\n"))),
|
|
||||||
}
|
}
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
RouteResolution::Unresolved => HttpResponse::NotFound().body("not found"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
enum RouteResolution<'a> {
|
enum RouteResolution<'a> {
|
||||||
Resolved { route: &'a Route, args: String },
|
Resolved { route: &'a Route, args: String },
|
||||||
Unresolved,
|
Unresolved,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempts to resolve the provided string into its route and its arguments.
|
/// Attempts to resolve the provided string into its route and its arguments.
|
||||||
|
@ -147,272 +144,287 @@ enum RouteResolution<'a> {
|
||||||
/// The first element in the tuple describes the route, while the second element
|
/// The first element in the tuple describes the route, while the second element
|
||||||
/// returns the remaining arguments. If none remain, an empty string is given.
|
/// returns the remaining arguments. If none remain, an empty string is given.
|
||||||
fn resolve_hop<'a>(
|
fn resolve_hop<'a>(
|
||||||
query: &str,
|
query: &str,
|
||||||
routes: &'a HashMap<String, Route>,
|
routes: &'a HashMap<String, Route>,
|
||||||
default_route: &Option<String>,
|
default_route: &Option<String>,
|
||||||
) -> RouteResolution<'a> {
|
) -> RouteResolution<'a> {
|
||||||
let mut split_args = query.split_ascii_whitespace().peekable();
|
let mut split_args = query.split_ascii_whitespace().peekable();
|
||||||
let maybe_route = {
|
let maybe_route = {
|
||||||
match split_args.peek() {
|
match split_args.peek() {
|
||||||
Some(command) => routes.get(*command),
|
Some(command) => routes.get(*command),
|
||||||
None => {
|
None => {
|
||||||
debug!("Found empty query, returning no route.");
|
debug!("Found empty query, returning no route.");
|
||||||
return RouteResolution::Unresolved;
|
return RouteResolution::Unresolved;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let args = split_args.collect::<Vec<_>>();
|
|
||||||
let arg_count = args.len();
|
|
||||||
|
|
||||||
// Try resolving with a matched command
|
|
||||||
if let Some(route) = maybe_route {
|
|
||||||
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}");
|
|
||||||
return RouteResolution::Resolved { route, args };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Try resolving with the default route, if it exists
|
let args = split_args.collect::<Vec<_>>();
|
||||||
if let Some(route) = default_route {
|
let arg_count = args.len();
|
||||||
if let Some(route) = routes.get(route) {
|
|
||||||
if check_route(route, arg_count) {
|
// Try resolving with a matched command
|
||||||
let args = args.join(" ");
|
if let Some(route) = maybe_route {
|
||||||
debug!("Using default route {route} with args {args}");
|
let args = if args.is_empty() { &[] } else { &args[1..] }.join(" ");
|
||||||
return RouteResolution::Resolved { route, args };
|
let arg_count = arg_count - 1;
|
||||||
}
|
if check_route(route, arg_count) {
|
||||||
}
|
debug!("Resolved {} with args {}", route, args);
|
||||||
|
return RouteResolution::Resolved { route, args };
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
RouteResolution::Unresolved
|
// Try resolving with the default route, if it exists
|
||||||
|
if let Some(route) = default_route {
|
||||||
|
if let Some(route) = routes.get(route) {
|
||||||
|
if check_route(route, arg_count) {
|
||||||
|
let args = args.join(" ");
|
||||||
|
debug!("Using default route {} with args {}", route, args);
|
||||||
|
return RouteResolution::Resolved { route, args };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RouteResolution::Unresolved
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the user provided string has the correct properties required by
|
/// Checks if the user provided string has the correct properties required by
|
||||||
/// the route to be successfully matched.
|
/// 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 let Some(min_args) = route.min_args {
|
||||||
if arg_count < min_args {
|
if arg_count < min_args {
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(max_args) = route.max_args {
|
if let Some(max_args) = route.max_args {
|
||||||
if arg_count > max_args {
|
if arg_count > max_args {
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, PartialEq, Eq)]
|
#[derive(Deserialize, Debug, PartialEq, Eq)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
enum HopAction {
|
enum HopAction {
|
||||||
Redirect(String),
|
Redirect(String),
|
||||||
Body(String),
|
Body(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runs the executable with the user's input as a single argument. Returns Ok
|
/// Runs the executable with the user's input as a single argument. Returns Ok
|
||||||
/// so long as the executable was successfully executed. Returns an Error if the
|
/// 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 doesn't exist or bunbun did not have permission to read and execute the
|
||||||
/// file.
|
/// 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()?)
|
let output = Command::new(path.canonicalize()?)
|
||||||
.args(args.split(' '))
|
.args(args.split(' '))
|
||||||
.output()?;
|
.output()?;
|
||||||
|
|
||||||
if output.status.success() {
|
if output.status.success() {
|
||||||
Ok(serde_json::from_slice(&output.stdout[..])?)
|
Ok(serde_json::from_slice(&output.stdout[..])?)
|
||||||
} else {
|
} else {
|
||||||
error!(
|
error!(
|
||||||
"Program exit code for {} was not 0! Dumping standard error!",
|
"Program exit code for {} was not 0! Dumping standard error!",
|
||||||
path.display(),
|
path.display(),
|
||||||
);
|
);
|
||||||
let error = String::from_utf8_lossy(&output.stderr);
|
let error = String::from_utf8_lossy(&output.stderr);
|
||||||
Err(BunBunError::CustomProgram(error.to_string()))
|
Err(BunBunError::CustomProgram(error.to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod resolve_hop {
|
mod resolve_hop {
|
||||||
use super::*;
|
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>(
|
||||||
RouteResolution::Resolved {
|
keyword: &'a Route,
|
||||||
route: keyword,
|
args: &str,
|
||||||
args: String::from(args),
|
) -> RouteResolution<'a> {
|
||||||
}
|
RouteResolution::Resolved {
|
||||||
|
route: keyword,
|
||||||
|
args: String::from(args),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_routes_no_default_yields_failed_hop() {
|
fn empty_routes_no_default_yields_failed_hop() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resolve_hop("hello world", &HashMap::new(), &None),
|
resolve_hop("hello world", &HashMap::new(), &None),
|
||||||
RouteResolution::Unresolved
|
RouteResolution::Unresolved
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_routes_some_default_yields_failed_hop() {
|
fn empty_routes_some_default_yields_failed_hop() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resolve_hop(
|
resolve_hop(
|
||||||
"hello world",
|
"hello world",
|
||||||
&HashMap::new(),
|
&HashMap::new(),
|
||||||
&Some(String::from("google"))
|
&Some(String::from("google"))
|
||||||
),
|
),
|
||||||
RouteResolution::Unresolved
|
RouteResolution::Unresolved
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[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();
|
let mut map: HashMap<String, Route> = HashMap::new();
|
||||||
map.insert("google".into(), Route::from("https://example.com"));
|
map.insert(
|
||||||
assert_eq!(
|
"google".into(),
|
||||||
resolve_hop("hello world", &map, &Some(String::from("google"))),
|
Route::from_str("https://example.com").unwrap(),
|
||||||
generate_route_result(&Route::from("https://example.com"), "hello world"),
|
);
|
||||||
);
|
assert_eq!(
|
||||||
Ok(())
|
resolve_hop("hello world", &map, &Some(String::from("google"))),
|
||||||
}
|
generate_route_result(
|
||||||
|
&Route::from_str("https://example.com").unwrap(),
|
||||||
|
"hello world"
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[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();
|
let mut map: HashMap<String, Route> = HashMap::new();
|
||||||
map.insert("google".into(), Route::from("https://example.com"));
|
map.insert(
|
||||||
assert_eq!(
|
"google".into(),
|
||||||
resolve_hop("google hello world", &map, &Some(String::from("a"))),
|
Route::from_str("https://example.com").unwrap(),
|
||||||
generate_route_result(&Route::from("https://example.com"), "hello world"),
|
);
|
||||||
);
|
assert_eq!(
|
||||||
Ok(())
|
resolve_hop("google hello world", &map, &Some(String::from("a"))),
|
||||||
}
|
generate_route_result(
|
||||||
|
&Route::from_str("https://example.com").unwrap(),
|
||||||
|
"hello world"
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[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();
|
let mut map: HashMap<String, Route> = HashMap::new();
|
||||||
map.insert("google".into(), Route::from("https://example.com"));
|
map.insert(
|
||||||
assert_eq!(
|
"google".into(),
|
||||||
resolve_hop("google hello world", &map, &None),
|
Route::from_str("https://example.com").unwrap(),
|
||||||
generate_route_result(&Route::from("https://example.com"), "hello world"),
|
);
|
||||||
);
|
assert_eq!(
|
||||||
Ok(())
|
resolve_hop("google hello world", &map, &None),
|
||||||
}
|
generate_route_result(
|
||||||
|
&Route::from_str("https://example.com").unwrap(),
|
||||||
|
"hello world"
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod check_route {
|
mod check_route {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn create_route(
|
fn create_route(
|
||||||
min_args: impl Into<Option<usize>>,
|
min_args: impl Into<Option<usize>>,
|
||||||
max_args: impl Into<Option<usize>>,
|
max_args: impl Into<Option<usize>>,
|
||||||
) -> Route {
|
) -> Route {
|
||||||
Route {
|
Route {
|
||||||
description: None,
|
description: None,
|
||||||
hidden: false,
|
hidden: false,
|
||||||
max_args: max_args.into(),
|
max_args: max_args.into(),
|
||||||
min_args: min_args.into(),
|
min_args: min_args.into(),
|
||||||
path: String::new(),
|
path: String::new(),
|
||||||
route_type: RouteType::External,
|
route_type: RouteType::External,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_min_arg_no_max_arg_counts() {
|
fn no_min_arg_no_max_arg_counts() {
|
||||||
assert!(check_route(&create_route(None, None), 0));
|
assert!(check_route(&create_route(None, None), 0));
|
||||||
assert!(check_route(&create_route(None, None), usize::MAX));
|
assert!(check_route(&create_route(None, None), usize::MAX));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn min_arg_no_max_arg_counts() {
|
fn min_arg_no_max_arg_counts() {
|
||||||
assert!(!check_route(&create_route(3, None), 0));
|
assert!(!check_route(&create_route(3, None), 0));
|
||||||
assert!(!check_route(&create_route(3, None), 2));
|
assert!(!check_route(&create_route(3, None), 2));
|
||||||
assert!(check_route(&create_route(3, None), 3));
|
assert!(check_route(&create_route(3, None), 3));
|
||||||
assert!(check_route(&create_route(3, None), 4));
|
assert!(check_route(&create_route(3, None), 4));
|
||||||
assert!(check_route(&create_route(3, None), usize::MAX));
|
assert!(check_route(&create_route(3, None), usize::MAX));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_min_arg_max_arg_counts() {
|
fn no_min_arg_max_arg_counts() {
|
||||||
assert!(check_route(&create_route(None, 3), 0));
|
assert!(check_route(&create_route(None, 3), 0));
|
||||||
assert!(check_route(&create_route(None, 3), 2));
|
assert!(check_route(&create_route(None, 3), 2));
|
||||||
assert!(check_route(&create_route(None, 3), 3));
|
assert!(check_route(&create_route(None, 3), 3));
|
||||||
assert!(!check_route(&create_route(None, 3), 4));
|
assert!(!check_route(&create_route(None, 3), 4));
|
||||||
assert!(!check_route(&create_route(None, 3), usize::MAX));
|
assert!(!check_route(&create_route(None, 3), usize::MAX));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn min_arg_max_arg_counts() {
|
fn min_arg_max_arg_counts() {
|
||||||
assert!(!check_route(&create_route(2, 3), 1));
|
assert!(!check_route(&create_route(2, 3), 1));
|
||||||
assert!(check_route(&create_route(2, 3), 2));
|
assert!(check_route(&create_route(2, 3), 2));
|
||||||
assert!(check_route(&create_route(2, 3), 3));
|
assert!(check_route(&create_route(2, 3), 3));
|
||||||
assert!(!check_route(&create_route(2, 3), 4));
|
assert!(!check_route(&create_route(2, 3), 4));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod resolve_path {
|
mod resolve_path {
|
||||||
use crate::error::BunBunError;
|
use super::{resolve_path, HopAction};
|
||||||
|
use std::env::current_dir;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use super::{resolve_path, HopAction};
|
#[test]
|
||||||
use anyhow::Result;
|
fn invalid_path_returns_err() {
|
||||||
use std::env::current_dir;
|
assert!(resolve_path(PathBuf::from("/bin/aaaa"), "aaaa").is_err());
|
||||||
use std::io::ErrorKind;
|
}
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn invalid_path_returns_err() {
|
fn valid_path_returns_ok() {
|
||||||
assert!(resolve_path(&Path::new("/bin/aaaa"), "aaaa").is_err());
|
assert!(
|
||||||
}
|
resolve_path(PathBuf::from("/bin/echo"), r#"{"body": "a"}"#).is_ok()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn valid_path_returns_ok() {
|
fn relative_path_returns_ok() {
|
||||||
assert!(resolve_path(&Path::new("/bin/echo"), r#"{"body": "a"}"#).is_ok());
|
// How many ".." needed to get to /
|
||||||
}
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn relative_path_returns_ok() -> Result<()> {
|
fn no_permissions_returns_err() {
|
||||||
// How many ".." needed to get to /
|
assert!(
|
||||||
let nest_level = current_dir()?.ancestors().count() - 1;
|
// Trying to run a command without permission
|
||||||
let mut rel_path = PathBuf::from("../".repeat(nest_level));
|
format!(
|
||||||
rel_path.push("./bin/echo");
|
"{}",
|
||||||
assert!(resolve_path(&rel_path, r#"{"body": "a"}"#).is_ok());
|
resolve_path(PathBuf::from("/root/some_exec"), "").unwrap_err()
|
||||||
Ok(())
|
)
|
||||||
}
|
.contains("Permission denied")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_permissions_returns_err() {
|
fn non_success_exit_code_yields_err() {
|
||||||
let result = match resolve_path(&Path::new("/root/some_exec"), "") {
|
// cat-ing a folder always returns exit code 1
|
||||||
Err(BunBunError::Io(e)) => e.kind() == ErrorKind::PermissionDenied,
|
assert!(resolve_path(PathBuf::from("/bin/cat"), "/").is_err());
|
||||||
_ => false,
|
}
|
||||||
};
|
|
||||||
assert!(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn non_success_exit_code_yields_err() {
|
fn return_body() {
|
||||||
// cat-ing a folder always returns exit code 1
|
assert_eq!(
|
||||||
assert!(resolve_path(&Path::new("/bin/cat"), "/").is_err());
|
resolve_path(PathBuf::from("/bin/echo"), r#"{"body": "a"}"#).unwrap(),
|
||||||
}
|
HopAction::Body("a".to_string())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn return_body() -> Result<()> {
|
fn return_redirect() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resolve_path(&Path::new("/bin/echo"), r#"{"body": "a"}"#)?,
|
resolve_path(PathBuf::from("/bin/echo"), r#"{"redirect": "a"}"#).unwrap(),
|
||||||
HopAction::Body("a".to_string())
|
HopAction::Redirect("a".to_string())
|
||||||
);
|
);
|
||||||
|
}
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn return_redirect() -> Result<()> {
|
|
||||||
assert_eq!(
|
|
||||||
resolve_path(&Path::new("/bin/echo"), r#"{"redirect": "a"}"#)?,
|
|
||||||
HopAction::Redirect("a".to_string())
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,17 @@
|
||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
use percent_encoding::PercentEncode;
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
pub fn query(query: PercentEncode<'_>) -> impl Serialize + '_ {
|
pub fn query(query: String) -> impl Serialize {
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct TemplateArgs<'a> {
|
struct TemplateArgs {
|
||||||
query: Cow<'a, str>,
|
query: String,
|
||||||
}
|
}
|
||||||
TemplateArgs {
|
TemplateArgs { query }
|
||||||
query: query.into(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hostname(hostname: &'_ str) -> impl Serialize + '_ {
|
pub fn hostname(hostname: String) -> impl Serialize {
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct TemplateArgs<'a> {
|
pub struct TemplateArgs {
|
||||||
pub hostname: &'a str,
|
pub hostname: String,
|
||||||
}
|
}
|
||||||
TemplateArgs { hostname }
|
TemplateArgs { hostname }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue