Crate axum_server[][src]

Expand description

axum-server is a hyper server implementation designed to be used with axum framework.

Features

  • Conveniently bind to any number of addresses.
  • Tls support through rustls. Only pem format is supported.
  • Reload tls while server is running.
  • Access to client ip address from services/handlers.
  • Record incoming and outgoing bytes for each connection.
  • Services created by axum can directly be served.
  • Although designed to be used with axum, any Service that implements Clone can be served.

Guide

Server can be created using Server::new or bind, pick whichever you are comfortable with.

To serve an app, server must at least bind to an address:port. Anything that implements ToSocketAddrs can be used with bind function for example "127.0.0.1:3000". After binding at least to one address, a Service that implements Clone can be provided to Server::serve method which can then be .awaited to run the server. This means all axum services can be served.

In Request extensions, SocketAddr type can be used to get client ip address.

When tls-rustls feature is enabled, Server can be turned into a TlsServer by calling methods about tls. Addresses defined using bind will be served with HTTP protocol and addresses defined using bind_rustls with HTTPS protocol.

When record feature is enabled, serve_and_record method can be used to record incoming and outgoing bytes. See module page for getting recorded bytes.

Examples

Hello World

axum “Hello, World!” example can be run like:

use axum::{
    handler::get,
    Router,
};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(|| async { "Hello, World!" }));

    axum_server::bind("127.0.0.1:3000")
        .serve(app)
        .await
        .unwrap();
}

Remote Address

use axum::{extract::Extension, handler::get, Router};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(handler));

    axum_server::bind("127.0.0.1:3000")
        .serve(app)
        .await
        .unwrap();
}

async fn handler(Extension(addr): Extension<SocketAddr>) -> String {
    format!("addr: {}", addr)
}

Modules

recordrecord

Recording utilities for servers.

tlstls-rustls

Tls implementation for HTTP server.

Structs

A struct that can be passed to a server for additional utilites.

Configurable HTTP server, supporting HTTP/1.1 and HTTP2.

Functions

Shortcut for creating Server and calling bind on it.

bind_rustlstls-rustls

Shortcut for creating Server and calling bind_rustls on it.