Show openapi docs in release mode

This commit is contained in:
Martin Berg Alstad
2024-06-16 12:54:53 +02:00
parent 4d953178e3
commit 5dc4a8e429
3 changed files with 18 additions and 6 deletions

View File

@ -1,5 +1,5 @@
use axum::body::Body;
use axum::http::{header, StatusCode};
use axum::http::StatusCode;
use axum::response::{Html, IntoResponse, Response};
use axum::Router;
use axum::routing::get;
@ -16,16 +16,17 @@ async fn index() -> &'static str {
"Welcome to the Simplify Truths API!\n"
}
// TODO open from target dir in release mode.
async fn open_api() -> Response {
let file_path = "./spec/dist/index.html";
let file_path = if cfg!(debug_assertions) {
"./spec/dist/index.html"
} else {
"./openapi/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()