Refactor the loading of handlebars templates out, and add some more error correction

Discovered another bug when vim dropped a .swp file in the templates/ directory.
Yay?
This commit is contained in:
R Tyler Croy 2020-01-18 12:44:21 -08:00
parent 70afc3df69
commit 49d073e74b
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
2 changed files with 66 additions and 8 deletions

View File

@ -6,6 +6,7 @@
* server loop that the eventbus uses.
*/
extern crate config;
extern crate futures;
extern crate log;
extern crate pretty_env_logger;
#[macro_use]
@ -14,10 +15,12 @@ extern crate rust_embed;
extern crate serde_json;
use chrono::Local;
use futures::{FutureExt, StreamExt};
use handlebars::Handlebars;
use log::{debug, error, info, trace};
use serde::Serialize;
use warp::Filter;
use warp::reject::Rejection;
use std::convert::Infallible;
use std::sync::Arc;
@ -26,8 +29,6 @@ use std::time::Duration;
use otto_eventbus::*;
pub mod eventbus;
/**
* Templates is a rust-embed struct which will contain all the files embedded from the
* eventbus/templates/ directory
@ -44,7 +45,7 @@ struct Templates;
#[folder = "$CARGO_MANIFEST_DIR/static"]
struct Static;
async fn index(hb: Arc<Handlebars>) -> Result<impl warp::Reply, Infallible> {
async fn index(hb: Arc<Handlebars>) -> Result<impl warp::Reply, Rejection> {
let data = json!({
"version" : option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"),
});
@ -97,21 +98,28 @@ fn load_settings() -> config::Config {
return settings;
}
#[tokio::main]
async fn main() {
pretty_env_logger::init();
let mut hb = Handlebars::new();
fn load_templates(hb: &mut Handlebars) {
for t in Templates::iter() {
if ! t.ends_with(".html") {
continue;
}
let template = Templates::get(&t)
.expect("Somehow we iterated Templates but didn't get one? How is this possible!");
let buf = std::str::from_utf8(template.as_ref())
.expect(format!("Unable to convert {} to a string buffer", &t).as_str());
hb.register_template_string(&t, buf)
.expect(format!("Failed to register {} as a Handlebars template", &t).as_str());
info!("Registered handlebars template: {}", &t);
}
}
#[tokio::main]
async fn main() {
pretty_env_logger::init();
let mut hb = Handlebars::new();
load_templates(&mut hb);
let hb = Arc::new(hb);
let _settings = load_settings();

View File

@ -4,6 +4,56 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Otto Eventbus 🤖</title>
<style type="text/css">
body {
margin: 0px;
font-family: 'proxima-nova', 'Helvetica', 'Avenir', 'Arial', sans-serif;
color: #4D4D4D;
background-color: white;
font-size: 1.1rem;
}
a:link, a:visited {
color: black;
}
a:hover {
color: red;
text-decoration: none;
}
@media(min-width: 1024px) {
.content {
max-width: 65%;
}
main {
margin-left: auto;
margin-right: auto;
}
footer {
margin-left: auto;
margin-right: auto;
margin-top: 2rem;
padding-top: 1rem;
border-top: 2px solid #e9e9e9;
max-width: 55%;
}
}
header.masthead {
float: right;
margin-right: 1rem;
}
.notice {
background: #ffffe0;
text-align: center;
margin-left: auto;
margin-right: auto;
padding: 1rem;
}
img {
max-width: 100%;
}
li {
margin: 0.5rem;
}
</style>
</head>
<body>
<center>