2019-12-15 08:07:36 -08:00
|
|
|
use actix_web::{
|
2019-12-21 11:21:13 -08:00
|
|
|
get,
|
|
|
|
http::header,
|
|
|
|
web::{Data, Query},
|
|
|
|
App, HttpResponse, HttpServer, Responder,
|
2019-12-15 08:07:36 -08:00
|
|
|
};
|
|
|
|
use handlebars::Handlebars;
|
2019-12-21 11:04:13 -08:00
|
|
|
use hotwatch::{Event, Hotwatch};
|
2019-12-15 09:49:16 -08:00
|
|
|
use serde::Deserialize;
|
2019-12-15 11:57:42 -08:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2019-12-21 12:43:04 -08:00
|
|
|
use std::fmt;
|
2019-12-15 08:07:36 -08:00
|
|
|
use std::fs::{File, OpenOptions};
|
2019-12-21 12:43:04 -08:00
|
|
|
use std::io::Write;
|
2019-12-15 09:49:16 -08:00
|
|
|
use std::sync::{Arc, RwLock};
|
2019-12-21 11:04:13 -08:00
|
|
|
use std::time::Duration;
|
2019-12-15 08:07:36 -08:00
|
|
|
|
2019-12-15 11:57:42 -08:00
|
|
|
static DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.toml");
|
2019-12-15 10:54:09 -08:00
|
|
|
static CONFIG_FILE: &str = "bunbun.toml";
|
2019-12-15 08:07:36 -08:00
|
|
|
|
2019-12-21 12:43:04 -08:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum BunBunError {
|
|
|
|
IoError(std::io::Error),
|
2019-12-21 12:56:59 -08:00
|
|
|
ParseError(serde_yaml::Error),
|
2019-12-21 20:34:03 -08:00
|
|
|
WatchError(hotwatch::Error),
|
2019-12-21 12:43:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for BunBunError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
BunBunError::IoError(e) => e.fmt(f),
|
2019-12-21 12:56:59 -08:00
|
|
|
BunBunError::ParseError(e) => e.fmt(f),
|
2019-12-21 20:34:03 -08:00
|
|
|
BunBunError::WatchError(e) => e.fmt(f),
|
2019-12-21 12:43:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:05:01 -08:00
|
|
|
/// Generates a from implementation from the specified type to the provided
|
|
|
|
/// bunbun error.
|
2019-12-21 20:34:03 -08:00
|
|
|
macro_rules! from_error {
|
|
|
|
($from:ty, $to:ident) => {
|
|
|
|
impl From<$from> for BunBunError {
|
|
|
|
fn from(e: $from) -> Self {
|
|
|
|
BunBunError::$to(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-12-21 12:43:04 -08:00
|
|
|
}
|
|
|
|
|
2019-12-21 20:34:03 -08:00
|
|
|
from_error!(std::io::Error, IoError);
|
|
|
|
from_error!(serde_yaml::Error, ParseError);
|
|
|
|
from_error!(hotwatch::Error, WatchError);
|
2019-12-21 12:56:59 -08:00
|
|
|
|
2019-12-15 08:07:36 -08:00
|
|
|
#[get("/ls")]
|
2019-12-21 11:04:13 -08:00
|
|
|
fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
2019-12-21 11:21:13 -08:00
|
|
|
let data = data.read().unwrap();
|
|
|
|
HttpResponse::Ok().body(data.renderer.render("list", &data.routes).unwrap())
|
2019-12-15 10:54:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SearchQuery {
|
2019-12-21 11:21:13 -08:00
|
|
|
to: String,
|
2019-12-15 08:07:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/hop")]
|
2019-12-22 17:05:01 -08:00
|
|
|
fn hop(
|
|
|
|
data: Data<Arc<RwLock<State>>>,
|
|
|
|
query: Query<SearchQuery>,
|
|
|
|
) -> impl Responder {
|
2019-12-21 11:21:13 -08:00
|
|
|
let data = data.read().unwrap();
|
|
|
|
let mut raw_args = query.to.split_ascii_whitespace();
|
|
|
|
let command = raw_args.next();
|
|
|
|
|
|
|
|
if command.is_none() {
|
|
|
|
return HttpResponse::NotFound().body("not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reform args into url-safe string (probably want to go thru an actual parser)
|
|
|
|
let mut args = String::new();
|
|
|
|
if let Some(first_arg) = raw_args.next() {
|
|
|
|
args.push_str(first_arg);
|
|
|
|
for arg in raw_args {
|
|
|
|
args.push_str("+");
|
|
|
|
args.push_str(arg);
|
2019-12-15 08:07:36 -08:00
|
|
|
}
|
2019-12-21 11:21:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut template_args = HashMap::new();
|
|
|
|
template_args.insert("query", args);
|
|
|
|
|
|
|
|
match data.routes.get(command.unwrap()) {
|
|
|
|
Some(template) => HttpResponse::Found()
|
|
|
|
.header(
|
|
|
|
header::LOCATION,
|
|
|
|
data
|
|
|
|
.renderer
|
|
|
|
.render_template(template, &template_args)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.finish(),
|
|
|
|
None => match &data.default_route {
|
|
|
|
Some(route) => {
|
|
|
|
template_args.insert(
|
|
|
|
"query",
|
|
|
|
format!(
|
|
|
|
"{}+{}",
|
|
|
|
command.unwrap(),
|
|
|
|
template_args.get("query").unwrap()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
HttpResponse::Found()
|
|
|
|
.header(
|
|
|
|
header::LOCATION,
|
|
|
|
data
|
|
|
|
.renderer
|
|
|
|
.render_template(data.routes.get(route).unwrap(), &template_args)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
None => HttpResponse::NotFound().body("not found"),
|
|
|
|
},
|
|
|
|
}
|
2019-12-15 08:07:36 -08:00
|
|
|
}
|
|
|
|
|
2019-12-15 09:49:16 -08:00
|
|
|
#[get("/")]
|
2019-12-21 11:04:13 -08:00
|
|
|
fn index(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
2019-12-21 11:21:13 -08:00
|
|
|
let data = data.read().unwrap();
|
|
|
|
let mut template_args = HashMap::new();
|
|
|
|
template_args.insert("hostname", &data.public_address);
|
2019-12-22 17:05:01 -08:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.body(data.renderer.render("index", &template_args).unwrap())
|
2019-12-15 09:49:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/bunbunsearch.xml")]
|
2019-12-21 11:04:13 -08:00
|
|
|
fn opensearch(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
2019-12-21 11:21:13 -08:00
|
|
|
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).unwrap())
|
2019-12-15 09:49:16 -08:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:05:01 -08:00
|
|
|
/// Dynamic variables that either need to be present at runtime, or can be
|
|
|
|
/// changed during runtime.
|
2019-12-15 09:49:16 -08:00
|
|
|
struct State {
|
2019-12-21 11:21:13 -08:00
|
|
|
public_address: String,
|
|
|
|
default_route: Option<String>,
|
|
|
|
routes: BTreeMap<String, String>,
|
|
|
|
renderer: Handlebars,
|
2019-12-15 09:49:16 -08:00
|
|
|
}
|
|
|
|
|
2019-12-21 12:43:04 -08:00
|
|
|
fn main() -> Result<(), BunBunError> {
|
2019-12-21 11:21:13 -08:00
|
|
|
let conf = read_config(CONFIG_FILE)?;
|
|
|
|
let renderer = compile_templates();
|
|
|
|
let state = Arc::from(RwLock::new(State {
|
|
|
|
public_address: conf.public_address,
|
|
|
|
default_route: conf.default_route,
|
|
|
|
routes: conf.routes,
|
|
|
|
renderer,
|
|
|
|
}));
|
|
|
|
let state_ref = state.clone();
|
|
|
|
|
2019-12-21 20:34:03 -08:00
|
|
|
let mut watch = Hotwatch::new_with_custom_delay(Duration::from_millis(500))?;
|
|
|
|
|
|
|
|
watch.watch(CONFIG_FILE, move |e: Event| {
|
|
|
|
if let Event::Write(_) = e {
|
|
|
|
let mut state = state.write().unwrap();
|
|
|
|
match read_config(CONFIG_FILE) {
|
|
|
|
Ok(conf) => {
|
|
|
|
state.public_address = conf.public_address;
|
|
|
|
state.default_route = conf.default_route;
|
|
|
|
state.routes = conf.routes;
|
2019-12-21 12:56:59 -08:00
|
|
|
}
|
2019-12-21 20:34:03 -08:00
|
|
|
Err(e) => eprintln!("Config is malformed: {}", e),
|
2019-12-21 11:21:13 -08:00
|
|
|
}
|
2019-12-21 20:34:03 -08:00
|
|
|
}
|
|
|
|
})?;
|
2019-12-21 11:21:13 -08:00
|
|
|
|
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.data(state_ref.clone())
|
|
|
|
.service(hop)
|
|
|
|
.service(list)
|
|
|
|
.service(index)
|
|
|
|
.service(opensearch)
|
|
|
|
})
|
|
|
|
.bind(&conf.bind_address)?
|
2019-12-21 12:43:04 -08:00
|
|
|
.run()?;
|
|
|
|
|
|
|
|
Ok(())
|
2019-12-15 09:49:16 -08:00
|
|
|
}
|
2019-12-15 08:07:36 -08:00
|
|
|
|
2019-12-22 17:05:01 -08:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Config {
|
|
|
|
bind_address: String,
|
|
|
|
public_address: String,
|
|
|
|
default_route: Option<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.
|
2019-12-21 12:43:04 -08:00
|
|
|
fn read_config(config_file_path: &str) -> Result<Config, BunBunError> {
|
2019-12-21 11:21:13 -08:00
|
|
|
let config_file = match File::open(config_file_path) {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(_) => {
|
|
|
|
eprintln!(
|
|
|
|
"Unable to find a {} file. Creating default!",
|
|
|
|
config_file_path
|
|
|
|
);
|
|
|
|
let mut fd = OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create_new(true)
|
|
|
|
.open(config_file_path)
|
|
|
|
.expect("Unable to write to directory!");
|
|
|
|
fd.write_all(DEFAULT_CONFIG)?;
|
|
|
|
File::open(config_file_path)?
|
|
|
|
}
|
|
|
|
};
|
2019-12-21 12:56:59 -08:00
|
|
|
Ok(serde_yaml::from_reader(config_file)?)
|
2019-12-21 11:16:47 -08:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:05:01 -08:00
|
|
|
/// Returns an instance with all pre-generated templates included into the
|
|
|
|
/// binary. This allows for users to have a portable binary without needed the
|
|
|
|
/// templates at runtime.
|
2019-12-15 09:49:16 -08:00
|
|
|
fn compile_templates() -> Handlebars {
|
2019-12-21 11:21:13 -08:00
|
|
|
let mut handlebars = Handlebars::new();
|
2019-12-21 20:34:03 -08:00
|
|
|
macro_rules! register_template {
|
2019-12-22 17:05:01 -08:00
|
|
|
[ $( $template:expr ),* ] => {
|
2019-12-21 20:34:03 -08:00
|
|
|
$(
|
|
|
|
handlebars
|
|
|
|
.register_template_string(
|
|
|
|
$template,
|
|
|
|
String::from_utf8_lossy(
|
|
|
|
include_bytes!(concat!("templates/", $template, ".hbs")))
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
2019-12-22 17:05:01 -08:00
|
|
|
register_template!["index", "list", "opensearch"];
|
2019-12-21 11:21:13 -08:00
|
|
|
handlebars
|
2019-12-15 08:07:36 -08:00
|
|
|
}
|