Compare commits
2 commits
10731c22f4
...
7995babb64
Author | SHA1 | Date | |
---|---|---|---|
7995babb64 | |||
39a1037b33 |
1 changed files with 21 additions and 28 deletions
49
src/main.rs
49
src/main.rs
|
@ -12,7 +12,7 @@ use crate::config::{
|
||||||
use actix_web::{middleware::Logger, App, HttpServer};
|
use actix_web::{middleware::Logger, App, HttpServer};
|
||||||
use clap::Clap;
|
use clap::Clap;
|
||||||
use error::BunBunError;
|
use error::BunBunError;
|
||||||
use handlebars::Handlebars;
|
use handlebars::{Handlebars, TemplateError};
|
||||||
use hotwatch::{Event, Hotwatch};
|
use hotwatch::{Event, Hotwatch};
|
||||||
use log::{debug, error, info, trace, warn};
|
use log::{debug, error, info, trace, warn};
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
|
@ -65,12 +65,18 @@ async fn run() -> Result<(), BunBunError> {
|
||||||
groups: conf.groups,
|
groups: conf.groups,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let _watch = start_watch(state.clone(), conf_data)?;
|
let _watch = start_watch(Arc::clone(&state), conf_data)?;
|
||||||
|
|
||||||
HttpServer::new(move || {
|
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()
|
App::new()
|
||||||
.data(state.clone())
|
.data(Arc::clone(&state))
|
||||||
.app_data(compile_templates())
|
.app_data(templates)
|
||||||
.wrap(Logger::default())
|
.wrap(Logger::default())
|
||||||
.service(routes::hop)
|
.service(routes::hop)
|
||||||
.service(routes::list)
|
.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
|
/// Returns an instance with all pre-generated templates included into the
|
||||||
/// binary. This allows for users to have a portable binary without needed the
|
/// binary. This allows for users to have a portable binary without needed the
|
||||||
/// templates at runtime.
|
/// templates at runtime.
|
||||||
fn compile_templates() -> Handlebars {
|
fn compile_templates() -> Result<Handlebars, TemplateError> {
|
||||||
let mut handlebars = Handlebars::new();
|
let mut handlebars = Handlebars::new();
|
||||||
handlebars.set_strict_mode(true);
|
handlebars.set_strict_mode(true);
|
||||||
handlebars
|
handlebars.register_partial("bunbun_version", env!("CARGO_PKG_VERSION"))?;
|
||||||
.register_partial("bunbun_version", env!("CARGO_PKG_VERSION"))
|
handlebars.register_partial("bunbun_src", env!("CARGO_PKG_REPOSITORY"))?;
|
||||||
.unwrap();
|
|
||||||
handlebars
|
|
||||||
.register_partial("bunbun_src", env!("CARGO_PKG_REPOSITORY"))
|
|
||||||
.unwrap();
|
|
||||||
macro_rules! register_template {
|
macro_rules! register_template {
|
||||||
[ $( $template:expr ),* ] => {
|
[ $( $template:expr ),* ] => {
|
||||||
$(
|
$(
|
||||||
|
@ -147,14 +149,13 @@ fn compile_templates() -> Handlebars {
|
||||||
$template,
|
$template,
|
||||||
String::from_utf8_lossy(
|
String::from_utf8_lossy(
|
||||||
include_bytes!(concat!("templates/", $template, ".hbs")))
|
include_bytes!(concat!("templates/", $template, ".hbs")))
|
||||||
)
|
)?;
|
||||||
.unwrap();
|
|
||||||
debug!("Loaded {} template.", $template);
|
debug!("Loaded {} template.", $template);
|
||||||
)*
|
)*
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
register_template!["index", "list", "opensearch"];
|
register_template!["index", "list", "opensearch"];
|
||||||
handlebars
|
Ok(handlebars)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Starts the watch on a file, if possible. This will only return an Error if
|
/// 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,
|
config_data: ConfigData,
|
||||||
) -> Result<Hotwatch, BunBunError> {
|
) -> Result<Hotwatch, BunBunError> {
|
||||||
let mut watch = Hotwatch::new_with_custom_delay(Duration::from_millis(500))?;
|
let mut watch = Hotwatch::new_with_custom_delay(Duration::from_millis(500))?;
|
||||||
|
let ConfigData { path, file } = config_data;
|
||||||
// Closures need their own copy of variables for proper life cycle management
|
let watch_result = watch.watch(&path, move |e: Event| {
|
||||||
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 {
|
if let Event::Write(_) = e {
|
||||||
trace!("Grabbing writer lock on state...");
|
trace!("Grabbing writer lock on state...");
|
||||||
let mut state = state.write().expect("Failed to get write lock on state");
|
let mut state = state.write().expect("Failed to get write lock on state");
|
||||||
trace!("Obtained writer lock on state!");
|
trace!("Obtained writer lock on state!");
|
||||||
match read_config(
|
match read_config(file.try_clone().expect("Failed to clone file handle"))
|
||||||
config_data_ref
|
{
|
||||||
.file
|
|
||||||
.try_clone()
|
|
||||||
.expect("Failed to clone file handle"),
|
|
||||||
) {
|
|
||||||
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;
|
||||||
|
@ -202,10 +195,10 @@ fn start_watch(
|
||||||
});
|
});
|
||||||
|
|
||||||
match watch_result {
|
match watch_result {
|
||||||
Ok(_) => info!("Watcher is now watching {:?}", &config_data.path),
|
Ok(_) => info!("Watcher is now watching {:?}", &path),
|
||||||
Err(e) => warn!(
|
Err(e) => warn!(
|
||||||
"Couldn't watch {:?}: {}. Changes to this file won't be seen!",
|
"Couldn't watch {:?}: {}. Changes to this file won't be seen!",
|
||||||
&config_data.path, e
|
&path, e
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue