Host OpenAPI Redoc page in dev mode

This commit is contained in:
Martin Berg Alstad 2024-06-16 01:24:29 +02:00
parent ee4523207b
commit 4d953178e3
4 changed files with 51 additions and 1 deletions

20
Cargo.lock generated
View File

@ -152,6 +152,12 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
[[package]]
name = "futures-sink"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5"
[[package]]
name = "futures-task"
version = "0.3.30"
@ -525,6 +531,7 @@ dependencies = [
"nom",
"serde",
"tokio",
"tokio-util",
"tower-http",
"tracing",
"tracing-subscriber",
@ -606,6 +613,19 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-util"
version = "0.7.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1"
dependencies = [
"bytes",
"futures-core",
"futures-sink",
"pin-project-lite",
"tokio",
]
[[package]]
name = "tower"
version = "0.4.13"

View File

@ -8,7 +8,8 @@ authors = ["Martin Berg Alstad"]
# Parsing
nom = "7.1.3"
# Async
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] }
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread", "fs"] }
tokio-util = { version = "0.7.11", features = ["io"] }
# Serialization / Deserialization
serde = { version = "1.0.203", features = ["derive", "rc"] }
# API

View File

@ -1,3 +1,11 @@
### GET index page
GET {{url}}
### GET OpenAPI page
GET {{url}}/openapi
### GET Atomic Expression
GET {{url}}/simplify/A

View File

@ -1,11 +1,32 @@
use axum::body::Body;
use axum::http::{header, StatusCode};
use axum::response::{Html, IntoResponse, Response};
use axum::Router;
use axum::routing::get;
use tokio::fs::File;
use tokio_util::io::ReaderStream;
pub fn router() -> Router {
Router::new()
.route("/", get(index))
.route("/openapi", get(open_api))
}
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 = 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()
}