pub struct Server<'a> { /* private fields */ }
Expand description
A listening HTTP server that accepts HTTP 1 connections.
Implementations§
source§impl<'a> Server<'a>
impl<'a> Server<'a>
sourcepub fn builder() -> ServerBuilder
pub fn builder() -> ServerBuilder
Starts the ServerBuilder
.
sourcepub fn bind<A>(addr: A) -> Server<'static>where
A: ToSocketAddrs,
pub fn bind<A>(addr: A) -> Server<'static>where A: ToSocketAddrs,
Binds the Server
to the given addr
.
Panics
This method will panic if binding to the address fails. For a non panic method to bind the
server, see ServerBuilder::try_bind
.
sourcepub fn serve<S>(self, service: S) -> Result<(), Error>where
S: Service + Send + Clone + 'static,
pub fn serve<S>(self, service: S) -> Result<(), Error>where S: Service + Send + Clone + 'static,
sourcepub fn serve_single_thread<S>(self, service: S) -> Result<(), Error>where
S: Service + Clone,
pub fn serve_single_thread<S>(self, service: S) -> Result<(), Error>where S: Service + Clone,
Serves an Service
on a single thread. This is useful when your Service
is not
Send
. Note that if a connection is kept alive on this mode, no other request may be
served before the said connection is closed.
Example
Server::bind("0.0.0.0:4444").serve_single_thread(|req: Request<_>| {
Response::builder()
.status(StatusCode::OK)
.body(req.into_body())
})
sourcepub fn make_service<M>(self, make_service: M) -> Result<(), Error>where
M: MakeService + Clone + 'static,
<M as MakeService>::Service: Send,
pub fn make_service<M>(self, make_service: M) -> Result<(), Error>where M: MakeService + Clone + 'static, <M as MakeService>::Service: Send,
Serves an Connection
. This should be used when you need to execute some logic on every
connection.
Example
Server::builder()
.bind("0.0.0.0:4444")
.make_service(|conn: &Connection| {
println!("New connection arrived: {:?}", conn.peer_addr());
Ok::<_, Infallible>(|_req| {
Response::builder()
.status(StatusCode::OK)
.body(())
})
})
Trait Implementations§
source§impl From<TcpListener> for Server<'static>
impl From<TcpListener> for Server<'static>
source§fn from(listener: TcpListener) -> Server<'static>
fn from(listener: TcpListener) -> Server<'static>
Converts to this type from the input type.
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for Server<'a>
impl<'a> !Send for Server<'a>
impl<'a> !Sync for Server<'a>
impl<'a> Unpin for Server<'a>
impl<'a> !UnwindSafe for Server<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more