From 33288efe82793e7d289070bd8d90ca0cc8c0cce8 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Tue, 23 Apr 2019 18:21:03 +0200 Subject: [PATCH] Use templates and load version information --- src/error.rs | 4 ++-- src/main.rs | 29 ++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/error.rs b/src/error.rs index fd6cd13..07845cf 100644 --- a/src/error.rs +++ b/src/error.rs @@ -29,13 +29,13 @@ impl ResponseError for Error { fn error_response(&self) -> HttpResponse { HttpResponse::InternalServerError() .content_type("text/html") - .body(P500) + .body(P500.as_slice()) } fn render_response(&self) -> HttpResponse { HttpResponse::InternalServerError() .content_type("text/html") - .body(P500) + .body(P500.as_slice()) } } diff --git a/src/main.rs b/src/main.rs index 0c534bf..4187575 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,8 +32,28 @@ use std::{ }; use structopt::StructOpt; +include!(concat!(env!("OUT_DIR"), "/templates.rs")); + +const COMMIT: &str = env!("VERGEN_SHA_SHORT"); +const VERSION: &str = env!("CARGO_PKG_VERSION"); + lazy_static! { static ref CLIENT: reqwest::Client = reqwest::Client::new(); + static ref INDEX: Vec = { + let mut buf = Vec::new(); + templates::index(&mut buf, COMMIT, VERSION).unwrap(); + buf + }; + static ref P404: Vec = { + let mut buf = Vec::new(); + templates::p404(&mut buf, COMMIT, VERSION).unwrap(); + buf + }; + static ref P500: Vec = { + let mut buf = Vec::new(); + templates::p500(&mut buf, COMMIT, VERSION).unwrap(); + buf + }; } struct State { @@ -41,9 +61,6 @@ struct State { cache: String, } -const INDEX: &str = include_str!("../static/index.html"); -const P404: &str = include_str!("../static/404.html"); -const P500: &str = include_str!("../static/500.html"); const CSS: &str = include_str!("../static/tacit-css.min.css"); #[derive(StructOpt, Debug)] @@ -211,13 +228,15 @@ fn overview(_: web::Path<(String, String)>) -> HttpResponse { #[get("/")] fn index() -> HttpResponse { - HttpResponse::Ok().content_type("text/html").body(INDEX) + HttpResponse::Ok() + .content_type("text/html") + .body(INDEX.as_slice()) } fn p404() -> HttpResponse { HttpResponse::NotFound() .content_type("text/html") - .body(P404) + .body(P404.as_slice()) } #[get("/tacit-css.min.css")]