Compare commits
No commits in common. "5ea3b8da5beecb56cc094516c3b274f30ed5d7b1" and "b98769687b3d8c33d8a41ebf09aa5b6660ed79bf" have entirely different histories.
5ea3b8da5b
...
b98769687b
2 changed files with 33 additions and 63 deletions
79
src/main.rs
79
src/main.rs
|
@ -9,15 +9,13 @@ use hotwatch::{Event, Hotwatch};
|
|||
use itertools::Itertools;
|
||||
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::fmt;
|
||||
use std::fs::{read_to_string, OpenOptions};
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::time::Duration;
|
||||
|
||||
mod template_args;
|
||||
|
||||
/// https://url.spec.whatwg.org/#fragment-percent-encode-set
|
||||
static FRAGMENT_ENCODE_SET: &AsciiSet =
|
||||
&CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
|
||||
|
@ -77,20 +75,23 @@ fn hop(
|
|||
let data = data.read().unwrap();
|
||||
|
||||
match resolve_hop(&query.to, &data.routes, &data.default_route) {
|
||||
(Some(path), args) => HttpResponse::Found()
|
||||
.header(
|
||||
header::LOCATION,
|
||||
data
|
||||
.renderer
|
||||
.render_template(
|
||||
&path,
|
||||
&template_args::query(
|
||||
utf8_percent_encode(&args, FRAGMENT_ENCODE_SET).to_string(),
|
||||
),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.finish(),
|
||||
(Some(path), args) => {
|
||||
let mut template_args = HashMap::new();
|
||||
template_args.insert(
|
||||
"query",
|
||||
utf8_percent_encode(&args, FRAGMENT_ENCODE_SET).to_string(),
|
||||
);
|
||||
|
||||
HttpResponse::Found()
|
||||
.header(
|
||||
header::LOCATION,
|
||||
data
|
||||
.renderer
|
||||
.render_template(&path, &template_args)
|
||||
.unwrap(),
|
||||
)
|
||||
.finish()
|
||||
}
|
||||
(None, _) => HttpResponse::NotFound().body("not found"),
|
||||
}
|
||||
}
|
||||
|
@ -103,7 +104,7 @@ fn hop(
|
|||
/// returns the remaining arguments. If none remain, an empty string is given.
|
||||
fn resolve_hop(
|
||||
query: &str,
|
||||
routes: &HashMap<String, String>,
|
||||
routes: &BTreeMap<String, String>,
|
||||
default_route: &Option<String>,
|
||||
) -> (Option<String>, String) {
|
||||
let mut split_args = query.split_ascii_whitespace().peekable();
|
||||
|
@ -132,34 +133,23 @@ fn resolve_hop(
|
|||
#[get("/")]
|
||||
fn index(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
||||
let data = data.read().unwrap();
|
||||
HttpResponse::Ok().body(
|
||||
data
|
||||
.renderer
|
||||
.render(
|
||||
"index",
|
||||
&template_args::hostname(data.public_address.clone()),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
let mut template_args = HashMap::new();
|
||||
template_args.insert("hostname", &data.public_address);
|
||||
HttpResponse::Ok()
|
||||
.body(data.renderer.render("index", &template_args).unwrap())
|
||||
}
|
||||
|
||||
#[get("/bunbunsearch.xml")]
|
||||
fn opensearch(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
||||
let data = data.read().unwrap();
|
||||
let mut template_args = HashMap::new();
|
||||
template_args.insert("hostname", &data.public_address);
|
||||
HttpResponse::Ok()
|
||||
.header(
|
||||
header::CONTENT_TYPE,
|
||||
"application/opensearchdescription+xml",
|
||||
)
|
||||
.body(
|
||||
data
|
||||
.renderer
|
||||
.render(
|
||||
"opensearch",
|
||||
&template_args::hostname(data.public_address.clone()),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.body(data.renderer.render("opensearch", &template_args).unwrap())
|
||||
}
|
||||
|
||||
/// Dynamic variables that either need to be present at runtime, or can be
|
||||
|
@ -167,7 +157,7 @@ fn opensearch(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
|||
struct State {
|
||||
public_address: String,
|
||||
default_route: Option<String>,
|
||||
routes: HashMap<String, String>,
|
||||
routes: BTreeMap<String, String>,
|
||||
renderer: Handlebars,
|
||||
}
|
||||
|
||||
|
@ -217,14 +207,14 @@ struct Config {
|
|||
bind_address: String,
|
||||
public_address: String,
|
||||
default_route: Option<String>,
|
||||
routes: HashMap<String, String>,
|
||||
routes: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
/// Attempts to read the config file. If it doesn't exist, generate one a
|
||||
/// default config file before attempting to parse it.
|
||||
fn read_config(config_file_path: &str) -> Result<Config, BunBunError> {
|
||||
let config_str = match read_to_string(config_file_path) {
|
||||
Ok(conf_str) => conf_str,
|
||||
let config_file = match File::open(config_file_path) {
|
||||
Ok(file) => file,
|
||||
Err(_) => {
|
||||
eprintln!(
|
||||
"Unable to find a {} file. Creating default!",
|
||||
|
@ -236,13 +226,10 @@ fn read_config(config_file_path: &str) -> Result<Config, BunBunError> {
|
|||
.open(config_file_path)
|
||||
.expect("Unable to write to directory!");
|
||||
fd.write_all(DEFAULT_CONFIG)?;
|
||||
String::from_utf8_lossy(DEFAULT_CONFIG).into_owned()
|
||||
File::open(config_file_path)?
|
||||
}
|
||||
};
|
||||
|
||||
// Reading from memory is faster than reading directly from a reader for some
|
||||
// reason; see https://github.com/serde-rs/json/issues/160
|
||||
Ok(serde_yaml::from_str(&config_str)?)
|
||||
Ok(serde_yaml::from_reader(config_file)?)
|
||||
}
|
||||
|
||||
/// Returns an instance with all pre-generated templates included into the
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
use serde::Serialize;
|
||||
|
||||
pub fn query(query: String) -> impl Serialize {
|
||||
#[derive(Serialize)]
|
||||
struct TemplateArgs {
|
||||
query: String,
|
||||
}
|
||||
TemplateArgs { query }
|
||||
}
|
||||
|
||||
pub fn hostname(hostname: String) -> impl Serialize {
|
||||
#[derive(Serialize)]
|
||||
pub struct TemplateArgs {
|
||||
pub hostname: String,
|
||||
}
|
||||
TemplateArgs { hostname }
|
||||
}
|
Loading…
Reference in a new issue