34 lines
875 B
Rust
Raw Normal View History

2024-06-16 01:24:29 +02:00
use axum::body::Body;
2024-06-16 12:54:53 +02:00
use axum::http::StatusCode;
2024-06-16 01:24:29 +02:00
use axum::response::{Html, IntoResponse, Response};
use axum::Router;
use axum::routing::get;
2024-06-16 01:24:29 +02:00
use tokio::fs::File;
use tokio_util::io::ReaderStream;
pub fn router() -> Router {
Router::new()
.route("/", get(index))
2024-06-16 01:24:29 +02:00
.route("/openapi", get(open_api))
}
async fn index() -> &'static str {
"Welcome to the Simplify Truths API!\n"
}
2024-06-16 01:24:29 +02:00
async fn open_api() -> Response {
2024-06-16 12:54:53 +02:00
let file_path = if cfg!(debug_assertions) {
"./spec/dist/index.html"
} else {
"./openapi/index.html"
};
2024-06-16 01:24:29 +02:00
let file = match File::open(file_path).await {
Ok(file) => file,
Err(err) => return (StatusCode::NOT_FOUND, format!("File not found: {err}")).into_response(),
};
let stream = ReaderStream::new(file);
let body = Body::from_stream(stream);
Html(body).into_response()
}