1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00
actix-web/src/lib.rs

239 lines
7.3 KiB
Rust
Raw Normal View History

2020-02-27 14:35:57 +01:00
#![warn(rust_2018_idioms, warnings)]
2019-12-08 07:31:16 +01:00
#![allow(
clippy::needless_doctest_main,
clippy::type_complexity,
clippy::borrow_interior_mutable_const
)]
2019-03-24 19:47:23 +01:00
//! Actix web is a small, pragmatic, and extremely fast web framework
//! for Rust.
//!
//! ## Example
//!
//! The `#[actix_rt::main]` macro in the example below is provided by the Actix runtime
//! crate, [`actix-rt`](https://crates.io/crates/actix-rt). You will need to include
//! `actix-rt` in your dependencies for it to run.
//!
2019-12-08 07:31:16 +01:00
//! ```rust,no_run
2019-03-24 19:47:23 +01:00
//! use actix_web::{web, App, Responder, HttpServer};
//!
2019-11-21 16:34:04 +01:00
//! async fn index(info: web::Path<(String, u32)>) -> impl Responder {
2019-03-24 19:47:23 +01:00
//! format!("Hello {}! id:{}", info.0, info.1)
//! }
//!
2019-12-08 07:31:16 +01:00
//! #[actix_rt::main]
//! async fn main() -> std::io::Result<()> {
2019-03-24 19:47:23 +01:00
//! HttpServer::new(|| App::new().service(
//! web::resource("/{name}/{id}/index.html").to(index))
//! )
//! .bind("127.0.0.1:8080")?
//! .run()
2019-12-08 07:31:16 +01:00
//! .await
2019-03-24 19:47:23 +01:00
//! }
//! ```
//!
//! ## Documentation & community resources
//!
//! Besides the API documentation (which you are currently looking
//! at!), several other resources are available:
//!
//! * [User Guide](https://actix.rs/docs/)
//! * [Chat on gitter](https://gitter.im/actix/actix)
//! * [GitHub repository](https://github.com/actix/actix-web)
//! * [Cargo package](https://crates.io/crates/actix-web)
//!
//! To get started navigating the API documentation you may want to
//! consider looking at the following pages:
//!
//! * [App](struct.App.html): This struct represents an actix-web
//! application and is used to configure routes and other common
//! settings.
//!
//! * [HttpServer](struct.HttpServer.html): This struct
//! represents an HTTP server instance and is used to instantiate and
//! configure servers.
//!
2019-03-30 18:04:38 +01:00
//! * [web](web/index.html): This module
//! provides essential helper functions and types for application registration.
2019-03-30 18:04:38 +01:00
//!
2019-03-24 19:47:23 +01:00
//! * [HttpRequest](struct.HttpRequest.html) and
//! [HttpResponse](struct.HttpResponse.html): These structs
//! represent HTTP requests and responses and expose various methods
//! for inspecting, creating and otherwise utilizing them.
//!
//! ## Features
//!
//! * Supported *HTTP/1.x* and *HTTP/2.0* protocols
//! * Streaming and pipelining
//! * Keep-alive and slow requests handling
//! * `WebSockets` server/client
//! * Transparent content compression/decompression (br, gzip, deflate)
//! * Configurable request routing
//! * Multipart streams
//! * SSL support with OpenSSL or `native-tls`
2019-07-31 15:49:46 +02:00
//! * Middlewares (`Logger`, `Session`, `CORS`, `DefaultHeaders`)
2019-03-24 19:47:23 +01:00
//! * Supports [Actix actor framework](https://github.com/actix/actix)
2019-11-26 11:07:39 +01:00
//! * Supported Rust version: 1.39 or later
2019-03-24 19:47:23 +01:00
//!
//! ## Package feature
//!
//! * `client` - enables http client (default enabled)
//! * `compress` - enables content encoding compression support (default enabled)
2019-11-20 18:33:22 +01:00
//! * `openssl` - enables ssl support via `openssl` crate, supports `http/2`
//! * `rustls` - enables ssl support via `rustls` crate, supports `http/2`
2019-03-30 18:04:38 +01:00
//! * `secure-cookies` - enables secure cookies support, includes `ring` crate as
2019-06-12 11:52:48 +02:00
//! dependency
#![allow(clippy::type_complexity, clippy::new_without_default)]
2019-03-02 07:51:32 +01:00
mod app;
mod app_service;
mod config;
2019-03-17 04:17:27 +01:00
mod data;
pub mod error;
mod extract;
2019-03-03 21:09:38 +01:00
pub mod guard;
mod handler;
mod info;
2017-12-27 04:59:41 +01:00
pub mod middleware;
2019-03-02 07:51:32 +01:00
mod request;
mod resource;
mod responder;
mod rmap;
2019-03-02 07:51:32 +01:00
mod route;
2019-03-04 06:02:01 +01:00
mod scope;
2019-03-05 01:29:03 +01:00
mod server;
2019-03-02 07:51:32 +01:00
mod service;
2019-03-03 01:24:14 +01:00
pub mod test;
2019-03-17 05:43:48 +01:00
mod types;
2019-03-30 18:04:38 +01:00
pub mod web;
2019-03-02 07:51:32 +01:00
2019-03-07 22:33:40 +01:00
#[doc(hidden)]
pub use actix_web_codegen::*;
2019-03-02 07:51:32 +01:00
// re-export for convenience
pub use actix_http::Response as HttpResponse;
pub use actix_http::{body, cookie, http, Error, HttpMessage, ResponseError, Result};
2019-03-02 07:51:32 +01:00
pub use crate::app::App;
2019-03-07 20:43:46 +01:00
pub use crate::extract::FromRequest;
2019-03-02 07:51:32 +01:00
pub use crate::request::HttpRequest;
pub use crate::resource::Resource;
pub use crate::responder::{Either, Responder};
pub use crate::route::Route;
2019-03-24 19:59:35 +01:00
pub use crate::scope::Scope;
2019-03-05 01:29:03 +01:00
pub use crate::server::HttpServer;
2018-07-29 08:43:04 +02:00
pub mod dev {
//! The `actix-web` prelude for library developers
//!
//! The purpose of this module is to alleviate imports of many common actix
//! traits by adding a glob import to the top of actix heavy modules:
//!
//! ```
//! # #![allow(unused_imports)]
//! use actix_web::dev::*;
//! ```
2019-04-15 16:32:49 +02:00
pub use crate::config::{AppConfig, AppService};
#[doc(hidden)]
2019-11-21 16:34:04 +01:00
pub use crate::handler::Factory;
pub use crate::info::ConnectionInfo;
pub use crate::rmap::ResourceMap;
2019-04-25 00:29:15 +02:00
pub use crate::service::{
HttpServiceFactory, ServiceRequest, ServiceResponse, WebService,
};
2019-11-20 18:33:22 +01:00
2019-11-26 06:25:50 +01:00
pub use crate::types::form::UrlEncoded;
pub use crate::types::json::JsonBody;
pub use crate::types::readlines::Readlines;
pub use actix_http::body::{Body, BodySize, MessageBody, ResponseBody, SizedStream};
2019-12-15 08:28:54 +01:00
#[cfg(feature = "compress")]
pub use actix_http::encoding::Decoder as Decompress;
2019-03-17 09:08:56 +01:00
pub use actix_http::ResponseBuilder as HttpResponseBuilder;
pub use actix_http::{
2019-03-27 18:38:01 +01:00
Extensions, Payload, PayloadStream, RequestHead, ResponseHead,
};
pub use actix_router::{Path, ResourceDef, ResourcePath, Url};
pub use actix_server::Server;
2019-05-22 20:20:37 +02:00
pub use actix_service::{Service, Transform};
pub(crate) fn insert_slash(mut patterns: Vec<String>) -> Vec<String> {
for path in &mut patterns {
if !path.is_empty() && !path.starts_with('/') {
path.insert(0, '/');
};
}
patterns
}
2019-12-16 12:22:26 +01:00
use crate::http::header::ContentEncoding;
use actix_http::{Response, ResponseBuilder};
struct Enc(ContentEncoding);
/// Helper trait that allows to set specific encoding for response.
pub trait BodyEncoding {
2019-12-18 04:30:14 +01:00
/// Get content encoding
fn get_encoding(&self) -> Option<ContentEncoding>;
2019-12-16 12:22:26 +01:00
2019-12-18 04:30:14 +01:00
/// Set content encoding
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self;
2019-12-16 12:22:26 +01:00
}
impl BodyEncoding for ResponseBuilder {
2019-12-18 04:30:14 +01:00
fn get_encoding(&self) -> Option<ContentEncoding> {
2019-12-16 12:22:26 +01:00
if let Some(ref enc) = self.extensions().get::<Enc>() {
Some(enc.0)
} else {
None
}
}
2019-12-18 04:30:14 +01:00
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
2019-12-16 12:22:26 +01:00
self.extensions_mut().insert(Enc(encoding));
self
}
}
impl<B> BodyEncoding for Response<B> {
2019-12-18 04:30:14 +01:00
fn get_encoding(&self) -> Option<ContentEncoding> {
2019-12-16 12:22:26 +01:00
if let Some(ref enc) = self.extensions().get::<Enc>() {
Some(enc.0)
} else {
None
}
}
2019-12-18 04:30:14 +01:00
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
2019-12-16 12:22:26 +01:00
self.extensions_mut().insert(Enc(encoding));
self
}
}
}
2019-03-27 17:24:55 +01:00
pub mod client {
//! An HTTP Client
//!
//! ```rust
//! use actix_web::client::Client;
//!
2019-11-26 06:25:50 +01:00
//! #[actix_rt::main]
//! async fn main() {
//! let mut client = Client::default();
2019-03-27 17:24:55 +01:00
//!
2019-11-26 06:25:50 +01:00
//! // Create request builder and send request
//! let response = client.get("http://www.rust-lang.org")
//! .header("User-Agent", "Actix-web")
//! .send().await; // <- Send http request
2019-11-20 18:33:22 +01:00
//!
2019-11-26 06:25:50 +01:00
//! println!("Response: {:?}", response);
2019-03-27 17:24:55 +01:00
//! }
//! ```
2019-03-28 02:53:19 +01:00
pub use awc::error::{
ConnectError, InvalidUrl, PayloadError, SendRequestError, WsClientError,
2019-03-27 17:24:55 +01:00
};
2019-04-05 20:36:26 +02:00
pub use awc::{
test, Client, ClientBuilder, ClientRequest, ClientResponse, Connector,
};
2019-03-27 17:24:55 +01:00
}