Compare commits

..

No commits in common. "996924582afe7500af7d1ef3ba6b1ad6b80ebc57" and "606620ae6a64b2b9b4a4b67bcf61a89d3373d67a" have entirely different histories.

2 changed files with 145 additions and 196 deletions

View file

@ -1,2 +0,0 @@
tab_spaces = 2
use_field_init_shorthand = true

View file

@ -8,49 +8,14 @@ use handlebars::Handlebars;
use hotwatch::{Event, Hotwatch};
use serde::Deserialize;
use std::collections::{BTreeMap, HashMap};
use std::error::Error;
use std::fmt;
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::io::{Error, Write};
use std::sync::{Arc, RwLock};
use std::time::Duration;
static DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.toml");
static CONFIG_FILE: &str = "bunbun.toml";
#[derive(Debug)]
enum BunBunError {
IoError(std::io::Error),
ParseError(serde_yaml::Error),
WatchError(hotwatch::Error),
}
impl fmt::Display for BunBunError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
BunBunError::IoError(e) => e.fmt(f),
BunBunError::ParseError(e) => e.fmt(f),
BunBunError::WatchError(e) => e.fmt(f),
}
}
}
impl Error for BunBunError {}
macro_rules! from_error {
($from:ty, $to:ident) => {
impl From<$from> for BunBunError {
fn from(e: $from) -> Self {
BunBunError::$to(e)
}
}
};
}
from_error!(std::io::Error, IoError);
from_error!(serde_yaml::Error, ParseError);
from_error!(hotwatch::Error, WatchError);
#[get("/ls")]
fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder {
let data = data.read().unwrap();
@ -89,8 +54,7 @@ fn hop(data: Data<Arc<RwLock<State>>>, query: Query<SearchQuery>) -> impl Respon
Some(template) => HttpResponse::Found()
.header(
header::LOCATION,
data
.renderer
data.renderer
.render_template(template, &template_args)
.unwrap(),
)
@ -108,8 +72,7 @@ fn hop(data: Data<Arc<RwLock<State>>>, query: Query<SearchQuery>) -> impl Respon
HttpResponse::Found()
.header(
header::LOCATION,
data
.renderer
data.renderer
.render_template(data.routes.get(route).unwrap(), &template_args)
.unwrap(),
)
@ -156,32 +119,47 @@ struct State {
renderer: Handlebars,
}
fn main() -> Result<(), BunBunError> {
let conf = read_config(CONFIG_FILE)?;
fn main() -> Result<(), Error> {
let config_file = match File::open(CONFIG_FILE) {
Ok(file) => file,
Err(_) => {
eprintln!("Unable to find a {} file. Creating default!", CONFIG_FILE);
let mut fd = OpenOptions::new()
.write(true)
.create_new(true)
.open(CONFIG_FILE)
.expect("Unable to write to directory!");
fd.write_all(DEFAULT_CONFIG)?;
File::open(CONFIG_FILE)?
}
};
let renderer = compile_templates();
let conf: Config = serde_yaml::from_reader(config_file).unwrap();
let state = Arc::from(RwLock::new(State {
public_address: conf.public_address,
default_route: conf.default_route,
routes: conf.routes,
renderer,
renderer: renderer,
}));
let state_ref = state.clone();
let mut watch = Hotwatch::new_with_custom_delay(Duration::from_millis(500))?;
let mut watch =
Hotwatch::new_with_custom_delay(Duration::from_millis(500)).expect("Failed to init watch");
watch.watch(CONFIG_FILE, move |e: Event| {
watch
.watch(CONFIG_FILE, move |e: Event| {
if let Event::Write(_) = e {
let config_file = File::open(CONFIG_FILE).unwrap();
let conf: Config = serde_yaml::from_reader(config_file).unwrap();
let state = state.clone();
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;
}
Err(e) => eprintln!("Config is malformed: {}", e),
}
}
})?;
})
.expect("failed to watch");
HttpServer::new(move || {
App::new()
@ -192,54 +170,27 @@ fn main() -> Result<(), BunBunError> {
.service(opensearch)
})
.bind(&conf.bind_address)?
.run()?;
Ok(())
}
fn read_config(config_file_path: &str) -> Result<Config, BunBunError> {
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)?
}
};
Ok(serde_yaml::from_reader(config_file)?)
.run()
}
fn compile_templates() -> Handlebars {
let mut handlebars = Handlebars::new();
macro_rules! register_template {
( $( $template:expr ),* ) => {
$(
handlebars
.register_template_string(
$template,
String::from_utf8_lossy(
include_bytes!(concat!("templates/", $template, ".hbs")))
"index",
String::from_utf8_lossy(include_bytes!("templates/index.hbs")),
)
.unwrap();
)*
};
}
register_template!("index", "list");
handlebars
.register_template_string(
"opensearch",
String::from_utf8_lossy(include_bytes!(concat!("templates/", "bunbunsearch.xml"))),
String::from_utf8_lossy(include_bytes!("templates/bunbunsearch.xml")),
)
.unwrap();
handlebars
.register_template_string(
"list",
String::from_utf8_lossy(include_bytes!("templates/list.hbs")),
)
.unwrap();
handlebars