diff --git a/.dockerignore b/.dockerignore index 8ef0081..075760b 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,5 @@ .github -spec +spec/node_modules http target README.md diff --git a/Dockerfile b/Dockerfile index 531d6de..6a50398 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,10 +19,21 @@ COPY ./src ./src RUN rm ./target/release/deps/simplify_truths* RUN cargo build --release +FROM node:20.14.0 as spec + +WORKDIR /spec + +COPY ./spec . +RUN npm install +RUN USER=root npm install -g @typespec/compiler && npm install -g @redocly/cli +RUN npm run tsp-compile && npm run redoc-build + FROM debian # copy the build artifact from the build stage COPY --from=build /simplify_truths/target/release/simplify_truths . +# copy the generated html file for REDOC documentation +COPY --from=spec /spec/dist/index.html ./openapi/index.html EXPOSE 8000 diff --git a/src/routing/index.rs b/src/routing/index.rs index ec1f560..259f143 100644 --- a/src/routing/index.rs +++ b/src/routing/index.rs @@ -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()