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/// HTTP client implementation for making outbound requests.
25#[cfg(feature = "client")]
26pub mod client;
27
28/// Request data extraction utilities for parsing query params, JSON, and more.
29pub mod extractors;
30
31/// File streaming utilities for serving files.
32#[cfg(feature = "file-stream")]
33pub mod file_stream;
34
35/// Request handler traits and implementations.
36mod handler;
37
38/// Middleware for processing requests and responses in a pipeline.
39pub mod middleware;
40
41/// Plugin system for extending framework functionality.
42#[cfg(feature = "plugins")]
43pub mod plugins;
44
45/// Response generation utilities and traits.
46pub mod responder;
47
48/// Route definition and matching logic.
49mod route;
50
51/// Request routing and dispatch functionality.
52pub mod router;
53
54/// HTTP server implementation and configuration.
55mod server;
56
57/// Server-Sent Events (SSE) support for real-time communication.
58pub mod sse;
59
60/// Application state management and dependency injection.
61pub mod state;
62
63/// Static file serving utilities.
64pub mod r#static;
65
66/// Distributed tracing integration for observability.
67#[cfg(feature = "tako-tracing")]
68pub mod tracing;
69
70/// Core type definitions used throughout the framework.
71pub mod types;
72
73/// WebSocket connection handling and message processing.
74pub mod ws;
75
76pub use bytes::Bytes;
77pub use hyper::{Method, StatusCode};
78
79/// Starts the HTTP server with the given listener and router.
80///
81/// This is the main entry point for starting a Tako web server. The function takes
82/// ownership of a TCP listener and router, then serves incoming connections until
83/// the server is shut down.
84///
85/// # Examples
86///
87/// ```rust,no_run
88/// use tako::{serve, router::Router};
89/// use tokio::net::TcpListener;
90///
91/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
92/// let listener = TcpListener::bind("127.0.0.1:8080").await?;
93/// let router = Router::new();
94/// serve(listener, router).await;
95/// # Ok(())
96/// # }
97/// ```
98pub use server::serve;
99
100/// TLS/SSL server implementation for secure connections.
101#[cfg(feature = "tls")]
102pub mod server_tls;
103
104/// Starts the HTTPS server with TLS encryption support.
105///
106/// Similar to `serve` but enables TLS encryption for secure connections. Requires
107/// the "tls" feature to be enabled and proper TLS configuration.
108///
109/// # Examples
110///
111/// ```rust,no_run
112/// # #[cfg(feature = "tls")]
113/// use tako::{serve_tls, router::Router};
114/// # #[cfg(feature = "tls")]
115/// use tokio::net::TcpListener;
116///
117/// # #[cfg(feature = "tls")]
118/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
119/// let listener = TcpListener::bind("127.0.0.1:8443").await?;
120/// let router = Router::new();
121/// // serve_tls(listener, router, tls_config).await;
122/// # Ok(())
123/// # }
124/// ```
125#[cfg(feature = "tls")]
126pub use server_tls::serve_tls;
127
128/// Global memory allocator using jemalloc for improved performance.
129#[cfg(feature = "jemalloc")]
130#[global_allocator]
131static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;