Skip to main content

Crate vetis_tokio

Crate vetis_tokio 

Source
Expand description

§VeTiS-Tokio (Very Tiny Server - Tokio runtime support)

Crates.io downloads crates.io Build Status Crates.io MSRV Documentation MIT licensed codecov

§Tokio Runtime Support for Vetis

The goal of this crate is provide tokio runtime support for Vetis.

§Quick Start

Add VeTiS and VeTiS-Tokio to your Cargo.toml:

vetis = { version = "0.1.4-beta.7" }
vetis-tokio = { version = "0.1.0-beta.2", features = ["http2", "rust-tls"] }

§Crate features

  • http1
  • http2 (default)
  • http3
  • rust-tls (default)

§Usage Example

Here’s how simple it is to create a web server with VeTiS:

use http::{StatusCode, Version};
use vetis::{
    listener::ListenerConfig,
    security::SecurityConfig,
    server::{ServerConfig},
    host::{handler_fn, HostConfig},
    VetisServer as _
};
use vetis_tokio::{
    host::{path::HandlerPath, HostImpl},
    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");

#[tokio::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)
        .protos(vec![Version::HTTP_11])
        .interface("0.0.0.0".parse().unwrap())
        .build()?;

    let config = ServerConfig::builder()
        .add_listener(https)
        .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)
        .build()?;

    let mut localhost_host = HostImpl::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::new(config);
    server
        .add_host(localhost_host)
        .await;

    server.run().await?;

    server
        .stop()
        .await?;

    Ok(())
}

§License

MIT

§Author

Rogerio Pereira Araujo rogerio.araujo@gmail.com

Re-exports§

pub use crate::rt::Vetis;

Modules§

errors
Error handling module Error handling types for VeTiS.
host
Host module Virtual host module
listener
Listener module
rt
Runtime module

Structs§

HostConfig
Configuration for a virtual host.
ListenerConfig
Configuration for a server listener.
Request
HTTP request wrapper supporting multiple protocols.
Response
HTTP response containing status, headers, and body.
SecurityConfig
Security configuration for TLS/SSL.
ServerConfig
Global server configuration.

Traits§

VetisServer
Base trait for Vetis server

Functions§

handler_fn
Creates a handler function from a function.

Type Aliases§

VetisHosts
A type alias for a vector of virtual hosts
VetisRwLock
A type alias for a read-write lock wrapping a value