less unwrapping

This commit is contained in:
Edward Shen 2020-07-05 01:51:24 -04:00
parent 39a1037b33
commit 7995babb64
Signed by: edward
GPG key ID: 19182661E818369F

View file

@ -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