load html on compile time instead of runtime.

Maybe fixed not found link to refer to correct url in release
This commit is contained in:
Martin Berg Alstad 2024-06-20 18:00:12 +02:00
parent bfd8053c15
commit ede5eedc24
4 changed files with 37 additions and 11 deletions

View File

@ -2,6 +2,10 @@
GET {{url}}
### GET index page on HTTP/2
GET {{url}} HTTP/2
### GET OpenAPI page
GET {{url}}/openapi

View File

@ -54,7 +54,7 @@
<div class="container">
<h1>404</h1>
<p>Oops! Page not found.</p>
<a href="http://localhost:8000/openapi">Go back to the documentation</a>
<a href="{{docs}}">Go back to the documentation</a>
</div>
</body>
</html>

View File

@ -2,11 +2,10 @@ use axum::extract::Path;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use crate::{load_html, router};
use crate::expressions::expression::Expression;
use crate::router;
use crate::routing::error::{Error, ErrorKind};
use crate::routing::response::IsLegalResponse;
use crate::utils::axum::load_html;
router!(
get "/" => index,
@ -19,10 +18,7 @@ async fn index() -> &'static str {
}
async fn open_api() -> Response {
match load_html("openapi.html").await {
Ok(html) => html.into_response(),
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, err).into_response()
}
load_html!("openapi.html").into_response()
}
async fn is_valid(Path(path): Path<String>) -> Response {
@ -33,8 +29,5 @@ async fn is_valid(Path(path): Path<String>) -> Response {
}
pub(crate) async fn not_found() -> Response {
match load_html("not-found.html").await {
Ok(html) => (StatusCode::NOT_FOUND, html).into_response(),
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, err).into_response()
}
(StatusCode::NOT_FOUND, load_html!("not-found.html")).into_response()
}

View File

@ -65,3 +65,32 @@ pub async fn load_html(file_path: &str) -> Result<Html<Body>, String> {
let stream = ReaderStream::new(file);
Ok(Html(Body::from_stream(stream)))
}
#[macro_export]
macro_rules! load_html {
($filename:literal) => {
axum::response::Html(
axum::body::Body::new(
$crate::absolute_path!($filename)
)
)
};
}
#[macro_export]
#[cfg(debug_assertions)]
macro_rules! absolute_path {
($filename:literal) => {
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/resources/static/", $filename))
.replace("{{docs}}", "/openapi")
};
}
#[macro_export]
#[cfg(not(debug_assertions))]
macro_rules! absolute_path {
($filename:literal) => {
include_str!(concat!("/static/", $filename))
.replace("{{docs}}", "simplify-truths/v2/openapi")
};
}