mirror of
https://github.com/actix/actix-website
synced 2025-02-02 12:19:04 +01:00
Final fixes before requesting review.
This commit is contained in:
parent
cbf046b1f0
commit
5133ab874d
@ -43,7 +43,7 @@ as the first application, it would match all incoming requests.
|
|||||||
## State
|
## State
|
||||||
|
|
||||||
Application state is shared with all routes and resources within the same scope. State
|
Application state is shared with all routes and resources within the same scope. State
|
||||||
can be accessed with `web::Data<State>` as read-only, but interior mutability with
|
can be accessed with the `web::Data<State>` extractor as read-only, but interior mutability with
|
||||||
`Cell` can be used to achieve state mutability. State is also available for route
|
`Cell` can be used to achieve state mutability. State is also available for route
|
||||||
matching guards and middlewares.
|
matching guards and middlewares.
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ converted into an `HttpInternalServerError`:
|
|||||||
```rust
|
```rust
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
fn index(req: &HttpRequest) -> io::Result<fs::NamedFile> {
|
fn index(_req: HttpRequest) -> io::Result<fs::NamedFile> {
|
||||||
Ok(fs::NamedFile::open("static/index.html")?)
|
Ok(fs::NamedFile::open("static/index.html")?)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -22,13 +22,13 @@ such as `&'static str`, `String`, etc.
|
|||||||
Examples of valid handlers:
|
Examples of valid handlers:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn index(req: &HttpRequest) -> &'static str {
|
fn index(_req: HttpRequest) -> &'static str {
|
||||||
"Hello world!"
|
"Hello world!"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn index(req: HttpRequest) -> String {
|
fn index(_req: HttpRequest) -> String {
|
||||||
"Hello world!".to_owned()
|
"Hello world!".to_owned()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -37,7 +37,7 @@ You can also change the signature to return `impl Responder` which works well if
|
|||||||
complex types are involved.
|
complex types are involved.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn index(req: HttpRequest) -> impl Responder {
|
fn index(_req: HttpRequest) -> impl Responder {
|
||||||
Bytes::from_static("Hello world!")
|
Bytes::from_static("Hello world!")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -49,9 +49,7 @@ is decompressed.
|
|||||||
|
|
||||||
# Multipart body
|
# Multipart body
|
||||||
|
|
||||||
Actix provides multipart stream support with an external crate, [`actix-multipart`][multipartcrate].
|
Actix-web provides multipart stream support with an external crate, [`actix-multipart`][multipartcrate].
|
||||||
|
|
||||||
The following demonstrates multipart stream handling for a simple form:
|
|
||||||
|
|
||||||
> A full example is available in the [examples directory][multipartexample].
|
> A full example is available in the [examples directory][multipartexample].
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ against a URL path pattern. `path` represents the path portion of the URL that w
|
|||||||
The way that *actix-web* does this is very simple. When a request enters the system,
|
The way that *actix-web* does this is very simple. When a request enters the system,
|
||||||
for each resource configuration declaration present in the system, actix checks
|
for each resource configuration declaration present in the system, actix checks
|
||||||
the request's path against the pattern declared. This checking happens in the order that
|
the request's path against the pattern declared. This checking happens in the order that
|
||||||
the routes were declared via `App::resource()` method. If resource can not be found,
|
the routes were declared via `App::service()` method. If resource can not be found,
|
||||||
the *default resource* is used as the matched resource.
|
the *default resource* is used as the matched resource.
|
||||||
|
|
||||||
When a route configuration is declared, it may contain route guard arguments. All route
|
When a route configuration is declared, it may contain route guard arguments. All route
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
// <setup>
|
// <setup>
|
||||||
use actix_web::{web, App, HttpRequest, Responder};
|
use actix_web::{web, App, Responder};
|
||||||
|
|
||||||
fn index(_req: HttpRequest) -> impl Responder {
|
fn index() -> impl Responder {
|
||||||
"Hello world!"
|
"Hello world!"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,12 +3,10 @@ fn is_error() -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// <async-stream>
|
// <async-stream>
|
||||||
use actix_web::{error, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
use actix_web::{error, Error, HttpResponse};
|
||||||
use futures::future::{result, Future};
|
use futures::future::{result, Future};
|
||||||
|
|
||||||
fn index(
|
fn index() -> Result<Box<Future<Item = HttpResponse, Error = Error>>, Error> {
|
||||||
_req: HttpRequest,
|
|
||||||
) -> Result<Box<Future<Item = HttpResponse, Error = Error>>, Error> {
|
|
||||||
if is_error() {
|
if is_error() {
|
||||||
Err(error::ErrorBadRequest("bad request"))
|
Err(error::ErrorBadRequest("bad request"))
|
||||||
} else {
|
} else {
|
||||||
@ -20,6 +18,8 @@ fn index(
|
|||||||
// </async-stream>
|
// </async-stream>
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| App::new().route("/", web::to_async(index)))
|
HttpServer::new(|| App::new().route("/", web::to_async(index)))
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -1,22 +1,30 @@
|
|||||||
pub mod async_stream;
|
pub mod async_stream;
|
||||||
pub mod stream;
|
pub mod stream;
|
||||||
// <async-responder>
|
// <async-responder>
|
||||||
use actix_web::{web, App, Error, HttpRequest, HttpResponse};
|
use actix_web::{Error, HttpResponse};
|
||||||
use futures::future::{ok, Future};
|
use futures::future::{ok, Future};
|
||||||
|
|
||||||
fn index(_req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
|
fn index() -> Box<Future<Item = HttpResponse, Error = Error>> {
|
||||||
Box::new(ok::<_, Error>(
|
Box::new(ok::<_, Error>(
|
||||||
HttpResponse::Ok().content_type("text/html").body("Hello!"),
|
HttpResponse::Ok().content_type("text/html").body("Hello!"),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn index2(_req: HttpRequest) -> Box<Future<Item = &'static str, Error = Error>> {
|
fn index2() -> Box<Future<Item = &'static str, Error = Error>> {
|
||||||
Box::new(ok::<_, Error>("Welcome!"))
|
Box::new(ok::<_, Error>("Welcome!"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.route("/async", web::to_async(index))
|
.route("/async", web::to_async(index))
|
||||||
.route("/", web::to_async(index2));
|
.route("/", web::to_async(index2))
|
||||||
|
})
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
|
.unwrap()
|
||||||
|
.run()
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
// </async-responder>
|
// </async-responder>
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
// <stream>
|
// <stream>
|
||||||
use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
use actix_web::{Error, HttpResponse};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures::stream::once;
|
use futures::stream::once;
|
||||||
|
|
||||||
fn index(_req: HttpRequest) -> HttpResponse {
|
fn index() -> HttpResponse {
|
||||||
let body = once::<Bytes, Error>(Ok(Bytes::from_static(b"test")));
|
let body = once::<Bytes, Error>(Ok(Bytes::from_static(b"test")));
|
||||||
|
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
@ -12,6 +12,8 @@ fn index(_req: HttpRequest) -> HttpResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| App::new().route("/async", web::to_async(index)))
|
HttpServer::new(|| App::new().route("/async", web::to_async(index)))
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
// <either>
|
// <either>
|
||||||
use actix_web::{web, App, Either, Error, HttpRequest, HttpResponse};
|
use actix_web::{Either, Error, HttpResponse};
|
||||||
use futures::future::{ok, Future};
|
use futures::future::{ok, Future};
|
||||||
|
|
||||||
type RegisterResult =
|
type RegisterResult =
|
||||||
Either<HttpResponse, Box<Future<Item = HttpResponse, Error = Error>>>;
|
Either<HttpResponse, Box<Future<Item = HttpResponse, Error = Error>>>;
|
||||||
|
|
||||||
fn index(_req: HttpRequest) -> RegisterResult {
|
fn index() -> RegisterResult {
|
||||||
if is_a_variant() {
|
if is_a_variant() {
|
||||||
// <- choose variant A
|
// <- choose variant A
|
||||||
Either::A(HttpResponse::BadRequest().body("Bad data"))
|
Either::A(HttpResponse::BadRequest().body("Bad data"))
|
||||||
@ -20,7 +20,13 @@ fn index(_req: HttpRequest) -> RegisterResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new().route("/", web::get().to(index));
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
|
.unwrap()
|
||||||
|
.run()
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
// </either>
|
// </either>
|
||||||
|
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
use actix_web::{web, App};
|
use actix_web::{web, App};
|
||||||
// <helpers>
|
// <helpers>
|
||||||
use actix_web::{error, HttpRequest, Result};
|
use actix_web::{error, Result};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct MyError {
|
struct MyError {
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn index(_req: HttpRequest) -> Result<&'static str> {
|
pub fn index() -> Result<&'static str> {
|
||||||
let result: Result<&'static str, MyError> = Err(MyError { name: "test error" });
|
let result: Result<&'static str, MyError> = Err(MyError { name: "test error" });
|
||||||
|
|
||||||
Ok(result.map_err(|e| error::ErrorBadRequest(e.name))?)
|
Ok(result.map_err(|e| error::ErrorBadRequest(e.name))?)
|
||||||
|
@ -16,7 +16,7 @@ pub struct MyError {
|
|||||||
// Use default implementation for `error_response()` method
|
// Use default implementation for `error_response()` method
|
||||||
impl error::ResponseError for MyError {}
|
impl error::ResponseError for MyError {}
|
||||||
|
|
||||||
fn index(_req: HttpRequest) -> Result<&'static str, MyError> {
|
fn index() -> Result<&'static str, MyError> {
|
||||||
Err(MyError { name: "test" })
|
Err(MyError { name: "test" })
|
||||||
}
|
}
|
||||||
// </response-error>
|
// </response-error>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use actix_web::{web, App};
|
use actix_web::{web, App};
|
||||||
// <override>
|
// <override>
|
||||||
use actix_web::{error, http, HttpRequest, HttpResponse};
|
use actix_web::{error, http, HttpResponse};
|
||||||
use failure::Fail;
|
use failure::Fail;
|
||||||
|
|
||||||
#[derive(Fail, Debug)]
|
#[derive(Fail, Debug)]
|
||||||
@ -25,16 +25,16 @@ impl error::ResponseError for MyError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn index(_req: HttpRequest) -> Result<&'static str, MyError> {
|
fn index() -> Result<&'static str, MyError> {
|
||||||
Err(MyError::BadClientData)
|
Err(MyError::BadClientData)
|
||||||
}
|
}
|
||||||
// </override>
|
// </override>
|
||||||
|
|
||||||
fn error2(_req: HttpRequest) -> Result<&'static str, MyError> {
|
fn error2() -> Result<&'static str, MyError> {
|
||||||
Err(MyError::InternalError)
|
Err(MyError::InternalError)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn error3(_req: HttpRequest) -> Result<&'static str, MyError> {
|
fn error3() -> Result<&'static str, MyError> {
|
||||||
Err(MyError::Timeout)
|
Err(MyError::Timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <form>
|
// <form>
|
||||||
use actix_web::{web, App, HttpServer, Result};
|
use actix_web::{web, Result};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -16,6 +16,8 @@ fn index(form: web::Form<FormData>) -> Result<String> {
|
|||||||
// </form>
|
// </form>
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <json-one>
|
// <json-one>
|
||||||
use actix_web::{web, App, HttpServer, Result};
|
use actix_web::{web, Result};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -14,6 +14,8 @@ fn index(info: web::Json<Info>) -> Result<String> {
|
|||||||
// </json-one>
|
// </json-one>
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <json-two>
|
// <json-two>
|
||||||
use actix_web::{error, web, App, FromRequest, HttpResponse, HttpServer, Responder};
|
use actix_web::{error, web, FromRequest, HttpResponse, Responder};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -13,6 +13,8 @@ fn index(info: web::Json<Info>) -> impl Responder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <multi>
|
// <multi>
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::web;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -15,6 +15,8 @@ fn index((path, query): (web::Path<(u32, String)>, web::Query<Info>)) -> String
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new().route(
|
App::new().route(
|
||||||
"/users/{userid}/{friend}", // <- define path parameters
|
"/users/{userid}/{friend}", // <- define path parameters
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <path-one>
|
// <path-one>
|
||||||
use actix_web::{web, App, HttpServer, Result};
|
use actix_web::{web, Result};
|
||||||
|
|
||||||
/// extract path info from "/users/{userid}/{friend}" url
|
/// extract path info from "/users/{userid}/{friend}" url
|
||||||
/// {userid} - - deserializes to a u32
|
/// {userid} - - deserializes to a u32
|
||||||
@ -9,6 +9,8 @@ fn index(info: web::Path<(u32, String)>) -> Result<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new().route(
|
App::new().route(
|
||||||
"/users/{userid}/{friend}", // <- define path parameters
|
"/users/{userid}/{friend}", // <- define path parameters
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use actix_web::{web, App, HttpRequest, HttpServer, Result};
|
use actix_web::{web, HttpRequest, Result};
|
||||||
|
|
||||||
// <path-three>
|
// <path-three>
|
||||||
fn index(req: HttpRequest) -> Result<String> {
|
fn index(req: HttpRequest) -> Result<String> {
|
||||||
@ -9,6 +9,8 @@ fn index(req: HttpRequest) -> Result<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new().route(
|
App::new().route(
|
||||||
"/users/{userid}/{friend}", // <- define path parameters
|
"/users/{userid}/{friend}", // <- define path parameters
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <path-two>
|
// <path-two>
|
||||||
use actix_web::{web, App, HttpServer, Result};
|
use actix_web::{web, Result};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -14,6 +14,8 @@ fn index(info: web::Path<Info>) -> Result<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new().route(
|
App::new().route(
|
||||||
"/users/{userid}/{friend}", // <- define path parameters
|
"/users/{userid}/{friend}", // <- define path parameters
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <query>
|
// <query>
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::web;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -14,6 +14,8 @@ fn index(info: web::Query<Info>) -> String {
|
|||||||
// </query>
|
// </query>
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
// <setup>
|
// <setup>
|
||||||
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
|
||||||
|
|
||||||
fn index() -> impl Responder {
|
fn index() -> impl Responder {
|
||||||
HttpResponse::Ok().body("Hello world!")
|
HttpResponse::Ok().body("Hello world!")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn index2(_req: HttpRequest) -> impl Responder {
|
fn index2() -> impl Responder {
|
||||||
HttpResponse::Ok().body("Hello world again!")
|
HttpResponse::Ok().body("Hello world again!")
|
||||||
}
|
}
|
||||||
// </setup>
|
// </setup>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <arc>
|
// <arc>
|
||||||
use actix_web::{web, App, HttpServer, Responder};
|
use actix_web::{web, Responder};
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@ -19,6 +19,8 @@ fn add_one(data: web::Data<AppState>) -> impl Responder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
let data = AppState {
|
let data = AppState {
|
||||||
count: Arc::new(AtomicUsize::new(0)),
|
count: Arc::new(AtomicUsize::new(0)),
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
pub mod handlers_arc;
|
pub mod handlers_arc;
|
||||||
// <data>
|
// <data>
|
||||||
use actix_web::{web, App, HttpServer, Responder};
|
use actix_web::{web, Responder};
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -20,6 +20,8 @@ fn add_one(data: web::Data<AppState>) -> impl Responder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
let data = AppState {
|
let data = AppState {
|
||||||
count: Cell::new(0),
|
count: Cell::new(0),
|
||||||
};
|
};
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <responder-trait>
|
// <responder-trait>
|
||||||
use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder};
|
use actix_web::{Error, HttpRequest, HttpResponse, Responder};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@ -28,6 +28,8 @@ fn index() -> impl Responder {
|
|||||||
// </responder-trait>
|
// </responder-trait>
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// <chunked>
|
// // <chunked>
|
||||||
// use actix_web::{web, HttpRequest, HttpResponse};
|
// use actix_web::{web, HttpRequest, HttpResponse};
|
||||||
// use bytes::Bytes;
|
// use bytes::Bytes;
|
||||||
// use futures::stream::once;
|
// use futures::stream::once;
|
||||||
@ -10,7 +10,7 @@
|
|||||||
// b"data",
|
// b"data",
|
||||||
// ))))))
|
// ))))))
|
||||||
// }
|
// }
|
||||||
// </chunked>
|
// // </chunked>
|
||||||
|
|
||||||
// pub fn main() {
|
// pub fn main() {
|
||||||
// use actix_web::{web, App, HttpServer};
|
// use actix_web::{web, App, HttpServer};
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
// <identity-two>
|
// <identity-two>
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
http::ContentEncoding, middleware, middleware::BodyEncoding, HttpRequest,
|
http::ContentEncoding, middleware, middleware::BodyEncoding, HttpResponse,
|
||||||
HttpResponse,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static HELLO_WORLD: &[u8] = &[
|
static HELLO_WORLD: &[u8] = &[
|
||||||
@ -10,7 +9,7 @@ static HELLO_WORLD: &[u8] = &[
|
|||||||
0x0c, 0x00, 0x00, 0x00,
|
0x0c, 0x00, 0x00, 0x00,
|
||||||
];
|
];
|
||||||
|
|
||||||
pub fn index(_req: HttpRequest) -> HttpResponse {
|
pub fn index() -> HttpResponse {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.encoding(ContentEncoding::Identity)
|
.encoding(ContentEncoding::Identity)
|
||||||
.header("content-encoding", "gzip")
|
.header("content-encoding", "gzip")
|
||||||
|
@ -7,11 +7,10 @@ pub mod identity_two;
|
|||||||
pub mod json_resp;
|
pub mod json_resp;
|
||||||
|
|
||||||
// <builder>
|
// <builder>
|
||||||
use actix_web::{http::ContentEncoding, middleware::BodyEncoding, HttpResponse};
|
use actix_web::HttpResponse;
|
||||||
|
|
||||||
fn index() -> HttpResponse {
|
fn index() -> HttpResponse {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.encoding(ContentEncoding::Br)
|
|
||||||
.content_type("plain/text")
|
.content_type("plain/text")
|
||||||
.header("X-Hdr", "sample")
|
.header("X-Hdr", "sample")
|
||||||
.body("data")
|
.body("data")
|
||||||
|
@ -4,7 +4,7 @@ pub mod directory;
|
|||||||
|
|
||||||
// <individual-file>
|
// <individual-file>
|
||||||
use actix_files::NamedFile;
|
use actix_files::NamedFile;
|
||||||
use actix_web::{web, App, HttpRequest, HttpServer, Result};
|
use actix_web::{HttpRequest, Result};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
fn index(req: HttpRequest) -> Result<NamedFile> {
|
fn index(req: HttpRequest) -> Result<NamedFile> {
|
||||||
@ -13,6 +13,8 @@ fn index(req: HttpRequest) -> Result<NamedFile> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| App::new().route("/{filename:.*}", web::get().to(index)))
|
HttpServer::new(|| App::new().route("/{filename:.*}", web::get().to(index)))
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
// <guard>
|
// <guard>
|
||||||
use actix_web::{
|
use actix_web::{dev::RequestHead, guard::Guard, http, HttpResponse};
|
||||||
dev::RequestHead, guard::Guard, http, web, App, HttpResponse, HttpServer,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ContentTypeHeader;
|
struct ContentTypeHeader;
|
||||||
|
|
||||||
@ -12,6 +10,8 @@ impl Guard for ContentTypeHeader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new().route(
|
App::new().route(
|
||||||
"/",
|
"/",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <minfo>
|
// <minfo>
|
||||||
use actix_web::{web, App, HttpRequest, Result};
|
use actix_web::{HttpRequest, HttpResponse, Result};
|
||||||
|
|
||||||
fn index(req: HttpRequest) -> Result<String> {
|
fn index(req: HttpRequest) -> Result<String> {
|
||||||
let v1: u8 = req.match_info().get("v1").unwrap().parse().unwrap();
|
let v1: u8 = req.match_info().get("v1").unwrap().parse().unwrap();
|
||||||
@ -9,12 +9,12 @@ fn index(req: HttpRequest) -> Result<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
use actix_web::HttpServer;
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.route("/a/{v1}/{v2}/", web::get().to(index))
|
.route("/a/{v1}/{v2}/", web::get().to(index))
|
||||||
.route("", web::get().to(|| actix_web::HttpResponse::Ok()))
|
.route("", web::get().to(|| HttpResponse::Ok()))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
// <norm>
|
// <norm>
|
||||||
use actix_web::{middleware, web, App, HttpResponse};
|
use actix_web::{middleware, HttpResponse};
|
||||||
|
|
||||||
fn index() -> HttpResponse {
|
fn index() -> HttpResponse {
|
||||||
HttpResponse::Ok().body("Hello")
|
HttpResponse::Ok().body("Hello")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
use actix_web::HttpServer;
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
|
use actix_web::HttpResponse;
|
||||||
|
|
||||||
|
fn index() -> HttpResponse {
|
||||||
|
HttpResponse::Ok().body("Hello")
|
||||||
|
}
|
||||||
|
|
||||||
// <norm>
|
// <norm>
|
||||||
use actix_web::{http::Method, middleware, web, App, HttpServer};
|
use actix_web::{http::Method, middleware, web, App, HttpServer};
|
||||||
|
|
||||||
@ -14,9 +20,3 @@ pub fn main() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
// </norm>
|
// </norm>
|
||||||
|
|
||||||
use actix_web::HttpResponse;
|
|
||||||
|
|
||||||
fn index() -> HttpResponse {
|
|
||||||
HttpResponse::Ok().body("Hello")
|
|
||||||
}
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
// <path>
|
// <path>
|
||||||
use actix_web::{web, App, Result};
|
use actix_web::{web, Result};
|
||||||
|
|
||||||
fn index(info: web::Path<(String, u32)>) -> Result<String> {
|
fn index(info: web::Path<(String, u32)>) -> Result<String> {
|
||||||
Ok(format!("Welcome {}! id: {}", info.0, info.1))
|
Ok(format!("Welcome {}! id: {}", info.0, info.1))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
use actix_web::HttpServer;
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new().route(
|
App::new().route(
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <path>
|
// <path>
|
||||||
use actix_web::{web, App, Result};
|
use actix_web::{web, Result};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -13,7 +13,7 @@ fn index(info: web::Path<Info>) -> Result<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
use actix_web::HttpServer;
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new().route(
|
App::new().route(
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <pbuf>
|
// <pbuf>
|
||||||
use actix_web::{web, App, HttpRequest, Result};
|
use actix_web::{HttpRequest, Result};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
fn index(req: HttpRequest) -> Result<String> {
|
fn index(req: HttpRequest) -> Result<String> {
|
||||||
@ -8,7 +8,7 @@ fn index(req: HttpRequest) -> Result<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
use actix_web::HttpServer;
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| App::new().route(r"/a/{tail:.*}", web::get().to(index)))
|
HttpServer::new(|| App::new().route(r"/a/{tail:.*}", web::get().to(index)))
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")
|
||||||
|
@ -5,8 +5,8 @@ fn show_users() -> HttpResponse {
|
|||||||
HttpResponse::Ok().body("Show users")
|
HttpResponse::Ok().body("Show users")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn user_detail(_path: web::Path<(u32,)>) -> HttpResponse {
|
fn user_detail(path: web::Path<(u32,)>) -> HttpResponse {
|
||||||
HttpResponse::Ok().body("User detail")
|
HttpResponse::Ok().body(format!("User detail: {}", path.0))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <ext>
|
// <ext>
|
||||||
use actix_web::{web, App, HttpRequest, Responder};
|
use actix_web::{HttpRequest, Responder};
|
||||||
|
|
||||||
fn index(req: HttpRequest) -> impl Responder {
|
fn index(req: HttpRequest) -> impl Responder {
|
||||||
let url = req.url_for("youtube", &["oHg5SJYRHA0"]).unwrap();
|
let url = req.url_for("youtube", &["oHg5SJYRHA0"]).unwrap();
|
||||||
@ -9,7 +9,7 @@ fn index(req: HttpRequest) -> impl Responder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
use actix_web::HttpServer;
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// <url>
|
// <url>
|
||||||
use actix_web::{guard, http::header, web, App, HttpRequest, HttpResponse, Result};
|
use actix_web::{guard, http::header, HttpRequest, HttpResponse, Result};
|
||||||
|
|
||||||
fn index(req: HttpRequest) -> Result<HttpResponse> {
|
fn index(req: HttpRequest) -> Result<HttpResponse> {
|
||||||
let url = req.url_for("foo", &["1", "2", "3"])?; // <- generate url for "foo" resource
|
let url = req.url_for("foo", &["1", "2", "3"])?; // <- generate url for "foo" resource
|
||||||
@ -10,7 +10,7 @@ fn index(req: HttpRequest) -> Result<HttpResponse> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
use actix_web::HttpServer;
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
|
@ -70,7 +70,6 @@ fn main() {
|
|||||||
to return consistent responses from your APIs.
|
to return consistent responses from your APIs.
|
||||||
</p>
|
</p>
|
||||||
{{ highlight `#[derive(Serialize)]
|
{{ highlight `#[derive(Serialize)]
|
||||||
#[derive(Serialize)]
|
|
||||||
struct Measurement {
|
struct Measurement {
|
||||||
temperature: f32,
|
temperature: f32,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user