Skip to content

Commit

Permalink
Example serving JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
paolorechia committed Jan 10, 2024
1 parent efb4671 commit c65f340
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion steeldb-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ hyper = { version = "1", features = ["full"]}
tokio = { version = "1", features = ["full"]}
http-body-util = {version = "0.1"}
hyper-util = { version = "0.1", features = ["full"]}
steeldb = { path = ".."}
steeldb = { path = ".."}
serde = { version = "1.0.195", features = ["derive"]}
serde_json = "1.0.111"
18 changes: 17 additions & 1 deletion steeldb-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde::{Deserialize, Serialize};
use std::convert::Infallible;
use std::net::SocketAddr;

Expand All @@ -9,8 +10,23 @@ use hyper::{Request, Response};
use hyper_util::rt::TokioIo;
use tokio::net::TcpListener;

#[derive(Deserialize, Serialize, Debug)]
struct HelloJSON {
hello: String,
}

async fn hello(_: Request<hyper::body::Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
Ok(Response::new(Full::new(Bytes::from("Hello, World!"))))
let hello_response = HelloJSON {
hello: "world!".to_owned(),
};
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]
Expand Down

0 comments on commit c65f340

Please sign in to comment.