Logging. CORS(*). Version in response and error type for JSON response
This commit is contained in:
22
src/main.rs
22
src/main.rs
@ -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();
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
@ -9,5 +9,5 @@ pub fn router() -> Router<()> {
|
||||
}
|
||||
|
||||
async fn table() {
|
||||
unimplemented!("Not yet implemented")
|
||||
unimplemented!()
|
||||
}
|
||||
|
Reference in New Issue
Block a user