2024-06-20 13:13:26 +02:00
|
|
|
use axum::extract::Path;
|
2024-06-16 12:54:53 +02:00
|
|
|
use axum::http::StatusCode;
|
2024-06-20 13:13:26 +02:00
|
|
|
use axum::response::{IntoResponse, Response};
|
2024-06-13 14:42:42 +02:00
|
|
|
|
2024-06-20 13:13:26 +02:00
|
|
|
use crate::expressions::expression::Expression;
|
2024-06-17 12:18:53 +02:00
|
|
|
use crate::router;
|
2024-06-20 13:13:26 +02:00
|
|
|
use crate::routing::error::{Error, ErrorKind};
|
|
|
|
use crate::routing::response::IsLegalResponse;
|
|
|
|
use crate::utils::axum::load_html;
|
2024-06-17 12:18:53 +02:00
|
|
|
|
|
|
|
router!(
|
|
|
|
get "/" => index,
|
2024-06-20 13:13:26 +02:00
|
|
|
get "/openapi" => open_api,
|
|
|
|
get "/is-valid/:exp" => is_valid
|
2024-06-17 12:18:53 +02:00
|
|
|
);
|
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
|
|
|
|
|
|
|
async fn open_api() -> Response {
|
2024-06-20 13:13:26 +02:00
|
|
|
match load_html("openapi.html").await {
|
|
|
|
Ok(html) => html.into_response(),
|
|
|
|
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, err).into_response()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn is_valid(Path(path): Path<String>) -> Response {
|
|
|
|
match Expression::try_from(path.as_str()) {
|
|
|
|
Ok(_) => IsLegalResponse { is_legal: true }.into_response(),
|
|
|
|
Err(error) => Error::new(error.to_string(), ErrorKind::InvalidExpression).into_response()
|
|
|
|
}
|
|
|
|
}
|
2024-06-16 01:24:29 +02:00
|
|
|
|
2024-06-20 13:13:26 +02:00
|
|
|
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()
|
|
|
|
}
|
2024-06-16 01:24:29 +02:00
|
|
|
}
|