Compare commits

...

2 Commits

Author SHA1 Message Date
Edward Shen 7995babb64
less unwrapping 2020-07-05 01:51:24 -04:00
Edward Shen 39a1037b33
remove uncessary arc 2020-07-05 01:37:45 -04:00
1 changed files with 21 additions and 28 deletions

View File

@ -12,7 +12,7 @@ use crate::config::{
use actix_web::{middleware::Logger, App, HttpServer};
use clap::Clap;
use error::BunBunError;
use handlebars::Handlebars;
use handlebars::{Handlebars, TemplateError};
use hotwatch::{Event, Hotwatch};
use log::{debug, error, info, trace, warn};
use std::cmp::min;
@ -65,12 +65,18 @@ async fn run() -> Result<(), BunBunError> {
groups: conf.groups,
}));
let _watch = start_watch(state.clone(), conf_data)?;
let _watch = start_watch(Arc::clone(&state), conf_data)?;
HttpServer::new(move || {
let templates = match compile_templates() {
Ok(templates) => templates,
// This implies a template error, which should be a compile time error. If
// we reach here then the release is very broken.
Err(e) => unreachable!("Failed to compile templates: {}", e),
};
App::new()
.data(state.clone())
.app_data(compile_templates())
.data(Arc::clone(&state))
.app_data(templates)
.wrap(Logger::default())
.service(routes::hop)
.service(routes::list)
@ -130,15 +136,11 @@ fn cache_routes(groups: &[RouteGroup]) -> HashMap<String, Route> {
/// 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.
fn compile_templates() -> Handlebars {
fn compile_templates() -> Result<Handlebars, TemplateError> {
let mut handlebars = Handlebars::new();
handlebars.set_strict_mode(true);
handlebars
.register_partial("bunbun_version", env!("CARGO_PKG_VERSION"))
.unwrap();
handlebars
.register_partial("bunbun_src", env!("CARGO_PKG_REPOSITORY"))
.unwrap();
handlebars.register_partial("bunbun_version", env!("CARGO_PKG_VERSION"))?;
handlebars.register_partial("bunbun_src", env!("CARGO_PKG_REPOSITORY"))?;
macro_rules! register_template {
[ $( $template:expr ),* ] => {
$(
@ -147,14 +149,13 @@ fn compile_templates() -> Handlebars {
$template,
String::from_utf8_lossy(
include_bytes!(concat!("templates/", $template, ".hbs")))
)
.unwrap();
)?;
debug!("Loaded {} template.", $template);
)*
};
}
register_template!["index", "list", "opensearch"];
handlebars
Ok(handlebars)
}
/// Starts the watch on a file, if possible. This will only return an Error if
@ -171,22 +172,14 @@ fn start_watch(
config_data: ConfigData,
) -> Result<Hotwatch, BunBunError> {
let mut watch = Hotwatch::new_with_custom_delay(Duration::from_millis(500))?;
// Closures need their own copy of variables for proper life cycle management
let config_data = Arc::new(config_data);
let config_data_ref = Arc::clone(&config_data);
let watch_result = watch.watch(&config_data.path, move |e: Event| {
let ConfigData { path, file } = config_data;
let watch_result = watch.watch(&path, move |e: Event| {
if let Event::Write(_) = e {
trace!("Grabbing writer lock on state...");
let mut state = state.write().expect("Failed to get write lock on state");
trace!("Obtained writer lock on state!");
match read_config(
config_data_ref
.file
.try_clone()
.expect("Failed to clone file handle"),
) {
match read_config(file.try_clone().expect("Failed to clone file handle"))
{
Ok(conf) => {
state.public_address = conf.public_address;
state.default_route = conf.default_route;
@ -202,10 +195,10 @@ fn start_watch(
});
match watch_result {
Ok(_) => info!("Watcher is now watching {:?}", &config_data.path),
Ok(_) => info!("Watcher is now watching {:?}", &path),
Err(e) => warn!(
"Couldn't watch {:?}: {}. Changes to this file won't be seen!",
&config_data.path, e
&path, e
),
}