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/// Redirection utilities for handling HTTP redirects.
51pub mod redirect;
52
53/// Route definition and matching logic.
54mod route;
55
56/// Request routing and dispatch functionality.
57pub mod router;
58
59/// HTTP server implementation and configuration.
60mod server;
61
62/// Server-Sent Events (SSE) support for real-time communication.
63pub mod sse;
64
65/// Application state management and dependency injection.
66pub mod state;
67
68/// Static file serving utilities.
69pub mod r#static;
70
71/// Distributed tracing integration for observability.
72#[cfg(feature = "tako-tracing")]
73pub mod tracing;
74
75/// Core type definitions used throughout the framework.
76pub mod types;
77
78/// WebSocket connection handling and message processing.
79pub mod ws;
80
81/// GraphQL support (request extractors, responses, and subscriptions).
82#[cfg(feature = "async-graphql")]
83pub mod graphql;
84
85/// GraphiQL UI helpers.
86#[cfg(feature = "graphiql")]
87pub mod graphiql;
88pub use bytes::Bytes;
89pub use http::{Method, StatusCode, header};
90pub use http_body_util::Full;
91pub use responder::NOT_FOUND;
92
93/// Starts the HTTP server with the given listener and router.
94///
95/// This is the main entry point for starting a Tako web server. The function takes
96/// ownership of a TCP listener and router, then serves incoming connections until
97/// the server is shut down.
98///
99/// # Examples
100///
101/// ```rust,no_run
102/// use tako::{serve, router::Router};
103/// use tokio::net::TcpListener;
104///
105/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
106/// let listener = TcpListener::bind("127.0.0.1:8080").await?;
107/// let router = Router::new();
108/// serve(listener, router).await;
109/// # Ok(())
110/// # }
111/// ```
112pub use server::serve;
113
114/// TLS/SSL server implementation for secure connections.
115#[cfg(feature = "tls")]
116pub mod server_tls;
117
118/// Starts the HTTPS server with TLS encryption support.
119///
120/// Similar to `serve` but enables TLS encryption for secure connections. Requires
121/// the "tls" feature to be enabled and proper TLS configuration.
122///
123/// # Examples
124///
125/// ```rust,no_run
126/// # #[cfg(feature = "tls")]
127/// use tako::{serve_tls, router::Router};
128/// # #[cfg(feature = "tls")]
129/// use tokio::net::TcpListener;
130///
131/// # #[cfg(feature = "tls")]
132/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
133/// let listener = TcpListener::bind("127.0.0.1:8443").await?;
134/// let router = Router::new();
135/// // serve_tls(listener, router, tls_config).await;
136/// # Ok(())
137/// # }
138/// ```
139#[cfg(feature = "tls")]
140pub use server_tls::serve_tls;
141
142/// Global memory allocator using jemalloc for improved performance.
143#[cfg(feature = "jemalloc")]
144#[global_allocator]
145static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;