Compare commits
No commits in common. "7995babb64e9f995d5ba3733421708284596802c" and "10731c22f4bd6576bb4ea277bdf8b5a78ff86636" have entirely different histories.
7995babb64
...
10731c22f4
1 changed files with 28 additions and 21 deletions
49
src/main.rs
49
src/main.rs
|
@ -12,7 +12,7 @@ use crate::config::{
|
|||
use actix_web::{middleware::Logger, App, HttpServer};
|
||||
use clap::Clap;
|
||||
use error::BunBunError;
|
||||
use handlebars::{Handlebars, TemplateError};
|
||||
use handlebars::Handlebars;
|
||||
use hotwatch::{Event, Hotwatch};
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use std::cmp::min;
|
||||
|
@ -65,18 +65,12 @@ async fn run() -> Result<(), BunBunError> {
|
|||
groups: conf.groups,
|
||||
}));
|
||||
|
||||
let _watch = start_watch(Arc::clone(&state), conf_data)?;
|
||||
let _watch = start_watch(state.clone(), 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(Arc::clone(&state))
|
||||
.app_data(templates)
|
||||
.data(state.clone())
|
||||
.app_data(compile_templates())
|
||||
.wrap(Logger::default())
|
||||
.service(routes::hop)
|
||||
.service(routes::list)
|
||||
|
@ -136,11 +130,15 @@ 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() -> Result<Handlebars, TemplateError> {
|
||||
fn compile_templates() -> Handlebars {
|
||||
let mut handlebars = Handlebars::new();
|
||||
handlebars.set_strict_mode(true);
|
||||
handlebars.register_partial("bunbun_version", env!("CARGO_PKG_VERSION"))?;
|
||||
handlebars.register_partial("bunbun_src", env!("CARGO_PKG_REPOSITORY"))?;
|
||||
handlebars
|
||||
.register_partial("bunbun_version", env!("CARGO_PKG_VERSION"))
|
||||
.unwrap();
|
||||
handlebars
|
||||
.register_partial("bunbun_src", env!("CARGO_PKG_REPOSITORY"))
|
||||
.unwrap();
|
||||
macro_rules! register_template {
|
||||
[ $( $template:expr ),* ] => {
|
||||
$(
|
||||
|
@ -149,13 +147,14 @@ fn compile_templates() -> Result<Handlebars, TemplateError> {
|
|||
$template,
|
||||
String::from_utf8_lossy(
|
||||
include_bytes!(concat!("templates/", $template, ".hbs")))
|
||||
)?;
|
||||
)
|
||||
.unwrap();
|
||||
debug!("Loaded {} template.", $template);
|
||||
)*
|
||||
};
|
||||
}
|
||||
register_template!["index", "list", "opensearch"];
|
||||
Ok(handlebars)
|
||||
handlebars
|
||||
}
|
||||
|
||||
/// Starts the watch on a file, if possible. This will only return an Error if
|
||||
|
@ -172,14 +171,22 @@ fn start_watch(
|
|||
config_data: ConfigData,
|
||||
) -> Result<Hotwatch, BunBunError> {
|
||||
let mut watch = Hotwatch::new_with_custom_delay(Duration::from_millis(500))?;
|
||||
let ConfigData { path, file } = config_data;
|
||||
let watch_result = watch.watch(&path, move |e: Event| {
|
||||
|
||||
// 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| {
|
||||
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(file.try_clone().expect("Failed to clone file handle"))
|
||||
{
|
||||
match read_config(
|
||||
config_data_ref
|
||||
.file
|
||||
.try_clone()
|
||||
.expect("Failed to clone file handle"),
|
||||
) {
|
||||
Ok(conf) => {
|
||||
state.public_address = conf.public_address;
|
||||
state.default_route = conf.default_route;
|
||||
|
@ -195,10 +202,10 @@ fn start_watch(
|
|||
});
|
||||
|
||||
match watch_result {
|
||||
Ok(_) => info!("Watcher is now watching {:?}", &path),
|
||||
Ok(_) => info!("Watcher is now watching {:?}", &config_data.path),
|
||||
Err(e) => warn!(
|
||||
"Couldn't watch {:?}: {}. Changes to this file won't be seen!",
|
||||
&path, e
|
||||
&config_data.path, e
|
||||
),
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue