From 9109b4b455627934f6afd5c533c0433d5d58577c Mon Sep 17 00:00:00 2001 From: Paolo Rechia Date: Wed, 10 Jan 2024 21:47:05 +0100 Subject: [PATCH] Migrate to a high level http framework --- .gitignore | 4 ++- steeldb-server/Cargo.toml | 6 ++-- steeldb-server/src/main.rs | 73 ++++++++++++-------------------------- 3 files changed, 27 insertions(+), 56 deletions(-) diff --git a/.gitignore b/.gitignore index 89ca511..8d47263 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,6 @@ Cargo.lock calculator # SteelDB Data -.steeldb/data \ No newline at end of file +.steeldb/data + +durp diff --git a/steeldb-server/Cargo.toml b/steeldb-server/Cargo.toml index d517f97..8280946 100644 --- a/steeldb-server/Cargo.toml +++ b/steeldb-server/Cargo.toml @@ -6,10 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -hyper = { version = "1", features = ["full"]} -tokio = { version = "1", features = ["full"]} -http-body-util = {version = "0.1"} -hyper-util = { version = "0.1", features = ["full"]} steeldb-core = { path = "../steeldb-core", features = ["json"]} steeldb = { path = ".."} serde_json = "1.0.111" +axum = "0.7.3" +tokio = { version = "1.35.1", features = ["macros", "rt-multi-thread"] } diff --git a/steeldb-server/src/main.rs b/steeldb-server/src/main.rs index 5c4ac05..63d7b6c 100644 --- a/steeldb-server/src/main.rs +++ b/steeldb-server/src/main.rs @@ -1,65 +1,36 @@ -use steeldb_core::json_result::TableJSON; +use std::collections::HashMap; use steeldb::SteelDB; +use steeldb_core::json_result::TableJSON; use steeldb_core::SteelDBInterface; -use std::collections::HashMap; -use std::convert::Infallible; -use std::net::SocketAddr; +use axum::{http::StatusCode, routing::post, Json, Router}; -use http_body_util::Full; -use hyper::body::Bytes; -use hyper::server::conn::http1; -use hyper::service::service_fn; -use hyper::{Request, Response}; -use hyper_util::rt::TokioIo; -use tokio::net::TcpListener; +#[tokio::main] +async fn main() { + // build our application with a route + let app = Router::new() + // `GET /` goes to `root` + .route("/query", post(handle_query)); + + // run our app with hyper, listening globally on port 3000 + let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); + axum::serve(listener, app).await.unwrap(); +} -async fn hello(_: Request) -> Result>, Infallible> { +async fn handle_query(// this argument tells axum to parse the request body + // as JSON into a `CreateUser` type + // Json(payload): Json, +) -> (StatusCode, Json) { + // insert your application logic here let hello_response = TableJSON { table_name: "world!".to_owned(), columns: HashMap::new(), select_columns: Vec::new(), }; - // TODO: create database ref in main function instead let mut database = SteelDB::new(); let result = database.execute("select name;".to_owned()); - println!("{:?}", result); - - let desserialized = serde_json::to_string(&hello_response).unwrap(); - let response = Response::builder() - .header("Content-Type", "application/json") - .header("content-length", desserialized.len()) - .body(desserialized.into()) - .unwrap(); - - Ok(response) -} - -#[tokio::main] -async fn main() -> Result<(), Box> { - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); - - // We create a TcpListener and bind it to 127.0.0.1:3000 - let listener = TcpListener::bind(addr).await?; - - // We start a loop to continuously accept incoming connections - loop { - let (stream, _) = listener.accept().await?; - - // Use an adapter to access something implementing `tokio::io` traits as if they implement - // `hyper::rt` IO traits. - let io = TokioIo::new(stream); - // Spawn a tokio task to serve multiple connections concurrently - tokio::task::spawn(async move { - // Finally, we bind the incoming connection to our `hello` service - if let Err(err) = http1::Builder::new() - // `service_fn` converts our function in a `Service` - .serve_connection(io, service_fn(hello)) - .await - { - println!("Error serving connection: {:?}", err); - } - }); - } + // this will be converted into a JSON response + // with a status code of `201 Created` + (StatusCode::CREATED, Json(hello_response)) }