1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 13:29:24 +00:00
actix-web/src/lib.rs

231 lines
7 KiB
Rust
Raw Normal View History

2020-02-27 13:35:57 +00:00
#![warn(rust_2018_idioms, warnings)]
#![allow(clippy::needless_doctest_main, clippy::type_complexity)]
2019-03-24 18:47:23 +00:00
//! Actix web is a small, pragmatic, and extremely fast web framework
//! for Rust.
//!
//! ## Example
//!
2019-12-08 06:31:16 +00:00
//! ```rust,no_run
2019-03-24 18:47:23 +00:00
//! use actix_web::{web, App, Responder, HttpServer};
//!
2019-11-21 15:34:04 +00:00
//! async fn index(info: web::Path<(String, u32)>) -> impl Responder {
2019-03-24 18:47:23 +00:00
//! format!("Hello {}! id:{}", info.0, info.1)
//! }
//!
//! #[actix_web::main]
2019-12-08 06:31:16 +00:00
//! async fn main() -> std::io::Result<()> {
2019-03-24 18:47:23 +00:00
//! HttpServer::new(|| App::new().service(
//! web::resource("/{name}/{id}/index.html").to(index))
//! )
//! .bind("127.0.0.1:8080")?
//! .run()
2019-12-08 06:31:16 +00:00
//! .await
2019-03-24 18:47:23 +00: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 17:04:38 +00:00
//! * [web](web/index.html): This module
//! provides essential helper functions and types for application registration.
2019-03-30 17:04:38 +00:00
//!
2019-03-24 18:47:23 +00: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 13:49:46 +00:00
//! * Middlewares (`Logger`, `Session`, `CORS`, `DefaultHeaders`)
2019-03-24 18:47:23 +00:00
//! * Supports [Actix actor framework](https://github.com/actix/actix)
2020-05-13 00:57:37 +00:00
//! * Supported Rust version: 1.40 or later
2019-03-24 18:47:23 +00:00
//!
//! ## Package feature
//!
//! * `client` - enables http client (default enabled)
//! * `compress` - enables content encoding compression support (default enabled)
2019-11-20 17:33:22 +00:00
//! * `openssl` - enables ssl support via `openssl` crate, supports `http/2`
//! * `rustls` - enables ssl support via `rustls` crate, supports `http/2`
//! * `secure-cookies` - enables secure cookies support
2019-03-02 06:51:32 +00:00
mod app;
mod app_service;
mod config;
2019-03-17 03:17:27 +00:00
mod data;
pub mod error;
mod extract;
2019-03-03 20:09:38 +00:00
pub mod guard;
mod handler;
mod info;
2017-12-27 03:59:41 +00:00
pub mod middleware;
2019-03-02 06:51:32 +00:00
mod request;
mod resource;
mod responder;
mod rmap;
2019-03-02 06:51:32 +00:00
mod route;
2019-03-04 05:02:01 +00:00
mod scope;
2019-03-05 00:29:03 +00:00
mod server;
2019-03-02 06:51:32 +00:00
mod service;
2019-03-03 00:24:14 +00:00
pub mod test;
2019-03-17 04:43:48 +00:00
mod types;
2019-03-30 17:04:38 +00:00
pub mod web;
2019-03-02 06:51:32 +00:00
2019-03-07 21:33:40 +00:00
pub use actix_web_codegen::*;
pub use actix_rt as rt;
2019-03-02 06:51:32 +00: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 06:51:32 +00:00
pub use crate::app::App;
2019-03-07 19:43:46 +00:00
pub use crate::extract::FromRequest;
2019-03-02 06:51:32 +00: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 18:59:35 +00:00
pub use crate::scope::Scope;
2019-03-05 00:29:03 +00:00
pub use crate::server::HttpServer;
2018-07-29 06:43:04 +00: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 14:32:49 +00:00
pub use crate::config::{AppConfig, AppService};
#[doc(hidden)]
2019-11-21 15:34:04 +00:00
pub use crate::handler::Factory;
pub use crate::info::ConnectionInfo;
pub use crate::rmap::ResourceMap;
2019-04-24 22:29:15 +00:00
pub use crate::service::{
HttpServiceFactory, ServiceRequest, ServiceResponse, WebService,
};
2019-11-20 17:33:22 +00:00
2019-11-26 05:25:50 +00: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 07:28:54 +00:00
#[cfg(feature = "compress")]
pub use actix_http::encoding::Decoder as Decompress;
2019-03-17 08:08:56 +00:00
pub use actix_http::ResponseBuilder as HttpResponseBuilder;
pub use actix_http::{
2019-03-27 17:38:01 +00:00
Extensions, Payload, PayloadStream, RequestHead, ResponseHead,
};
pub use actix_router::{Path, ResourceDef, ResourcePath, Url};
pub use actix_server::Server;
2019-05-22 18:20:37 +00: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 11:22:26 +00: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 03:30:14 +00:00
/// Get content encoding
fn get_encoding(&self) -> Option<ContentEncoding>;
2019-12-16 11:22:26 +00:00
2019-12-18 03:30:14 +00:00
/// Set content encoding
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self;
2019-12-16 11:22:26 +00:00
}
impl BodyEncoding for ResponseBuilder {
2019-12-18 03:30:14 +00:00
fn get_encoding(&self) -> Option<ContentEncoding> {
2019-12-16 11:22:26 +00:00
if let Some(ref enc) = self.extensions().get::<Enc>() {
Some(enc.0)
} else {
None
}
}
2019-12-18 03:30:14 +00:00
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
2019-12-16 11:22:26 +00:00
self.extensions_mut().insert(Enc(encoding));
self
}
}
impl<B> BodyEncoding for Response<B> {
2019-12-18 03:30:14 +00:00
fn get_encoding(&self) -> Option<ContentEncoding> {
2019-12-16 11:22:26 +00:00
if let Some(ref enc) = self.extensions().get::<Enc>() {
Some(enc.0)
} else {
None
}
}
2019-12-18 03:30:14 +00:00
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
2019-12-16 11:22:26 +00:00
self.extensions_mut().insert(Enc(encoding));
self
}
}
}
2019-03-27 16:24:55 +00:00
pub mod client {
//! An HTTP Client
//!
//! ```rust
//! use actix_web::client::Client;
//!
2019-11-26 05:25:50 +00:00
//! #[actix_rt::main]
//! async fn main() {
//! let mut client = Client::default();
2019-03-27 16:24:55 +00:00
//!
2019-11-26 05:25:50 +00: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 17:33:22 +00:00
//!
2019-11-26 05:25:50 +00:00
//! println!("Response: {:?}", response);
2019-03-27 16:24:55 +00:00
//! }
//! ```
2019-03-28 01:53:19 +00:00
pub use awc::error::{
ConnectError, InvalidUrl, PayloadError, SendRequestError, WsClientError,
2019-03-27 16:24:55 +00:00
};
2019-04-05 18:36:26 +00:00
pub use awc::{
test, Client, ClientBuilder, ClientRequest, ClientResponse, Connector,
};
2019-03-27 16:24:55 +00:00
}