Skip to the content.

Vetis Smol

Smol runtime support for Vetis HTTP server.

Installation

vetis = { version = "0.1.4-beta.23" }
vetis-smol = { version = "0.1.0-beta.12", features = ["http2", "rust-tls"] }

Usage

use http::Version;
use hyper::StatusCode;
use macro_rules_attribute::apply;
use smol_macros::main;
use vetis::{
    listener::ListenerConfig,
    security::SecurityConfig,
    server::{ServerConfig},
    host::{handler_fn, HostConfig},
    VetisServer as _
};
use vetis_smol::{
    host::{path::HandlerPath, Host},
    listener::build_listeners,
    rt::Vetis,
};

pub(crate) const CA_CERT: &[u8] = include_bytes!("../../certs/ca.der");
pub(crate) const SERVER_CERT: &[u8] = include_bytes!("../../certs/server.der");
pub(crate) const SERVER_KEY: &[u8] = include_bytes!("../../certs/server.key.der");

#[apply(main!)]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    env_logger::Builder::from_env(env_logger::Env::default().filter_or("RUST_LOG", "error")).init();

    let https = ListenerConfig::builder()
        .port(8443)
        .protocol_version(Version::HTTP_2)
        .interface("0.0.0.0".parse().unwrap())
        .build()?;

    let security_config = SecurityConfig::builder()
        .ca_cert_from_bytes(CA_CERT.to_vec())
        .cert_from_bytes(SERVER_CERT.to_vec())
        .key_from_bytes(SERVER_KEY.to_vec())
        .build()?;

    let localhost_config = HostConfig::builder()
        .hostname("localhost")
        .security(security_config)
        .root_directory("/home/rogerio/Downloads".into())
        .bind_addresses(vec![(
            "0.0.0.0"
                .parse()
                .unwrap(),
            8443,
        )])
        .build()?;

    let mut localhost_host = Host::new(localhost_config);

    let root_path = HandlerPath::builder()
        .uri("/hello")
        .handler(handler_fn(|request| async move {
            let response = vetis::Response::builder()
                .status(StatusCode::OK)
                .text("Hello from localhost");
            Ok(response)
        }))
        .build()?;

    localhost_host.add_path(root_path);

    let health_path = HandlerPath::builder()
        .uri("/health")
        .handler(handler_fn(|request| async move {
            let response = vetis::Response::builder()
                .status(StatusCode::OK)
                .text("Health check");
            Ok(response)
        }))
        .build()?;

    localhost_host.add_path(health_path);

    let mut server = Vetis::builder()
        .add_listeners(build_listeners(https))?
        .add_host(localhost_host)?
        .build();

    server.run().await?;

    server
        .stop()
        .await?;

    Ok(())
}

API Reference

For detailed API documentation, see the docs.rs page.