Compare commits
No commits in common. "9ec219e9c7ee099ca54ea85d19b7dc1237962d21" and "5ea3b8da5beecb56cc094516c3b274f30ed5d7b1" have entirely different histories.
9ec219e9c7
...
5ea3b8da5b
2 changed files with 120 additions and 131 deletions
127
src/main.rs
127
src/main.rs
|
@ -1,6 +1,13 @@
|
||||||
use actix_web::{App, HttpServer};
|
use actix_web::{
|
||||||
|
get,
|
||||||
|
http::header,
|
||||||
|
web::{Data, Query},
|
||||||
|
App, HttpResponse, HttpServer, Responder,
|
||||||
|
};
|
||||||
use handlebars::Handlebars;
|
use handlebars::Handlebars;
|
||||||
use hotwatch::{Event, Hotwatch};
|
use hotwatch::{Event, Hotwatch};
|
||||||
|
use itertools::Itertools;
|
||||||
|
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
@ -9,9 +16,11 @@ use std::io::Write;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
mod routes;
|
|
||||||
mod template_args;
|
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'`');
|
||||||
static DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.toml");
|
static DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.toml");
|
||||||
static CONFIG_FILE: &str = "bunbun.toml";
|
static CONFIG_FILE: &str = "bunbun.toml";
|
||||||
|
|
||||||
|
@ -49,9 +58,113 @@ from_error!(std::io::Error, IoError);
|
||||||
from_error!(serde_yaml::Error, ParseError);
|
from_error!(serde_yaml::Error, ParseError);
|
||||||
from_error!(hotwatch::Error, WatchError);
|
from_error!(hotwatch::Error, WatchError);
|
||||||
|
|
||||||
|
#[get("/ls")]
|
||||||
|
fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
||||||
|
let data = data.read().unwrap();
|
||||||
|
HttpResponse::Ok().body(data.renderer.render("list", &data.routes).unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct SearchQuery {
|
||||||
|
to: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/hop")]
|
||||||
|
fn hop(
|
||||||
|
data: Data<Arc<RwLock<State>>>,
|
||||||
|
query: Query<SearchQuery>,
|
||||||
|
) -> impl Responder {
|
||||||
|
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(),
|
||||||
|
(None, _) => HttpResponse::NotFound().body("not found"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempts to resolve the provided string into its route and its arguments.
|
||||||
|
/// If a default route was provided, then this will consider that route before
|
||||||
|
/// failing to resolve a route.
|
||||||
|
///
|
||||||
|
/// 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.
|
||||||
|
fn resolve_hop(
|
||||||
|
query: &str,
|
||||||
|
routes: &HashMap<String, String>,
|
||||||
|
default_route: &Option<String>,
|
||||||
|
) -> (Option<String>, String) {
|
||||||
|
let mut split_args = query.split_ascii_whitespace().peekable();
|
||||||
|
let command = match split_args.peek() {
|
||||||
|
Some(command) => command,
|
||||||
|
None => return (None, String::new()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match (routes.get(*command), default_route) {
|
||||||
|
// Found a route
|
||||||
|
(Some(resolved), _) => (
|
||||||
|
Some(resolved.clone()),
|
||||||
|
match split_args.next() {
|
||||||
|
// Discard the first result, we found the route using the first arg
|
||||||
|
Some(_) => split_args.join(" "),
|
||||||
|
None => String::new(),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
// Unable to find route, but had a default route
|
||||||
|
(None, Some(route)) => (routes.get(route).cloned(), split_args.join(" ")),
|
||||||
|
// No default route and no match
|
||||||
|
(None, None) => (None, String::new()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/bunbunsearch.xml")]
|
||||||
|
fn opensearch(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
||||||
|
let data = data.read().unwrap();
|
||||||
|
HttpResponse::Ok()
|
||||||
|
.header(
|
||||||
|
header::CONTENT_TYPE,
|
||||||
|
"application/opensearchdescription+xml",
|
||||||
|
)
|
||||||
|
.body(
|
||||||
|
data
|
||||||
|
.renderer
|
||||||
|
.render(
|
||||||
|
"opensearch",
|
||||||
|
&template_args::hostname(data.public_address.clone()),
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// 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 {
|
struct State {
|
||||||
public_address: String,
|
public_address: String,
|
||||||
default_route: Option<String>,
|
default_route: Option<String>,
|
||||||
routes: HashMap<String, String>,
|
routes: HashMap<String, String>,
|
||||||
|
@ -88,10 +201,10 @@ fn main() -> Result<(), BunBunError> {
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.data(state_ref.clone())
|
.data(state_ref.clone())
|
||||||
.service(routes::hop)
|
.service(hop)
|
||||||
.service(routes::list)
|
.service(list)
|
||||||
.service(routes::index)
|
.service(index)
|
||||||
.service(routes::opensearch)
|
.service(opensearch)
|
||||||
})
|
})
|
||||||
.bind(&conf.bind_address)?
|
.bind(&conf.bind_address)?
|
||||||
.run()?;
|
.run()?;
|
||||||
|
|
124
src/routes.rs
124
src/routes.rs
|
@ -1,124 +0,0 @@
|
||||||
use crate::template_args;
|
|
||||||
use crate::State;
|
|
||||||
use actix_web::get;
|
|
||||||
use actix_web::http::header;
|
|
||||||
use actix_web::web::{Data, Query};
|
|
||||||
use actix_web::{HttpResponse, Responder};
|
|
||||||
use itertools::Itertools;
|
|
||||||
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
|
|
||||||
use serde::Deserialize;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::sync::{Arc, RwLock};
|
|
||||||
|
|
||||||
/// 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'`')
|
|
||||||
.add(b'+');
|
|
||||||
|
|
||||||
#[get("/ls")]
|
|
||||||
pub fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
|
||||||
let data = data.read().unwrap();
|
|
||||||
HttpResponse::Ok().body(data.renderer.render("list", &data.routes).unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct SearchQuery {
|
|
||||||
to: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/hop")]
|
|
||||||
pub fn hop(
|
|
||||||
data: Data<Arc<RwLock<State>>>,
|
|
||||||
query: Query<SearchQuery>,
|
|
||||||
) -> impl Responder {
|
|
||||||
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(),
|
|
||||||
(None, _) => HttpResponse::NotFound().body("not found"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Attempts to resolve the provided string into its route and its arguments.
|
|
||||||
/// If a default route was provided, then this will consider that route before
|
|
||||||
/// failing to resolve a route.
|
|
||||||
///
|
|
||||||
/// 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.
|
|
||||||
fn resolve_hop(
|
|
||||||
query: &str,
|
|
||||||
routes: &HashMap<String, String>,
|
|
||||||
default_route: &Option<String>,
|
|
||||||
) -> (Option<String>, String) {
|
|
||||||
let mut split_args = query.split_ascii_whitespace().peekable();
|
|
||||||
let command = match split_args.peek() {
|
|
||||||
Some(command) => command,
|
|
||||||
None => return (None, String::new()),
|
|
||||||
};
|
|
||||||
|
|
||||||
match (routes.get(*command), default_route) {
|
|
||||||
// Found a route
|
|
||||||
(Some(resolved), _) => (
|
|
||||||
Some(resolved.clone()),
|
|
||||||
match split_args.next() {
|
|
||||||
// Discard the first result, we found the route using the first arg
|
|
||||||
Some(_) => split_args.join(" "),
|
|
||||||
None => String::new(),
|
|
||||||
},
|
|
||||||
),
|
|
||||||
// Unable to find route, but had a default route
|
|
||||||
(None, Some(route)) => (routes.get(route).cloned(), split_args.join(" ")),
|
|
||||||
// No default route and no match
|
|
||||||
(None, None) => (None, String::new()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/")]
|
|
||||||
pub 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(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/bunbunsearch.xml")]
|
|
||||||
pub fn opensearch(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
|
||||||
let data = data.read().unwrap();
|
|
||||||
HttpResponse::Ok()
|
|
||||||
.header(
|
|
||||||
header::CONTENT_TYPE,
|
|
||||||
"application/opensearchdescription+xml",
|
|
||||||
)
|
|
||||||
.body(
|
|
||||||
data
|
|
||||||
.renderer
|
|
||||||
.render(
|
|
||||||
"opensearch",
|
|
||||||
&template_args::hostname(data.public_address.clone()),
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
}
|
|
Loading…
Reference in a new issue