hyper http server

This commit is contained in:
Thilo Behnke
2022-05-12 22:15:19 +02:00
parent c128908a8e
commit d6c9946f2b
4 changed files with 52 additions and 1 deletions

View File

@@ -5,7 +5,7 @@ authors = ["Thilo Behnke <thilo.behnke@gmx.net>"]
edition = "2018"
[workspace]
members = ["pong"]
members = ["pong", "server"]
[lib]
crate-type = ["cdylib", "rlib"]

12
server/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "server"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
kafka = { version = "0.8.0" }
hyper = {version = "0.14.18", features = ["full"]}
tokio = { version = "1", features = ["full"] }
tokio-stream = {version = "0.1" }

31
server/src/http.rs Normal file
View File

@@ -0,0 +1,31 @@
use std::convert::Infallible;
use hyper::{Body, Request, Response, Server};
use hyper::server::conn::AddrStream;
use hyper::service::{make_service_fn, service_fn};
pub struct HttpServer {
addr: [u8; 4],
port: u16
}
impl HttpServer {
pub fn new(addr: [u8; 4], port: u16) -> HttpServer {
HttpServer {addr, port}
}
pub async fn run(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let make_svc = make_service_fn(|socket: &AddrStream| async {
Ok::<_, Infallible>(service_fn(handle_request))
});
let host = (self.addr, self.port).into();
let server = Server::bind(&host).serve(make_svc);
println!("Listening on http://{}", host);
server.await?;
Ok(())
}
}
async fn handle_request(req: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new("hello".into()))
}

8
server/src/main.rs Normal file
View File

@@ -0,0 +1,8 @@
use crate::http::HttpServer;
mod http;
#[tokio::main]
pub async fn main() {
HttpServer::new([127, 0, 0, 1], 4000).run().await.expect("failed to run server");
}