use the render_error! macro

master
rascul 2 years ago
parent 218c65e851
commit 4a0de56664

@ -1,11 +1,10 @@
use actix_web::{get, web, HttpResponse}; use actix_web::{Error, get, web, HttpResponse};
use handlebars::Handlebars; use handlebars::Handlebars;
use serde_json::json; use serde_json::Value;
#[get("/help")] use crate::render_error;
pub async fn get(hb: web::Data<Handlebars<'_>>) -> HttpResponse {
let data = json!({});
let body = hb.render("help", &data).unwrap();
HttpResponse::Ok().body(body) #[get("/help")]
pub async fn get(hb: web::Data<Handlebars<'_>>) -> Result<HttpResponse, Error> {
Ok(HttpResponse::Ok().body(render_error!(hb, hb.render("help", &Value::Null))))
} }

@ -1,13 +1,10 @@
use actix_web::{get, web, HttpResponse}; use actix_web::{get, web, Error, HttpResponse};
use handlebars::Handlebars; use handlebars::Handlebars;
use serde_json::json; use serde_json::Value;
#[get("/")] use crate::render_error;
pub async fn get(hb: web::Data<Handlebars<'_>>) -> HttpResponse {
let data = json!({
"name": "rewot"
});
let body = hb.render("index", &data).unwrap();
HttpResponse::Ok().body(body) #[get("/")]
pub async fn get(hb: web::Data<Handlebars<'_>>) -> Result<HttpResponse, Error> {
Ok(HttpResponse::Ok().body(render_error!(hb, hb.render("index", &Value::Null))))
} }

@ -9,7 +9,7 @@ use handlebars::Handlebars;
use log::info; use log::info;
use serde_json::json; use serde_json::json;
use crate::Result; use crate::{render_error, Result};
/// load up and play the replay /// load up and play the replay
#[get("/+{id}")] #[get("/+{id}")]
@ -17,16 +17,17 @@ pub async fn get(id: Path<String>, hb: Data<Handlebars<'_>>) -> Result<HttpRespo
let mut path = PathBuf::from("replays"); let mut path = PathBuf::from("replays");
let id: String = id.into_inner(); let id: String = id.into_inner();
path.push(&id); path.push(&id);
let file: File = File::open(&path)?;
let file: File = render_error!(hb, File::open(&path));
let mut buffer = BufReader::new(file); let mut buffer = BufReader::new(file);
let mut file_contents = String::new(); let mut file_contents = String::new();
buffer.read_to_string(&mut file_contents)?; render_error!(hb, buffer.read_to_string(&mut file_contents));
info!("read to buffer"); info!("read to buffer");
let data = json!({ let data = json!({
"replayid": id, "replayid": id,
}); });
let body = hb.render("replay", &data).unwrap(); let body = render_error!(hb, hb.render("replay", &data));
Ok(HttpResponse::Ok().body(body)) Ok(HttpResponse::Ok().body(body))
} }

@ -1,13 +1,10 @@
use actix_web::{get, web, HttpResponse}; use actix_web::{Error, get, web, HttpResponse};
use handlebars::Handlebars; use handlebars::Handlebars;
use serde_json::json; use serde_json::Value;
#[get("/submit")] use crate::render_error;
pub async fn get(hb: web::Data<Handlebars<'_>>) -> HttpResponse {
let data = json!({
"name": "rewot"
});
let body = hb.render("submit", &data).unwrap();
HttpResponse::Ok().body(body) #[get("/submit")]
pub async fn get(hb: web::Data<Handlebars<'_>>) -> Result<HttpResponse, Error> {
Ok(HttpResponse::Ok().body(render_error!(hb, hb.render("submit", &Value::Null))))
} }

@ -2,7 +2,7 @@ use actix_multipart::Multipart;
use actix_web::{post, web, Error, HttpResponse}; use actix_web::{post, web, Error, HttpResponse};
use handlebars::Handlebars; use handlebars::Handlebars;
use serde_json::{json, to_string_pretty}; use serde_json::to_string_pretty;
use crate::render_error; use crate::render_error;
use crate::WotLog; use crate::WotLog;
@ -17,7 +17,7 @@ pub async fn post(
render_error!(hb, wotlog.parse_log()); render_error!(hb, wotlog.parse_log());
render_error!(hb, wotlog.save()); render_error!(hb, wotlog.save());
let body = format!("{}", render_error!(hb, to_string_pretty(&wotlog.json))); let body = render_error!(hb, to_string_pretty(&wotlog.json));
Ok(HttpResponse::Ok().body(body)) Ok(HttpResponse::Ok().body(body))
} }

Loading…
Cancel
Save