Heavy refactor and extraction of most code to handler submodules
This commit is contained in:
parent
7d87046166
commit
ab6e90b6b8
9 changed files with 313 additions and 277 deletions
54
src/handlers/fixed.rs
Normal file
54
src/handlers/fixed.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use axum::{
|
||||
body::Body,
|
||||
http::{Response, StatusCode, header, HeaderValue},
|
||||
};
|
||||
|
||||
use crate::formats::{Format, populate_graph, serialize_graph};
|
||||
use crate::handlers;
|
||||
|
||||
pub async fn file(file_path: &str, content_type: &str) -> Response<Body> {
|
||||
let content = match std::fs::read(file_path) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
panic!("[file_handler] Failed to read file contents: {e}")
|
||||
},
|
||||
};
|
||||
|
||||
let mut response = Response::new(Body::from(content));
|
||||
*response.status_mut() = StatusCode::OK;
|
||||
let header = header::CONTENT_TYPE;
|
||||
|
||||
if let Ok(header_value) = HeaderValue::from_str(content_type) {
|
||||
if let Some(h) = response.headers_mut().insert(header, header_value) {
|
||||
eprintln!(
|
||||
"[file_handler] Overwrote existing header {h:?} \
|
||||
because a header for the same key existed"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
eprintln!(
|
||||
"[file_handler] Failed to create content type \
|
||||
header value from {content_type}"
|
||||
);
|
||||
}
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
pub async fn serial(format: &Format) -> Response<Body> {
|
||||
let graph = populate_graph();
|
||||
let body = serialize_graph(format, &graph);
|
||||
|
||||
match *format {
|
||||
Format::Toml => handlers::raw::make_response(
|
||||
&body,
|
||||
200,
|
||||
&[(header::CONTENT_TYPE, "text/plain")],
|
||||
),
|
||||
Format::Json => handlers::raw::make_response(
|
||||
&body,
|
||||
200,
|
||||
&[(header::CONTENT_TYPE, "application/json")],
|
||||
),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue