Server

Struct Server 

Source
pub struct Server<Addr: Address, A = DefaultAcceptor> { /* private fields */ }
Expand description

HTTP server.

Implementations§

Source§

impl<A: Address> Server<A>

Source

pub fn bind(addr: A) -> Self

Create a server that will bind to provided address.

Source

pub fn from_listener(listener: A::Listener) -> Self

Create a server from an existing AddrListener.

Source§

impl<A: Address, Acc> Server<A, Acc>

Source

pub fn acceptor<Acceptor>(self, acceptor: Acceptor) -> Server<A, Acceptor>

Overwrite acceptor.

Examples found in repository?
examples/rustls_session.rs (line 32)
17async fn main() {
18    let app = Router::new().route("/", get(handler));
19
20    let config = RustlsConfig::from_pem_file(
21        "examples/self-signed-certs/cert.pem",
22        "examples/self-signed-certs/key.pem",
23    )
24    .await
25    .unwrap();
26
27    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
28
29    println!("listening on {}", addr);
30
31    let acceptor = CustomAcceptor::new(RustlsAcceptor::new(config));
32    let server = axum_server::bind(addr).acceptor(acceptor);
33
34    server.serve(app.into_make_service()).await.unwrap();
35}
Source

pub fn map<Acceptor, F>(self, acceptor: F) -> Server<A, Acceptor>
where F: FnOnce(Acc) -> Acceptor,

Map acceptor.

Source

pub fn get_ref(&self) -> &Acc

Returns a reference to the acceptor.

Source

pub fn get_mut(&mut self) -> &mut Acc

Returns a mutable reference to the acceptor.

Source

pub fn http_builder(&mut self) -> &mut Builder<TokioExecutor>

Returns a mutable reference to the Http builder.

Source

pub fn http1_only(self) -> Self

Only accepts HTTP/1

Source

pub fn http2_only(self) -> Self

Only accepts HTTP/2

Source

pub fn handle(self, handle: Handle<A>) -> Self

Provide a handle for additional utilities.

Examples found in repository?
examples/shutdown.rs (line 24)
13async fn main() {
14    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
15
16    let handle = Handle::new();
17
18    // Spawn a task to shutdown server.
19    tokio::spawn(shutdown(handle.clone()));
20
21    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
22    println!("listening on {}", addr);
23    axum_server::bind(addr)
24        .handle(handle)
25        .serve(app.into_make_service())
26        .await
27        .unwrap();
28
29    println!("server is shut down");
30}
More examples
Hide additional examples
examples/graceful_shutdown.rs (line 27)
16async fn main() {
17    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
18
19    let handle = Handle::new();
20
21    // Spawn a task to gracefully shutdown server.
22    tokio::spawn(graceful_shutdown(handle.clone()));
23
24    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
25    println!("listening on {}", addr);
26    axum_server::bind(addr)
27        .handle(handle)
28        .serve(app.into_make_service())
29        .await
30        .unwrap();
31
32    println!("server is shut down");
33}
Source

pub async fn serve<M>(self, make_service: M) -> Result<()>
where M: MakeService<A, Request<Incoming>>, A: Send + 'static, A::Stream: Send, Acc: Accept<A::Stream, M::Service> + Clone + Send + Sync + 'static, Acc::Stream: AsyncRead + AsyncWrite + Unpin + Send, Acc::Service: SendService<Request<Incoming>> + Send, Acc::Future: Send,

Serve provided MakeService.

To create MakeService easily, Shared from tower can be used.

§Errors

An error will be returned when:

  • Binding to an address fails.
  • make_service returns an error when poll_ready is called. This never happens on axum make services.
Examples found in repository?
examples/multiple_addresses.rs (line 22)
18async fn start_server(addr: SocketAddr) {
19    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
20
21    axum_server::bind(addr)
22        .serve(app.into_make_service())
23        .await
24        .unwrap();
25}
More examples
Hide additional examples
examples/remote_address.rs (line 16)
9async fn main() {
10    let app = Router::new()
11        .route("/", get(handler))
12        .into_make_service_with_connect_info::<SocketAddr>();
13
14    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
15
16    axum_server::bind(addr).serve(app).await.unwrap();
17}
examples/http_and_https.rs (line 25)
19async fn http_server() {
20    let app = Router::new().route("/", get(http_handler));
21
22    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
23    println!("http listening on {}", addr);
24    axum_server::bind(addr)
25        .serve(app.into_make_service())
26        .await
27        .unwrap();
28}
29
30async fn http_handler(uri: Uri) -> Redirect {
31    let uri = format!("https://127.0.0.1:3443{}", uri.path());
32
33    Redirect::temporary(&uri)
34}
35
36async fn https_server() {
37    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
38
39    let config = RustlsConfig::from_pem_file(
40        "examples/self-signed-certs/cert.pem",
41        "examples/self-signed-certs/key.pem",
42    )
43    .await
44    .unwrap();
45
46    let addr = SocketAddr::from(([127, 0, 0, 1], 3443));
47    println!("https listening on {}", addr);
48    axum_server::bind_rustls(addr, config)
49        .serve(app.into_make_service())
50        .await
51        .unwrap();
52}
examples/hello_world.rs (line 15)
9async fn main() {
10    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
11
12    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
13    println!("listening on {}", addr);
14    axum_server::bind(addr)
15        .serve(app.into_make_service())
16        .await
17        .unwrap();
18}
examples/hello_world_unix.rs (line 15)
9async fn main() {
10    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
11
12    let addr = SocketAddr::from_pathname("/tmp/axum-server.sock").unwrap();
13    println!("listening on {}", addr.as_pathname().unwrap().display());
14    axum_server::bind(addr)
15        .serve(app.into_make_service())
16        .await
17        .unwrap();
18}
examples/from_std_listener.rs (line 17)
9async fn main() {
10    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
11
12    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
13    let listener = TcpListener::bind(addr).unwrap();
14    println!("listening on {}", addr);
15    axum_server::from_tcp(listener)
16        .unwrap()
17        .serve(app.into_make_service())
18        .await
19        .unwrap();
20}

Trait Implementations§

Source§

impl<A: Address, B> Debug for Server<A, B>
where Listener<A>: Debug, Handle<A>: Debug, B: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Addr, A> Freeze for Server<Addr, A>
where A: Freeze, Addr: Freeze, <Addr as Address>::Listener: Freeze,

§

impl<Addr, A = DefaultAcceptor> !RefUnwindSafe for Server<Addr, A>

§

impl<Addr, A> Send for Server<Addr, A>
where A: Send, Addr: Send, <Addr as Address>::Listener: Send,

§

impl<Addr, A> Sync for Server<Addr, A>
where A: Sync, Addr: Sync + Send, <Addr as Address>::Listener: Sync,

§

impl<Addr, A> Unpin for Server<Addr, A>
where A: Unpin, Addr: Unpin, <Addr as Address>::Listener: Unpin,

§

impl<Addr, A = DefaultAcceptor> !UnwindSafe for Server<Addr, A>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more