Skip to main content

tako/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2
3//! A multi-transport Rust framework for modern network services.
4//!
5//! Tako is built for services that go beyond plain HTTP. It gives you one
6//! cohesive model for routing, extraction, middleware, streaming, observability,
7//! and graceful shutdown across several protocols and transport layers.
8//!
9//! This umbrella crate stitches together the workspace sub-crates:
10//!
11//! - `tako-core` — routing, handlers, middleware and plugin traits, body and
12//!   request types, state, signals, queue, plus `GraphQL`, gRPC and `OpenAPI`
13//!   helpers
14//! - `tako-extractors` — concrete request extractors (cookies, form, query,
15//!   path, JWT, multipart, simdjson, …)
16//! - `tako-server` — HTTP/1, TLS, HTTP/3, raw TCP / UDP / Unix, PROXY protocol,
17//!   plus the compio variants
18//! - `tako-streams` — WebSocket, SSE, file streaming, static file serving,
19//!   WebTransport
20//! - `tako-plugins` — built-in middleware (auth, CSRF, sessions, …) and
21//!   plugins (CORS, compression, rate limiting, idempotency, metrics)
22//!
23//! All public types stay reachable at the original `tako::*` paths.
24
25pub use tako_rs_core::Bytes;
26pub use tako_rs_core::Full;
27pub use tako_rs_core::Method;
28pub use tako_rs_core::NOT_FOUND;
29pub use tako_rs_core::StatusCode;
30pub use tako_rs_core::header;
31pub use tako_rs_macros::delete;
32pub use tako_rs_macros::get;
33pub use tako_rs_macros::patch;
34pub use tako_rs_macros::post;
35pub use tako_rs_macros::put;
36pub use tako_rs_macros::route;
37
38/// Implementation details for tako's proc macros. Not part of the stable
39/// API — relied on only by macro-generated code.
40#[doc(hidden)]
41pub mod __private {
42  pub use linkme;
43}
44
45pub use tako_rs_core::body;
46// `tako_rs_core::client` is tokio-runtime-only (tokio-rustls + hyper-util
47// client-legacy). When `compio` is also enabled, the upstream crate refuses
48// to compile the `client` module, so we cannot re-export it either. We
49// surface this as a docs-only note rather than a `compile_error!`, because
50// the workspace `cargo check --workspace --all-features` invocation turns
51// every feature on simultaneously and we must not break that — when
52// `compio` is on, `tako::client` is intentionally absent, see the cfg
53// expression for the exact condition.
54#[cfg(all(feature = "client", not(feature = "compio")))]
55#[cfg_attr(docsrs, doc(cfg(all(feature = "client", not(feature = "compio")))))]
56pub use tako_rs_core::client;
57pub use tako_rs_core::config;
58pub use tako_rs_core::conn_info;
59#[cfg(feature = "graphiql")]
60#[cfg_attr(docsrs, doc(cfg(feature = "graphiql")))]
61pub use tako_rs_core::graphiql;
62#[cfg(feature = "async-graphql")]
63#[cfg_attr(docsrs, doc(cfg(feature = "async-graphql")))]
64pub use tako_rs_core::graphql;
65#[cfg(feature = "grpc")]
66#[cfg_attr(docsrs, doc(cfg(feature = "grpc")))]
67pub use tako_rs_core::grpc;
68#[cfg(any(feature = "utoipa", feature = "vespera"))]
69#[cfg_attr(docsrs, doc(cfg(any(feature = "utoipa", feature = "vespera"))))]
70pub use tako_rs_core::openapi;
71pub use tako_rs_core::problem;
72pub use tako_rs_core::queue;
73pub use tako_rs_core::redirect;
74pub use tako_rs_core::responder;
75pub use tako_rs_core::route;
76pub use tako_rs_core::router;
77pub use tako_rs_core::router_state;
78#[cfg(feature = "signals")]
79#[cfg_attr(docsrs, doc(cfg(feature = "signals")))]
80pub use tako_rs_core::signals;
81pub use tako_rs_core::state;
82#[cfg(feature = "tako-tracing")]
83#[cfg_attr(docsrs, doc(cfg(feature = "tako-tracing")))]
84pub use tako_rs_core::tracing;
85pub use tako_rs_core::types;
86pub use tako_rs_server::AcceptBackoff;
87#[cfg(feature = "compio")]
88pub use tako_rs_server::CompioServer;
89#[cfg(feature = "compio")]
90pub use tako_rs_server::CompioServerBuilder;
91#[cfg(not(feature = "compio"))]
92pub use tako_rs_server::Server;
93#[cfg(not(feature = "compio"))]
94pub use tako_rs_server::ServerBuilder;
95pub use tako_rs_server::ServerConfig;
96pub use tako_rs_server::ServerHandle;
97pub use tako_rs_server::TlsCert;
98pub use tako_rs_server::bind_with_port_fallback;
99#[cfg(not(any(feature = "compio", feature = "compio-tls", feature = "compio-ws")))]
100pub use tako_rs_server::proxy_protocol;
101pub use tako_rs_server::serve;
102#[cfg(all(feature = "http2", not(feature = "compio")))]
103#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
104pub use tako_rs_server::serve_h2c;
105#[cfg(all(feature = "http2", not(feature = "compio")))]
106#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
107pub use tako_rs_server::serve_h2c_with_config;
108#[cfg(all(feature = "http2", not(feature = "compio")))]
109#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
110pub use tako_rs_server::serve_h2c_with_shutdown;
111#[cfg(all(feature = "http2", not(feature = "compio")))]
112#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
113pub use tako_rs_server::serve_h2c_with_shutdown_and_config;
114#[cfg(all(feature = "http3", not(feature = "compio")))]
115#[cfg_attr(docsrs, doc(cfg(feature = "http3")))]
116pub use tako_rs_server::serve_h3;
117#[cfg(all(feature = "http3", not(feature = "compio")))]
118#[cfg_attr(docsrs, doc(cfg(feature = "http3")))]
119pub use tako_rs_server::serve_h3_with_config;
120#[cfg(all(feature = "http3", not(feature = "compio")))]
121#[cfg_attr(docsrs, doc(cfg(feature = "http3")))]
122pub use tako_rs_server::serve_h3_with_shutdown;
123#[cfg(all(feature = "http3", not(feature = "compio")))]
124#[cfg_attr(docsrs, doc(cfg(feature = "http3")))]
125pub use tako_rs_server::serve_h3_with_shutdown_and_config;
126#[cfg(any(
127  all(
128    feature = "tls",
129    not(any(feature = "compio", feature = "compio-tls", feature = "compio-ws"))
130  ),
131  feature = "compio-tls"
132))]
133#[cfg_attr(docsrs, doc(cfg(any(feature = "tls", feature = "compio-tls"))))]
134pub use tako_rs_server::serve_tls;
135#[cfg(all(
136  feature = "tls",
137  not(any(feature = "compio", feature = "compio-tls", feature = "compio-ws"))
138))]
139#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
140pub use tako_rs_server::serve_tls_with_config;
141#[cfg(any(
142  all(
143    feature = "tls",
144    not(any(feature = "compio", feature = "compio-tls", feature = "compio-ws"))
145  ),
146  feature = "compio-tls"
147))]
148#[cfg_attr(docsrs, doc(cfg(any(feature = "tls", feature = "compio-tls"))))]
149pub use tako_rs_server::serve_tls_with_shutdown;
150#[cfg(all(
151  feature = "tls",
152  not(any(feature = "compio", feature = "compio-tls", feature = "compio-ws"))
153))]
154#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
155pub use tako_rs_server::serve_tls_with_shutdown_and_config;
156#[cfg(not(feature = "compio"))]
157pub use tako_rs_server::serve_with_config;
158pub use tako_rs_server::serve_with_shutdown;
159#[cfg(not(feature = "compio"))]
160pub use tako_rs_server::serve_with_shutdown_and_config;
161#[cfg(feature = "compio")]
162#[cfg_attr(docsrs, doc(cfg(feature = "compio")))]
163pub use tako_rs_server::server_compio;
164#[cfg(all(feature = "http2", not(feature = "compio")))]
165#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
166pub use tako_rs_server::server_h2c;
167#[cfg(all(feature = "http3", not(feature = "compio")))]
168#[cfg_attr(docsrs, doc(cfg(feature = "http3")))]
169pub use tako_rs_server::server_h3;
170pub use tako_rs_server::server_tcp;
171#[cfg(all(not(feature = "compio-tls"), feature = "tls"))]
172#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
173pub use tako_rs_server::server_tls;
174#[cfg(feature = "compio-tls")]
175#[cfg_attr(docsrs, doc(cfg(feature = "compio-tls")))]
176pub use tako_rs_server::server_tls_compio;
177pub use tako_rs_server::server_udp;
178#[cfg(all(
179  unix,
180  not(any(feature = "compio", feature = "compio-tls", feature = "compio-ws"))
181))]
182pub use tako_rs_server::server_unix;
183#[cfg(feature = "file-stream")]
184#[cfg_attr(docsrs, doc(cfg(feature = "file-stream")))]
185pub use tako_rs_streams::file_stream;
186pub use tako_rs_streams::sse;
187pub use tako_rs_streams::r#static;
188#[cfg(all(feature = "webtransport", not(feature = "compio")))]
189#[cfg_attr(docsrs, doc(cfg(feature = "webtransport")))]
190pub use tako_rs_streams::webtransport;
191#[cfg(not(any(feature = "compio", feature = "compio-ws")))]
192pub use tako_rs_streams::ws;
193#[cfg(feature = "compio-ws")]
194#[cfg_attr(docsrs, doc(cfg(feature = "compio-ws")))]
195pub use tako_rs_streams::ws_compio;
196
197/// Request data extraction utilities.
198pub mod extractors {
199  pub use tako_rs_core::extractors::FromRequest;
200  pub use tako_rs_core::extractors::FromRequestParts;
201  #[doc(hidden)]
202  pub use tako_rs_core::extractors::is_json_content_type;
203  pub use tako_rs_core::extractors::json;
204  pub use tako_rs_core::extractors::params;
205  pub use tako_rs_core::extractors::range;
206  pub use tako_rs_core::extractors::typed_params;
207  pub use tako_rs_extractors::acc_lang;
208  pub use tako_rs_extractors::accept;
209  pub use tako_rs_extractors::basic;
210  pub use tako_rs_extractors::bearer;
211  pub use tako_rs_extractors::bytes;
212  pub use tako_rs_extractors::connect_info;
213  pub use tako_rs_extractors::content_length_limit;
214  pub use tako_rs_extractors::cookie_jar;
215  pub use tako_rs_extractors::cookie_key_expansion;
216  pub use tako_rs_extractors::cookie_private;
217  pub use tako_rs_extractors::cookie_signed;
218  pub use tako_rs_extractors::extension;
219  pub use tako_rs_extractors::form;
220  pub use tako_rs_extractors::header_map;
221  pub use tako_rs_extractors::ipaddr;
222  pub use tako_rs_extractors::jwt;
223  pub use tako_rs_extractors::matched_path;
224  #[cfg(feature = "multipart")]
225  #[cfg_attr(docsrs, doc(cfg(feature = "multipart")))]
226  pub use tako_rs_extractors::multipart;
227  pub use tako_rs_extractors::path;
228  #[cfg(feature = "protobuf")]
229  #[cfg_attr(docsrs, doc(cfg(feature = "protobuf")))]
230  pub use tako_rs_extractors::protobuf;
231  pub use tako_rs_extractors::query;
232  pub use tako_rs_extractors::query_multi;
233  #[cfg(feature = "simd")]
234  #[cfg_attr(docsrs, doc(cfg(feature = "simd")))]
235  pub use tako_rs_extractors::simdjson;
236  pub use tako_rs_extractors::state;
237  #[cfg(feature = "typed-header")]
238  #[cfg_attr(docsrs, doc(cfg(feature = "typed-header")))]
239  pub use tako_rs_extractors::typed_header;
240  pub use tako_rs_extractors::uri_parts;
241  #[cfg(any(feature = "validator", feature = "garde"))]
242  #[cfg_attr(docsrs, doc(cfg(any(feature = "validator", feature = "garde"))))]
243  pub use tako_rs_extractors::validate;
244}
245
246/// Middleware for processing requests and responses in a pipeline.
247pub mod middleware {
248  pub use tako_rs_core::middleware::IntoMiddleware;
249  pub use tako_rs_core::middleware::Next;
250  pub use tako_rs_plugins::middleware::access_log;
251  pub use tako_rs_plugins::middleware::api_key_auth;
252  pub use tako_rs_plugins::middleware::basic_auth;
253  pub use tako_rs_plugins::middleware::bearer_auth;
254  pub use tako_rs_plugins::middleware::body_limit;
255  pub use tako_rs_plugins::middleware::circuit_breaker;
256  pub use tako_rs_plugins::middleware::csrf;
257  pub use tako_rs_plugins::middleware::etag;
258  pub use tako_rs_plugins::middleware::healthcheck;
259  #[cfg(feature = "hmac-signature")]
260  #[cfg_attr(docsrs, doc(cfg(feature = "hmac-signature")))]
261  pub use tako_rs_plugins::middleware::hmac_signature;
262  #[cfg(feature = "ip-filter")]
263  #[cfg_attr(docsrs, doc(cfg(feature = "ip-filter")))]
264  pub use tako_rs_plugins::middleware::ip_filter;
265  #[cfg(feature = "json-schema")]
266  #[cfg_attr(docsrs, doc(cfg(feature = "json-schema")))]
267  pub use tako_rs_plugins::middleware::json_schema;
268  pub use tako_rs_plugins::middleware::jwt_auth;
269  pub use tako_rs_plugins::middleware::problem_json;
270  pub use tako_rs_plugins::middleware::request_id;
271  pub use tako_rs_plugins::middleware::security_headers;
272  pub use tako_rs_plugins::middleware::session;
273  pub use tako_rs_plugins::middleware::tenant;
274  pub use tako_rs_plugins::middleware::timeout;
275  pub use tako_rs_plugins::middleware::traceparent;
276  pub use tako_rs_plugins::middleware::upload_progress;
277}
278
279/// Pluggable backend traits for stateful middleware (sessions, rate limiting, …).
280pub mod stores {
281  pub use tako_rs_plugins::stores::*;
282}
283
284#[cfg(feature = "plugins")]
285#[cfg_attr(docsrs, doc(cfg(feature = "plugins")))]
286pub mod plugins {
287  pub use tako_rs_core::plugins::TakoPlugin;
288  pub use tako_rs_plugins::plugins::compression;
289  pub use tako_rs_plugins::plugins::cors;
290  pub use tako_rs_plugins::plugins::idempotency;
291  #[cfg(any(feature = "metrics-prometheus", feature = "metrics-opentelemetry"))]
292  #[cfg_attr(
293    docsrs,
294    doc(cfg(any(feature = "metrics-prometheus", feature = "metrics-opentelemetry")))
295  )]
296  pub use tako_rs_plugins::plugins::metrics;
297  pub use tako_rs_plugins::plugins::rate_limiter;
298}
299
300#[cfg(feature = "zero-copy-extractors")]
301#[cfg_attr(docsrs, doc(cfg(feature = "zero-copy-extractors")))]
302pub use tako_rs_extractors::zero_copy_extractors;
303#[cfg(feature = "per-thread")]
304#[cfg_attr(docsrs, doc(cfg(feature = "per-thread")))]
305pub use tako_rs_server_pt::PerThreadConfig;
306#[cfg(feature = "per-thread")]
307#[cfg_attr(docsrs, doc(cfg(feature = "per-thread")))]
308pub use tako_rs_server_pt::serve_per_thread;
309#[cfg(feature = "per-thread-compio")]
310#[cfg_attr(docsrs, doc(cfg(feature = "per-thread-compio")))]
311pub use tako_rs_server_pt::serve_per_thread_compio;
312
313#[cfg(feature = "jemalloc")]
314#[cfg_attr(docsrs, doc(cfg(feature = "jemalloc")))]
315#[global_allocator]
316static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
317
318/// Re-exports of the types most handlers reach for in everyday code.
319///
320/// Import everything with `use tako::prelude::*;` to get `Method`,
321/// `StatusCode`, `Router`, `Responder`, `Request`, `Response`, the common
322/// body / query / form / path extractors, and the middleware traits.
323/// The prelude intentionally re-exports types only — modules stay at their
324/// canonical `tako::extractors::…` / `tako::middleware::…` paths so users
325/// can still pull individual items in qualified form when they prefer.
326pub mod prelude {
327  pub use tako_rs_core::Method;
328  pub use tako_rs_core::StatusCode;
329  pub use tako_rs_core::body::TakoBody;
330  pub use tako_rs_core::extractors::FromRequest;
331  pub use tako_rs_core::extractors::FromRequestParts;
332  pub use tako_rs_core::extractors::json::Json;
333  pub use tako_rs_core::extractors::params::Params;
334  pub use tako_rs_core::middleware::IntoMiddleware;
335  pub use tako_rs_core::middleware::Next;
336  pub use tako_rs_core::responder::Responder;
337  pub use tako_rs_core::router::Router;
338  pub use tako_rs_core::types::Request;
339  pub use tako_rs_core::types::Response;
340  pub use tako_rs_extractors::form::Form;
341  pub use tako_rs_extractors::path::Path;
342  pub use tako_rs_extractors::query::Query;
343  pub use tako_rs_extractors::state::State;
344}