Logging. CORS(*). Version in response and error type for JSON response

This commit is contained in:
Martin Berg Alstad
2024-06-13 18:58:50 +02:00
parent c1b9273e0c
commit 2a826be8ea
7 changed files with 203 additions and 12 deletions

View File

@ -1,6 +1,10 @@
use std::net::SocketAddr;
use tokio::net::TcpListener;
use tower_http::cors::{Any, CorsLayer};
use tower_http::trace;
use tower_http::trace::TraceLayer;
use tracing::Level;
use crate::routing::{index, simplify, table};
@ -18,11 +22,25 @@ async fn main() {
.await
.unwrap();
println!("Listening on: {}", listener.local_addr().unwrap());
tracing_subscriber::fmt()
.with_target(false)
.compact()
.init();
let routes = simplify::router()
.merge(table::router())
.merge(index::router());
axum::serve(listener, routes).await.unwrap();
let app = routes
.layer(CorsLayer::new().allow_origin(Any))
.layer(TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new()
.level(Level::INFO))
.on_response(trace::DefaultOnResponse::new()
.level(Level::INFO))
);
tracing::info!("Starting server on: {addr}");
axum::serve(listener, app.into_make_service()).await.unwrap();
}

View File

@ -33,16 +33,34 @@ struct QueryOptions {
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SimplifyResponse {
enum Law {
// TODO
}
#[derive(Serialize)]
struct OrderOfOperation {
before: String,
after: String,
order_of_operations: Vec<String>,
law: Law, // TODO
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SimplifyResponse {
version: String, // TODO better versioning
before: String,
after: String,
order_of_operations: Vec<OrderOfOperation>,
expression: Expression,
#[serde(skip_serializing_if = "Option::is_none")]
truth_table: Option<TruthTable>,
}
#[derive(Serialize)]
struct Error {
message: String,
}
// TODO
async fn simplify(Path(path): Path<String>, query: Query<QueryOptions>, accept_language: Option<AcceptLanguage>) -> Response {
if let Ok(mut expression) = Expression::try_from(path.as_str()) {
@ -51,6 +69,7 @@ async fn simplify(Path(path): Path<String>, query: Query<QueryOptions>, accept_l
expression = expression.simplify();
}
Json(SimplifyResponse {
version: "2.0.0".to_string(),
before,
after: expression.to_string(),
order_of_operations: vec![], // TODO
@ -58,7 +77,7 @@ async fn simplify(Path(path): Path<String>, query: Query<QueryOptions>, accept_l
truth_table: None,
}).into_response()
} else {
(StatusCode::BAD_REQUEST, "Invalid expression").into_response()
(StatusCode::BAD_REQUEST, Json(Error { message: "Invalid expression".into() })).into_response()
}
}
@ -71,6 +90,7 @@ async fn simplify_and_table(Path(path): Path<String>, query: Query<QueryOptions>
// TODO options
let truth_table = TruthTable::new(&expression, TruthTableOptions::default());
Json(SimplifyResponse {
version: "2.0.0".to_string(),
before,
after: expression.to_string(),
order_of_operations: vec![], // TODO
@ -78,6 +98,6 @@ async fn simplify_and_table(Path(path): Path<String>, query: Query<QueryOptions>
truth_table: Some(truth_table),
}).into_response()
} else {
(StatusCode::BAD_REQUEST, "Invalid expression").into_response()
(StatusCode::BAD_REQUEST, Json(Error { message: "Invalid expression".into() })).into_response()
}
}

View File

@ -9,5 +9,5 @@ pub fn router() -> Router<()> {
}
async fn table() {
unimplemented!("Not yet implemented")
unimplemented!()
}