Start extracting web interface from implementation

This commit is contained in:
Lucien Greathouse
2019-09-03 13:57:28 -07:00
parent f1729163cf
commit a31bfbefa7
6 changed files with 68 additions and 53 deletions

20
src/web/util.rs Normal file
View File

@@ -0,0 +1,20 @@
use hyper::{header::CONTENT_TYPE, Body, Response, StatusCode};
use serde::Serialize;
pub fn response_json<T: Serialize>(value: T) -> Response<Body> {
let serialized = match serde_json::to_string(&value) {
Ok(v) => v,
Err(err) => {
return Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.header(CONTENT_TYPE, "text/plain")
.body(Body::from(err.to_string()))
.unwrap();
}
};
Response::builder()
.header(CONTENT_TYPE, "application/json")
.body(Body::from(serialized))
.unwrap()
}