mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
re-export rt in web and add main macro (#1575)
This commit is contained in:
parent
c11052f826
commit
a70e599ff5
@ -97,8 +97,8 @@ serde_json = "1.0"
|
|||||||
serde_urlencoded = "0.6.1"
|
serde_urlencoded = "0.6.1"
|
||||||
time = { version = "0.2.7", default-features = false, features = ["std"] }
|
time = { version = "0.2.7", default-features = false, features = ["std"] }
|
||||||
url = "2.1"
|
url = "2.1"
|
||||||
open-ssl = { version="0.10", package = "openssl", optional = true }
|
open-ssl = { package = "openssl", version = "0.10", optional = true }
|
||||||
rust-tls = { version = "0.17.0", package = "rustls", optional = true }
|
rust-tls = { package = "rustls", version = "0.17.0", optional = true }
|
||||||
tinyvec = { version = "0.3", features = ["alloc"] }
|
tinyvec = { version = "0.3", features = ["alloc"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
@ -11,7 +11,6 @@ documentation = "https://docs.rs/actix-files/"
|
|||||||
categories = ["asynchronous", "web-programming::http-server"]
|
categories = ["asynchronous", "web-programming::http-server"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_files"
|
name = "actix_files"
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [Unreleased] - XXXX-XX-XX
|
||||||
|
|
||||||
|
* Add main entry-point macro that uses re-exported runtime.
|
||||||
|
|
||||||
|
|
||||||
## [0.2.2] - 2020-05-23
|
## [0.2.2] - 2020-05-23
|
||||||
|
|
||||||
* Add resource middleware on actix-web-codegen [#1467]
|
* Add resource middleware on actix-web-codegen [#1467]
|
||||||
|
@ -9,7 +9,6 @@ documentation = "https://docs.rs/actix-web-codegen"
|
|||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
proc-macro = true
|
proc-macro = true
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# Macros for actix-web framework [![Build Status](https://travis-ci.org/actix/actix-web.svg?branch=master)](https://travis-ci.org/actix/actix-web) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) [![crates.io](https://meritbadge.herokuapp.com/actix-web-codegen)](https://crates.io/crates/actix-web-codegen) [![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
# Helper and convenience macros for Actix-web. [![Build Status](https://travis-ci.org/actix/actix-web.svg?branch=master)](https://travis-ci.org/actix/actix-web) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) [![crates.io](https://meritbadge.herokuapp.com/actix-web-codegen)](https://crates.io/crates/actix-web-codegen) [![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
## Documentation & Resources
|
## Documentation & Resources
|
||||||
|
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
#![recursion_limit = "512"]
|
#![recursion_limit = "512"]
|
||||||
//! Actix-web codegen module
|
|
||||||
|
//! Helper and convenience macros for Actix-web.
|
||||||
//!
|
//!
|
||||||
//! Generators for routes and scopes
|
//! ## Runtime Setup
|
||||||
//!
|
//!
|
||||||
//! ## Route
|
//! - [main](attr.main.html)
|
||||||
//!
|
//!
|
||||||
//! Macros:
|
//! ## Resource Macros:
|
||||||
//!
|
//!
|
||||||
//! - [get](attr.get.html)
|
//! - [get](attr.get.html)
|
||||||
//! - [post](attr.post.html)
|
//! - [post](attr.post.html)
|
||||||
@ -23,12 +24,12 @@
|
|||||||
//! - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
//! - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
||||||
//! - `wrap="Middleware"` - Registers a resource middleware.
|
//! - `wrap="Middleware"` - Registers a resource middleware.
|
||||||
//!
|
//!
|
||||||
//! ## Notes
|
//! ### Notes
|
||||||
//!
|
//!
|
||||||
//! Function name can be specified as any expression that is going to be accessible to the generate
|
//! Function name can be specified as any expression that is going to be accessible to the generate
|
||||||
//! code (e.g `my_guard` or `my_module::my_guard`)
|
//! code (e.g `my_guard` or `my_module::my_guard`)
|
||||||
//!
|
//!
|
||||||
//! ## Example:
|
//! ### Example:
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use actix_web::HttpResponse;
|
//! use actix_web::HttpResponse;
|
||||||
@ -139,3 +140,43 @@ pub fn trace(args: TokenStream, input: TokenStream) -> TokenStream {
|
|||||||
pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream {
|
pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
route::generate(args, input, route::GuardType::Patch)
|
route::generate(args, input, route::GuardType::Patch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Marks async main function as the actix system entry-point.
|
||||||
|
///
|
||||||
|
/// ## Usage
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// #[actix_web::main]
|
||||||
|
/// async fn main() {
|
||||||
|
/// async { println!("Hello world"); }.await
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[proc_macro_attribute]
|
||||||
|
#[cfg(not(test))] // Work around for rust-lang/rust#62127
|
||||||
|
pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
|
use quote::quote;
|
||||||
|
|
||||||
|
let mut input = syn::parse_macro_input!(item as syn::ItemFn);
|
||||||
|
let attrs = &input.attrs;
|
||||||
|
let vis = &input.vis;
|
||||||
|
let sig = &mut input.sig;
|
||||||
|
let body = &input.block;
|
||||||
|
let name = &sig.ident;
|
||||||
|
|
||||||
|
if sig.asyncness.is_none() {
|
||||||
|
return syn::Error::new_spanned(sig.fn_token, "only async fn is supported")
|
||||||
|
.to_compile_error()
|
||||||
|
.into();
|
||||||
|
}
|
||||||
|
|
||||||
|
sig.asyncness = None;
|
||||||
|
|
||||||
|
(quote! {
|
||||||
|
#(#attrs)*
|
||||||
|
#vis #sig {
|
||||||
|
actix_web::rt::System::new(stringify!(#name))
|
||||||
|
.block_on(async move { #body })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
1
rust-toolchain
Normal file
1
rust-toolchain
Normal file
@ -0,0 +1 @@
|
|||||||
|
1.40.0
|
@ -42,6 +42,7 @@ pub struct App<T, B> {
|
|||||||
|
|
||||||
impl App<AppEntry, Body> {
|
impl App<AppEntry, Body> {
|
||||||
/// Create application builder. Application can be configured with a builder-like pattern.
|
/// Create application builder. Application can be configured with a builder-like pattern.
|
||||||
|
#[allow(clippy::new_without_default)]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let fref = Rc::new(RefCell::new(None));
|
let fref = Rc::new(RefCell::new(None));
|
||||||
App {
|
App {
|
||||||
|
@ -25,7 +25,7 @@ impl ConnectionInfo {
|
|||||||
Ref::map(req.extensions(), |e| e.get().unwrap())
|
Ref::map(req.extensions(), |e| e.get().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::cognitive_complexity)]
|
#[allow(clippy::cognitive_complexity, clippy::borrow_interior_mutable_const)]
|
||||||
fn new(req: &RequestHead, cfg: &AppConfig) -> ConnectionInfo {
|
fn new(req: &RequestHead, cfg: &AppConfig) -> ConnectionInfo {
|
||||||
let mut host = None;
|
let mut host = None;
|
||||||
let mut scheme = None;
|
let mut scheme = None;
|
||||||
|
21
src/lib.rs
21
src/lib.rs
@ -1,18 +1,11 @@
|
|||||||
#![warn(rust_2018_idioms, warnings)]
|
#![warn(rust_2018_idioms, warnings)]
|
||||||
#![allow(
|
#![allow(clippy::needless_doctest_main, clippy::type_complexity)]
|
||||||
clippy::needless_doctest_main,
|
|
||||||
clippy::type_complexity,
|
|
||||||
clippy::borrow_interior_mutable_const
|
|
||||||
)]
|
|
||||||
//! Actix web is a small, pragmatic, and extremely fast web framework
|
//! Actix web is a small, pragmatic, and extremely fast web framework
|
||||||
//! for Rust.
|
//! for Rust.
|
||||||
//!
|
//!
|
||||||
//! ## Example
|
//! ## 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.
|
|
||||||
//!
|
|
||||||
//! ```rust,no_run
|
//! ```rust,no_run
|
||||||
//! use actix_web::{web, App, Responder, HttpServer};
|
//! use actix_web::{web, App, Responder, HttpServer};
|
||||||
//!
|
//!
|
||||||
@ -20,7 +13,7 @@
|
|||||||
//! format!("Hello {}! id:{}", info.0, info.1)
|
//! format!("Hello {}! id:{}", info.0, info.1)
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! #[actix_rt::main]
|
//! #[actix_web::main]
|
||||||
//! async fn main() -> std::io::Result<()> {
|
//! async fn main() -> std::io::Result<()> {
|
||||||
//! HttpServer::new(|| App::new().service(
|
//! HttpServer::new(|| App::new().service(
|
||||||
//! web::resource("/{name}/{id}/index.html").to(index))
|
//! web::resource("/{name}/{id}/index.html").to(index))
|
||||||
@ -80,9 +73,7 @@
|
|||||||
//! * `compress` - enables content encoding compression support (default enabled)
|
//! * `compress` - enables content encoding compression support (default enabled)
|
||||||
//! * `openssl` - enables ssl support via `openssl` crate, supports `http/2`
|
//! * `openssl` - enables ssl support via `openssl` crate, supports `http/2`
|
||||||
//! * `rustls` - enables ssl support via `rustls` crate, supports `http/2`
|
//! * `rustls` - enables ssl support via `rustls` crate, supports `http/2`
|
||||||
//! * `secure-cookies` - enables secure cookies support, includes `ring` crate as
|
//! * `secure-cookies` - enables secure cookies support
|
||||||
//! dependency
|
|
||||||
#![allow(clippy::type_complexity, clippy::new_without_default)]
|
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
mod app_service;
|
mod app_service;
|
||||||
@ -106,13 +97,12 @@ pub mod test;
|
|||||||
mod types;
|
mod types;
|
||||||
pub mod web;
|
pub mod web;
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub use actix_web_codegen::*;
|
pub use actix_web_codegen::*;
|
||||||
|
pub use actix_rt as rt;
|
||||||
|
|
||||||
// re-export for convenience
|
// re-export for convenience
|
||||||
pub use actix_http::Response as HttpResponse;
|
pub use actix_http::Response as HttpResponse;
|
||||||
pub use actix_http::{body, cookie, http, Error, HttpMessage, ResponseError, Result};
|
pub use actix_http::{body, cookie, http, Error, HttpMessage, ResponseError, Result};
|
||||||
pub use actix_macros::{main, test as test_rt};
|
|
||||||
|
|
||||||
pub use crate::app::App;
|
pub use crate::app::App;
|
||||||
pub use crate::extract::FromRequest;
|
pub use crate::extract::FromRequest;
|
||||||
@ -230,6 +220,7 @@ pub mod client {
|
|||||||
//! println!("Response: {:?}", response);
|
//! println!("Response: {:?}", response);
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
pub use awc::error::{
|
pub use awc::error::{
|
||||||
ConnectError, InvalidUrl, PayloadError, SendRequestError, WsClientError,
|
ConnectError, InvalidUrl, PayloadError, SendRequestError, WsClientError,
|
||||||
};
|
};
|
||||||
|
@ -90,6 +90,7 @@ where
|
|||||||
self.service.poll_ready(cx)
|
self.service.poll_ready(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::borrow_interior_mutable_const)]
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
||||||
// negotiate content-encoding
|
// negotiate content-encoding
|
||||||
let encoding = if let Some(val) = req.headers().get(&ACCEPT_ENCODING) {
|
let encoding = if let Some(val) = req.headers().get(&ACCEPT_ENCODING) {
|
||||||
|
@ -128,6 +128,7 @@ where
|
|||||||
self.service.poll_ready(cx)
|
self.service.poll_ready(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::borrow_interior_mutable_const)]
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
||||||
let inner = self.inner.clone();
|
let inner = self.inner.clone();
|
||||||
let fut = self.service.call(req);
|
let fut = self.service.call(req);
|
||||||
|
@ -478,7 +478,7 @@ impl FormatText {
|
|||||||
}
|
}
|
||||||
FormatText::RemoteAddr => {
|
FormatText::RemoteAddr => {
|
||||||
let s = if let Some(ref peer) = req.connection_info().remote_addr() {
|
let s = if let Some(ref peer) = req.connection_info().remote_addr() {
|
||||||
FormatText::Str(peer.to_string())
|
FormatText::Str((*peer).to_string())
|
||||||
} else {
|
} else {
|
||||||
FormatText::Str("-".to_string())
|
FormatText::Str("-".to_string())
|
||||||
};
|
};
|
||||||
|
@ -46,6 +46,7 @@ pub struct Route {
|
|||||||
|
|
||||||
impl Route {
|
impl Route {
|
||||||
/// Create new route which matches any request.
|
/// Create new route which matches any request.
|
||||||
|
#[allow(clippy::new_without_default)]
|
||||||
pub fn new() -> Route {
|
pub fn new() -> Route {
|
||||||
Route {
|
Route {
|
||||||
service: Box::new(RouteNewService::new(Extract::new(Handler::new(|| {
|
service: Box::new(RouteNewService::new(Extract::new(Handler::new(|| {
|
||||||
|
@ -252,6 +252,7 @@ pub struct UrlEncoded<U> {
|
|||||||
fut: Option<LocalBoxFuture<'static, Result<U, UrlencodedError>>>,
|
fut: Option<LocalBoxFuture<'static, Result<U, UrlencodedError>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::borrow_interior_mutable_const)]
|
||||||
impl<U> UrlEncoded<U> {
|
impl<U> UrlEncoded<U> {
|
||||||
/// Create a new future to URL encode a request
|
/// Create a new future to URL encode a request
|
||||||
pub fn new(req: &HttpRequest, payload: &mut Payload) -> UrlEncoded<U> {
|
pub fn new(req: &HttpRequest, payload: &mut Payload) -> UrlEncoded<U> {
|
||||||
|
@ -319,6 +319,7 @@ where
|
|||||||
U: DeserializeOwned + 'static,
|
U: DeserializeOwned + 'static,
|
||||||
{
|
{
|
||||||
/// Create `JsonBody` for request.
|
/// Create `JsonBody` for request.
|
||||||
|
#[allow(clippy::borrow_interior_mutable_const)]
|
||||||
pub fn new(
|
pub fn new(
|
||||||
req: &HttpRequest,
|
req: &HttpRequest,
|
||||||
payload: &mut Payload,
|
payload: &mut Payload,
|
||||||
|
@ -315,6 +315,7 @@ pub struct HttpMessageBody {
|
|||||||
|
|
||||||
impl HttpMessageBody {
|
impl HttpMessageBody {
|
||||||
/// Create `MessageBody` for request.
|
/// Create `MessageBody` for request.
|
||||||
|
#[allow(clippy::borrow_interior_mutable_const)]
|
||||||
pub fn new(req: &HttpRequest, payload: &mut dev::Payload) -> HttpMessageBody {
|
pub fn new(req: &HttpRequest, payload: &mut dev::Payload) -> HttpMessageBody {
|
||||||
let mut len = None;
|
let mut len = None;
|
||||||
if let Some(l) = req.headers().get(&header::CONTENT_LENGTH) {
|
if let Some(l) = req.headers().get(&header::CONTENT_LENGTH) {
|
||||||
|
@ -14,7 +14,6 @@ categories = ["network-programming", "asynchronous",
|
|||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
exclude = [".gitignore", ".cargo/config"]
|
exclude = [".gitignore", ".cargo/config"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
features = []
|
features = []
|
||||||
|
Loading…
Reference in New Issue
Block a user