tako/lib.rs
1//! A lightweight and modular web framework for building async applications in Rust.
2//!
3//! Tako provides core components for routing, middleware, request handling, and response
4//! generation. The framework is designed around composable modules that can be mixed and
5//! matched based on application needs. Key types include `Router` for routing requests,
6//! various extractors for parsing request data, and responders for generating responses.
7//!
8//! # Examples
9//!
10//! ```rust
11//! use tako::{Method, router::Router, responder::Responder, types::Request};
12//!
13//! async fn hello(_: Request) -> impl Responder {
14//! "Hello, World!".into_response()
15//! }
16//!
17//! let mut router = Router::new();
18//! router.route(Method::GET, "/", hello);
19//! ```
20
21/// HTTP request and response body handling utilities.
22pub mod body;
23
24/// Byte stream and buffer manipulation utilities.
25pub mod bytes;
26
27/// HTTP client implementation for making outbound requests.
28#[cfg(feature = "client")]
29pub mod client;
30
31/// Request data extraction utilities for parsing query params, JSON, and more.
32pub mod extractors;
33
34/// File streaming utilities for serving files.
35#[cfg(feature = "file-stream")]
36pub mod file_stream;
37
38/// Request handler traits and implementations.
39mod handler;
40
41/// Middleware for processing requests and responses in a pipeline.
42pub mod middleware;
43
44/// Plugin system for extending framework functionality.
45#[cfg(feature = "plugins")]
46pub mod plugins;
47
48/// Response generation utilities and traits.
49pub mod responder;
50
51/// Route definition and matching logic.
52mod route;
53
54/// Request routing and dispatch functionality.
55pub mod router;
56
57/// HTTP server implementation and configuration.
58mod server;
59
60/// Server-Sent Events (SSE) support for real-time communication.
61pub mod sse;
62
63/// Application state management and dependency injection.
64pub mod state;
65
66/// Static file serving utilities.
67pub mod r#static;
68
69/// Distributed tracing integration for observability.
70#[cfg(feature = "tako-tracing")]
71pub mod tracing;
72
73/// Core type definitions used throughout the framework.
74pub mod types;
75
76/// WebSocket connection handling and message processing.
77pub mod ws;
78
79/// HTTP method enumeration re-exported from hyper.
80pub use hyper::Method;
81
82/// Starts the HTTP server with the given listener and router.
83///
84/// This is the main entry point for starting a Tako web server. The function takes
85/// ownership of a TCP listener and router, then serves incoming connections until
86/// the server is shut down.
87///
88/// # Examples
89///
90/// ```rust,no_run
91/// use tako::{serve, router::Router};
92/// use tokio::net::TcpListener;
93///
94/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
95/// let listener = TcpListener::bind("127.0.0.1:8080").await?;
96/// let router = Router::new();
97/// serve(listener, router).await;
98/// # Ok(())
99/// # }
100/// ```
101pub use server::serve;
102
103/// TLS/SSL server implementation for secure connections.
104#[cfg(feature = "tls")]
105pub mod server_tls;
106
107/// Starts the HTTPS server with TLS encryption support.
108///
109/// Similar to `serve` but enables TLS encryption for secure connections. Requires
110/// the "tls" feature to be enabled and proper TLS configuration.
111///
112/// # Examples
113///
114/// ```rust,no_run
115/// # #[cfg(feature = "tls")]
116/// use tako::{serve_tls, router::Router};
117/// # #[cfg(feature = "tls")]
118/// use tokio::net::TcpListener;
119///
120/// # #[cfg(feature = "tls")]
121/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
122/// let listener = TcpListener::bind("127.0.0.1:8443").await?;
123/// let router = Router::new();
124/// // serve_tls(listener, router, tls_config).await;
125/// # Ok(())
126/// # }
127/// ```
128#[cfg(feature = "tls")]
129pub use server_tls::serve_tls;
130
131/// Global memory allocator using jemalloc for improved performance.
132#[cfg(feature = "jemalloc")]
133#[global_allocator]
134static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;