You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
There is no way to pass data to the new Server connection service in Hyper 1 #3463
In Hyper 0.14, you could create a service that passed data to the connection service. This example in the old documentation explained how:
use std::convert::Infallible;use std::net::SocketAddr;use hyper::{Body,Request,Response,Server};use hyper::service::{make_service_fn, service_fn};use hyper::server::conn::AddrStream;#[derive(Clone)]structAppContext{// Whatever data your application needs can go here}asyncfnhandle(context:AppContext,addr:SocketAddr,req:Request<Body>) -> Result<Response<Body>,Infallible>{Ok(Response::new(Body::from("Hello World")))}#[tokio::main]asyncfn main(){let context = AppContext{// ...};// A `MakeService` that produces a `Service` to handle each connection.let make_service = make_service_fn(move |conn:&AddrStream| {// We have to clone the context to share it with each invocation of// `make_service`. If your data doesn't implement `Clone` consider using// an `std::sync::Arc`.let context = context.clone();// You can grab the address of the incoming connection like so.let addr = conn.remote_addr();// Create a `Service` for responding to the request.let service = service_fn(move |req| {handle(context.clone(), addr, req)});// Return the service to hyper.asyncmove{Ok::<_,Infallible>(service)}});// Run the server like above...let addr = SocketAddr::from(([127,0,0,1],3000));let server = Server::bind(&addr).serve(make_service);ifletErr(e) = server.await{eprintln!("server error: {}", e);}
Activity