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>
pub fn serve<S>(self, service: S) -> Result<(), Error>
Sourcepub fn serve_single_thread<S>(self, service: S) -> Result<(), Error>
pub fn serve_single_thread<S>(self, service: S) -> Result<(), Error>
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>
pub fn make_service<M>(self, make_service: M) -> Result<(), Error>
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> Freeze for Server<'a>
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