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