2024-06-16 01:24:29 +02:00
|
|
|
use axum::body::Body;
|
|
|
|
use axum::http::{header, StatusCode};
|
|
|
|
use axum::response::{Html, IntoResponse, Response};
|
2024-06-13 14:42:42 +02:00
|
|
|
use axum::Router;
|
|
|
|
use axum::routing::get;
|
2024-06-16 01:24:29 +02:00
|
|
|
use tokio::fs::File;
|
|
|
|
use tokio_util::io::ReaderStream;
|
2024-06-13 14:42:42 +02:00
|
|
|
|
|
|
|
pub fn router() -> Router {
|
|
|
|
Router::new()
|
|
|
|
.route("/", get(index))
|
2024-06-16 01:24:29 +02:00
|
|
|
.route("/openapi", get(open_api))
|
2024-06-13 14:42:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn index() -> &'static str {
|
|
|
|
"Welcome to the Simplify Truths API!\n"
|
|
|
|
}
|
2024-06-16 01:24:29 +02:00
|
|
|
|
|
|
|
// TODO open from target dir in release mode.
|
|
|
|
async fn open_api() -> Response {
|
|
|
|
let file_path = "./spec/dist/index.html";
|
|
|
|
let file = match File::open(file_path).await {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(err) => return (StatusCode::NOT_FOUND, format!("File not found: {err}")).into_response(),
|
|
|
|
};
|
|
|
|
// convert the `AsyncRead` into a `Stream`
|
|
|
|
let stream = ReaderStream::new(file);
|
|
|
|
// convert the `Stream` into an `axum::body::HttpBody`
|
|
|
|
let body = Body::from_stream(stream);
|
|
|
|
|
|
|
|
Html(body).into_response()
|
|
|
|
}
|