pub async fn start_server(
state: Arc<ServerState>,
listen_addr: &str,
port: u16,
cert_path: Option<&str>,
key_path: Option<&str>,
) -> Result<(), Error>Expand description
Start the HTTP or HTTPS server.
Builds the router via routes::build_router, wraps it with the
request-logging middleware, binds the listener at
<listen_addr>:<port>, and serves until a SIGTERM or Ctrl+C is
received — at which point the in-process shutdown handler
triggers axum_server’s graceful drain with the
ServerState::shutdown_grace_period window.
When cert_path and key_path are both Some, the server
listens with TLS (https://). When both are None, it listens
as plain HTTP. Mixing one of the two is rejected.
§Errors
- [
Error::BadRequest] whenlisten_addr:portdoes not parse as aSocketAddr, or when exactly one ofcert_path/key_pathis supplied (they must be set together). - Any I/O / TLS load error from
RustlsConfig::from_pem_fileor the underlyingaxum_server::bind*call surfaces via theFrom<io::Error>impl on [Error].