Skip to main content

http

Function http 

Source
pub async fn http(
    router: impl Into<Router>,
    config: &Config,
) -> Result<HttpServer>
Expand description

Bind a TCP listener and start serving router.

router may be any value that implements Into<axum::Router>, including axum::Router itself and HostRouter. The router is wrapped in a NormalizePathLayer so trailing slashes are trimmed before path matching (root / is preserved).

Returns an HttpServer handle immediately; the server runs on a background Tokio task. Pass the handle to crate::run! so it is shut down gracefully when a signal arrives.

§Errors

Returns crate::Error::internal if the TCP port cannot be bound (address already in use, permission denied, malformed host) or if the bound socket’s local address cannot be read.

§Example

use modo::server::{Config, http};

#[tokio::main]
async fn main() -> modo::Result<()> {
    let config = Config::default();
    let router = modo::axum::Router::new();
    let server = http(router, &config).await?;
    modo::run!(server).await
}

With a HostRouter:

use modo::server::{self, Config, HostRouter};

#[tokio::main]
async fn main() -> modo::Result<()> {
    let config = Config::default();
    let app = HostRouter::new()
        .host("acme.com", modo::axum::Router::new())
        .host("*.acme.com", modo::axum::Router::new());
    let server = server::http(app, &config).await?;
    modo::run!(server).await
}