use macros
This commit is contained in:
parent
320e41c6f2
commit
996924582a
1 changed files with 43 additions and 37 deletions
80
src/main.rs
80
src/main.rs
|
@ -22,6 +22,7 @@ static CONFIG_FILE: &str = "bunbun.toml";
|
||||||
enum BunBunError {
|
enum BunBunError {
|
||||||
IoError(std::io::Error),
|
IoError(std::io::Error),
|
||||||
ParseError(serde_yaml::Error),
|
ParseError(serde_yaml::Error),
|
||||||
|
WatchError(hotwatch::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for BunBunError {
|
impl fmt::Display for BunBunError {
|
||||||
|
@ -29,23 +30,26 @@ impl fmt::Display for BunBunError {
|
||||||
match self {
|
match self {
|
||||||
BunBunError::IoError(e) => e.fmt(f),
|
BunBunError::IoError(e) => e.fmt(f),
|
||||||
BunBunError::ParseError(e) => e.fmt(f),
|
BunBunError::ParseError(e) => e.fmt(f),
|
||||||
|
BunBunError::WatchError(e) => e.fmt(f),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for BunBunError {}
|
impl Error for BunBunError {}
|
||||||
|
|
||||||
impl From<std::io::Error> for BunBunError {
|
macro_rules! from_error {
|
||||||
fn from(error: std::io::Error) -> Self {
|
($from:ty, $to:ident) => {
|
||||||
BunBunError::IoError(error)
|
impl From<$from> for BunBunError {
|
||||||
}
|
fn from(e: $from) -> Self {
|
||||||
|
BunBunError::$to(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<serde_yaml::Error> for BunBunError {
|
from_error!(std::io::Error, IoError);
|
||||||
fn from(error: serde_yaml::Error) -> Self {
|
from_error!(serde_yaml::Error, ParseError);
|
||||||
BunBunError::ParseError(error)
|
from_error!(hotwatch::Error, WatchError);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/ls")]
|
#[get("/ls")]
|
||||||
fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
||||||
|
@ -163,24 +167,21 @@ fn main() -> Result<(), BunBunError> {
|
||||||
}));
|
}));
|
||||||
let state_ref = state.clone();
|
let state_ref = state.clone();
|
||||||
|
|
||||||
let mut watch =
|
let mut watch = Hotwatch::new_with_custom_delay(Duration::from_millis(500))?;
|
||||||
Hotwatch::new_with_custom_delay(Duration::from_millis(500)).expect("Failed to init watch");
|
|
||||||
|
|
||||||
watch
|
watch.watch(CONFIG_FILE, move |e: Event| {
|
||||||
.watch(CONFIG_FILE, move |e: Event| {
|
if let Event::Write(_) = e {
|
||||||
if let Event::Write(_) = e {
|
let mut state = state.write().unwrap();
|
||||||
let mut state = state.write().unwrap();
|
match read_config(CONFIG_FILE) {
|
||||||
match read_config(CONFIG_FILE) {
|
Ok(conf) => {
|
||||||
Ok(conf) => {
|
state.public_address = conf.public_address;
|
||||||
state.public_address = conf.public_address;
|
state.default_route = conf.default_route;
|
||||||
state.default_route = conf.default_route;
|
state.routes = conf.routes;
|
||||||
state.routes = conf.routes;
|
|
||||||
}
|
|
||||||
Err(e) => eprintln!("Config is malformed: {}", e),
|
|
||||||
}
|
}
|
||||||
|
Err(e) => eprintln!("Config is malformed: {}", e),
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
.expect("failed to watch");
|
})?;
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
|
@ -218,22 +219,27 @@ fn read_config(config_file_path: &str) -> Result<Config, BunBunError> {
|
||||||
|
|
||||||
fn compile_templates() -> Handlebars {
|
fn compile_templates() -> Handlebars {
|
||||||
let mut handlebars = Handlebars::new();
|
let mut handlebars = Handlebars::new();
|
||||||
handlebars
|
|
||||||
.register_template_string(
|
macro_rules! register_template {
|
||||||
"index",
|
( $( $template:expr ),* ) => {
|
||||||
String::from_utf8_lossy(include_bytes!("templates/index.hbs")),
|
$(
|
||||||
)
|
handlebars
|
||||||
.unwrap();
|
.register_template_string(
|
||||||
|
$template,
|
||||||
|
String::from_utf8_lossy(
|
||||||
|
include_bytes!(concat!("templates/", $template, ".hbs")))
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
)*
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
register_template!("index", "list");
|
||||||
|
|
||||||
handlebars
|
handlebars
|
||||||
.register_template_string(
|
.register_template_string(
|
||||||
"opensearch",
|
"opensearch",
|
||||||
String::from_utf8_lossy(include_bytes!("templates/bunbunsearch.xml")),
|
String::from_utf8_lossy(include_bytes!(concat!("templates/", "bunbunsearch.xml"))),
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
handlebars
|
|
||||||
.register_template_string(
|
|
||||||
"list",
|
|
||||||
String::from_utf8_lossy(include_bytes!("templates/list.hbs")),
|
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
handlebars
|
handlebars
|
||||||
|
|
Loading…
Reference in a new issue