mirror of
https://github.com/actix/actix-website
synced 2025-06-27 15:39:02 +02:00
v3 (#188)
This commit is contained in:
@ -1,30 +1,26 @@
|
||||
[workspace]
|
||||
members = [
|
||||
"application",
|
||||
"async-handlers",
|
||||
"easy-form-handling",
|
||||
"either",
|
||||
"errors",
|
||||
"extractors",
|
||||
"flexible-responders",
|
||||
"getting-started",
|
||||
"main-example",
|
||||
"powerful-extractors",
|
||||
"request-routing",
|
||||
"server",
|
||||
"url-dispatch",
|
||||
"responder-trait",
|
||||
"either",
|
||||
"extractors",
|
||||
"autoreload",
|
||||
"errors",
|
||||
"requests",
|
||||
"responses",
|
||||
"middleware",
|
||||
"static-files",
|
||||
"http2",
|
||||
"testing",
|
||||
"async-handlers",
|
||||
"websockets",
|
||||
"main-example",
|
||||
"middleware",
|
||||
"powerful-extractors",
|
||||
"request-handlers",
|
||||
"request-routing",
|
||||
"requests",
|
||||
"responder-trait",
|
||||
"responses",
|
||||
"server",
|
||||
"static-files",
|
||||
"testing",
|
||||
"url-dispatch",
|
||||
"websockets",
|
||||
]
|
||||
exclude = [
|
||||
"og_databases",
|
||||
"sentry",
|
||||
]
|
||||
exclude = ["databases", "sentry"]
|
||||
|
@ -5,6 +5,5 @@ edition = "2018"
|
||||
workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-service = "1.0"
|
||||
actix-web = "3"
|
||||
actix-service = "1"
|
||||
|
@ -1,18 +1,23 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
// <setup>
|
||||
use actix_web::{web, App, Responder, HttpServer};
|
||||
use actix_web::{web, App, HttpServer, Responder};
|
||||
|
||||
async fn index() -> impl Responder {
|
||||
"Hello world!"
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new().service(
|
||||
web::scope("/app").route("/index.html", web::get().to(index)),
|
||||
// prefixes all resources and routes attached to it...
|
||||
web::scope("/app")
|
||||
// ...so this handles requests for `GET /app/index.html`
|
||||
.route("/index.html", web::get().to(index)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,24 +1,27 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
// <combine>
|
||||
struct State1;
|
||||
struct State2;
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(
|
||||
web::scope("/app1")
|
||||
.data(State1)
|
||||
.route("/", web::to(|| HttpResponse::Ok())))
|
||||
.route("/", web::to(|| HttpResponse::Ok())),
|
||||
)
|
||||
.service(
|
||||
web::scope("/app2")
|
||||
.data(State2)
|
||||
.route("/", web::to(|| HttpResponse::Ok())))
|
||||
.route("/", web::to(|| HttpResponse::Ok())),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
// <config>
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
@ -19,7 +21,7 @@ fn config(cfg: &mut web::ServiceConfig) {
|
||||
);
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
@ -27,7 +29,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(web::scope("/api").configure(scoped_config))
|
||||
.route("/", web::get().to(|| HttpResponse::Ok().body("/")))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ pub mod state;
|
||||
pub mod vh;
|
||||
|
||||
// <multi>
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
@ -20,7 +20,7 @@ async fn main() -> std::io::Result<()> {
|
||||
)
|
||||
.route("/", web::to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ async fn index(data: web::Data<AppStateWithCounter>) -> String {
|
||||
// </setup_mutable>
|
||||
|
||||
// <make_app_mutable>
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let counter = web::Data::new(AppStateWithCounter {
|
||||
counter: Mutex::new(0),
|
||||
@ -28,7 +28,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.app_data(counter.clone()) // <- register the created data
|
||||
.route("/", web::get().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,16 +1,14 @@
|
||||
use actix_web::{web, App, HttpRequest, Responder};
|
||||
use actix_web::{get, web, App, HttpRequest, Responder};
|
||||
|
||||
#[get("/show")]
|
||||
async fn show_users(_req: HttpRequest) -> impl Responder {
|
||||
"unimplemented!"
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
// <scope>
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() {
|
||||
App::new()
|
||||
.service(
|
||||
web::scope("/users")
|
||||
.route("/show", web::get().to(show_users)));
|
||||
let scope = web::scope("/users").service(show_users);
|
||||
App::new().service(scope);
|
||||
}
|
||||
// </scope>
|
||||
|
@ -1,12 +1,12 @@
|
||||
// <setup>
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use std::sync::Mutex;
|
||||
use actix_web::{get, web, App, HttpServer};
|
||||
|
||||
// This struct represents state
|
||||
struct AppState {
|
||||
app_name: String,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn index(data: web::Data<AppState>) -> String {
|
||||
let app_name = &data.app_name; // <- get app_name
|
||||
|
||||
@ -15,16 +15,16 @@ async fn index(data: web::Data<AppState>) -> String {
|
||||
// </setup>
|
||||
|
||||
// <start_app>
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.data(AppState {
|
||||
app_name: String::from("Actix-web"),
|
||||
})
|
||||
.route("/", web::get().to(index))
|
||||
.service(index)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use actix_web::{guard, web, App, HttpResponse, HttpServer};
|
||||
|
||||
// <vh>
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
@ -17,7 +17,7 @@ async fn main() -> std::io::Result<()> {
|
||||
)
|
||||
.route("/", web::to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ version = "2.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
futures = "0.3.1"
|
||||
bytes = "0.5"
|
||||
|
@ -1,10 +1,11 @@
|
||||
// <stream>
|
||||
use actix_web::{web, App, HttpServer, Error, HttpResponse};
|
||||
use actix_web::{get, App, Error, HttpResponse, HttpServer};
|
||||
use bytes::Bytes;
|
||||
use futures::stream::once;
|
||||
use futures::future::ok;
|
||||
use futures::stream::once;
|
||||
|
||||
async fn index() -> HttpResponse {
|
||||
#[get("/stream")]
|
||||
async fn stream() -> HttpResponse {
|
||||
let body = once(ok::<_, Error>(Bytes::from_static(b"test")));
|
||||
|
||||
HttpResponse::Ok()
|
||||
@ -12,10 +13,10 @@ async fn index() -> HttpResponse {
|
||||
.streaming(body)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().route("/async", web::to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
HttpServer::new(|| App::new().service(stream))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,9 +0,0 @@
|
||||
[package]
|
||||
name = "autoreload"
|
||||
version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
listenfd = "0.3"
|
@ -1,22 +0,0 @@
|
||||
// <autoreload>
|
||||
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
|
||||
use listenfd::ListenFd;
|
||||
|
||||
async fn index(_req: HttpRequest) -> impl Responder {
|
||||
"Hello World!"
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let mut listenfd = ListenFd::from_env();
|
||||
let mut server = HttpServer::new(|| App::new().route("/", web::get().to(index)));
|
||||
|
||||
server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() {
|
||||
server.listen(l)?
|
||||
} else {
|
||||
server.bind("127.0.0.1:3000")?
|
||||
};
|
||||
|
||||
server.run().await
|
||||
}
|
||||
// </autoreload>
|
7
examples/databases/Cargo.toml
Normal file
7
examples/databases/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "databases"
|
||||
version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
63
examples/databases/src/main.rs
Normal file
63
examples/databases/src/main.rs
Normal file
@ -0,0 +1,63 @@
|
||||
// <handler>
|
||||
fn insert_new_user(db: &SqliteConnection, user: CreateUser) -> Result<User, Error> {
|
||||
use self::schema::users::dsl::*;
|
||||
|
||||
// Create insertion model
|
||||
let uuid = format!("{}", uuid::Uuid::new_v4());
|
||||
let new_user = models::NewUser {
|
||||
id: &uuid,
|
||||
name: &user.name,
|
||||
};
|
||||
|
||||
// normal diesel operations
|
||||
diesel::insert_into(users)
|
||||
.values(&new_user)
|
||||
.execute(&self.0)
|
||||
.expect("Error inserting person");
|
||||
|
||||
let mut items = users
|
||||
.filter(id.eq(&uuid))
|
||||
.load::<models::User>(&self.0)
|
||||
.expect("Error loading person");
|
||||
|
||||
Ok(items.pop().unwrap())
|
||||
}
|
||||
// </handler>
|
||||
|
||||
// <main>
|
||||
type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
// Create connection pool
|
||||
let pool = r2d2::Pool::builder()
|
||||
.build(manager)
|
||||
.expect("Failed to create pool.");
|
||||
|
||||
// Start HTTP server
|
||||
HttpServer::new(move || {
|
||||
App::new::data(pool.clone())
|
||||
.resource("/{name}", web::get().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </main>
|
||||
|
||||
// <index>
|
||||
async fn index(req: web::Data<DbPool>, name: web::Path<(String)>) -> impl Responder {
|
||||
let name = name.into_inner();
|
||||
|
||||
let conn = pool.get().expect("couldn't get db connection from pool");
|
||||
|
||||
let user = web::block(move || actions::insert_new_user(&conn, &user))
|
||||
.await
|
||||
.map_err(|e| {
|
||||
eprintln!("{}", e);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
})?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(user))
|
||||
}
|
||||
// </index>
|
@ -4,6 +4,5 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
serde = "1.0"
|
||||
|
@ -18,14 +18,14 @@ async fn register(form: web::Form<Register>) -> impl Responder {
|
||||
format!("Hello {} from {}!", form.username, form.country)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/", web::get().to(index))
|
||||
.route("/register", web::post().to(register))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,5 +4,4 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "1.0"
|
||||
futures = "0.1"
|
||||
actix-web = "3"
|
||||
|
@ -3,7 +3,7 @@ use actix_web::{Either, Error, HttpResponse};
|
||||
|
||||
type RegisterResult = Either<HttpResponse, Result<&'static str, Error>>;
|
||||
|
||||
fn index() -> RegisterResult {
|
||||
async fn index() -> RegisterResult {
|
||||
if is_a_variant() {
|
||||
// <- choose variant A
|
||||
Either::A(HttpResponse::BadRequest().body("Bad data"))
|
||||
@ -14,14 +14,14 @@ fn index() -> RegisterResult {
|
||||
}
|
||||
// </either>
|
||||
|
||||
fn main() {
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
||||
fn is_a_variant() -> bool {
|
||||
|
@ -4,9 +4,8 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0.0"
|
||||
actix-rt = "1.0.0"
|
||||
env_logger = "0.7.1"
|
||||
log = "0.4.8"
|
||||
failure = "0.1.6"
|
||||
actix-http = "1.0.1"
|
||||
actix-web = "3"
|
||||
derive_more = "0.99"
|
||||
env_logger = "0.7"
|
||||
log = "0.4"
|
||||
# actix-http = "1"
|
||||
|
@ -1,12 +1,12 @@
|
||||
use actix_web::{web, App};
|
||||
// <helpers>
|
||||
use actix_web::{error, Result};
|
||||
use actix_web::{error, get, App, HttpServer, Result};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct MyError {
|
||||
name: &'static str,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> Result<&'static str> {
|
||||
let result: Result<&'static str, MyError> = Err(MyError { name: "test error" });
|
||||
|
||||
@ -14,12 +14,10 @@ async fn index() -> Result<&'static str> {
|
||||
}
|
||||
// </helpers>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
// <logging>
|
||||
use actix_web::{error, Result};
|
||||
use failure::Fail;
|
||||
use actix_web::{error, get, middleware::Logger, App, HttpServer, Result};
|
||||
use log::debug;
|
||||
use derive_more::{Display, Error};
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
#[fail(display = "my error")]
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "my error: {}", name)]
|
||||
pub struct MyError {
|
||||
name: &'static str,
|
||||
}
|
||||
@ -12,27 +12,22 @@ pub struct MyError {
|
||||
// Use default implementation for `error_response()` method
|
||||
impl error::ResponseError for MyError {}
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> Result<&'static str, MyError> {
|
||||
let err = MyError { name: "test error" };
|
||||
debug!("{}", err);
|
||||
Err(err)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{middleware::Logger, web, App, HttpServer};
|
||||
|
||||
std::env::set_var("RUST_LOG", "my_errors=debug,actix_web=info");
|
||||
std::env::set_var("RUST_BACKTRACE", "1");
|
||||
env_logger::init();
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(Logger::default())
|
||||
.route("/", web::get().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().wrap(Logger::default()).service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </logging>
|
||||
|
@ -6,10 +6,10 @@ pub mod recommend_two;
|
||||
|
||||
// <response-error>
|
||||
use actix_web::{error, Result};
|
||||
use failure::Fail;
|
||||
use derive_more::{Display, Error};
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
#[fail(display = "my error")]
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "my error: {}", name)]
|
||||
struct MyError {
|
||||
name: &'static str,
|
||||
}
|
||||
@ -22,12 +22,12 @@ async fn index() -> Result<&'static str, MyError> {
|
||||
}
|
||||
// </response-error>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,22 +1,24 @@
|
||||
use actix_web::{web, App};
|
||||
// <override>
|
||||
use actix_http::ResponseBuilder;
|
||||
use actix_web::{error, http::header, http::StatusCode, HttpResponse};
|
||||
use failure::Fail;
|
||||
use actix_web::{
|
||||
dev::HttpResponseBuilder, error, get, http::header, http::StatusCode, App, HttpResponse,
|
||||
};
|
||||
use derive_more::{Display, Error};
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
#[derive(Debug, Display, Error)]
|
||||
enum MyError {
|
||||
#[fail(display = "internal error")]
|
||||
#[display(fmt = "internal error")]
|
||||
InternalError,
|
||||
#[fail(display = "bad request")]
|
||||
|
||||
#[display(fmt = "bad request")]
|
||||
BadClientData,
|
||||
#[fail(display = "timeout")]
|
||||
|
||||
#[display(fmt = "timeout")]
|
||||
Timeout,
|
||||
}
|
||||
|
||||
impl error::ResponseError for MyError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
ResponseBuilder::new(self.status_code())
|
||||
HttpResponseBuilder::new(self.status_code())
|
||||
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
||||
.body(self.to_string())
|
||||
}
|
||||
@ -30,30 +32,28 @@ impl error::ResponseError for MyError {
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> Result<&'static str, MyError> {
|
||||
Err(MyError::BadClientData)
|
||||
}
|
||||
// </override>
|
||||
|
||||
#[get("/e2")]
|
||||
async fn error2() -> Result<&'static str, MyError> {
|
||||
Err(MyError::InternalError)
|
||||
}
|
||||
|
||||
#[get("/e3")]
|
||||
async fn error3() -> Result<&'static str, MyError> {
|
||||
Err(MyError::Timeout)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/", web::get().to(index))
|
||||
.route("/e2", web::get().to(error2))
|
||||
.route("/e3", web::get().to(error3))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().service(index).service(error2).service(error3))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,17 +1,19 @@
|
||||
// <recommend-one>
|
||||
use actix_http::ResponseBuilder;
|
||||
use actix_web::{error, http::header, http::StatusCode, HttpResponse};
|
||||
use failure::Fail;
|
||||
use actix_web::{
|
||||
dev::HttpResponseBuilder, error, get, http::header, http::StatusCode, App, HttpResponse,
|
||||
HttpServer,
|
||||
};
|
||||
use derive_more::{Display, Error};
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
#[derive(Debug, Display, Error)]
|
||||
enum UserError {
|
||||
#[fail(display = "Validation error on field: {}", field)]
|
||||
#[display(fmt = "Validation error on field: {}", field)]
|
||||
ValidationError { field: String },
|
||||
}
|
||||
|
||||
impl error::ResponseError for UserError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
ResponseBuilder::new(self.status_code())
|
||||
HttpResponseBuilder::new(self.status_code())
|
||||
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
||||
.body(self.to_string())
|
||||
}
|
||||
@ -22,18 +24,18 @@ impl error::ResponseError for UserError {
|
||||
}
|
||||
}
|
||||
// </recommend-one>
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> Result<&'static str, UserError> {
|
||||
Err(UserError::ValidationError {
|
||||
field: "bad stuff".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,17 +1,19 @@
|
||||
// <recommend-two>
|
||||
use actix_http::ResponseBuilder;
|
||||
use actix_web::{error, http::header, http::StatusCode, HttpResponse};
|
||||
use failure::Fail;
|
||||
use actix_web::{
|
||||
dev::HttpResponseBuilder, error, get, http::header, http::StatusCode, App, HttpResponse,
|
||||
HttpServer,
|
||||
};
|
||||
use derive_more::{Display, Error};
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
#[derive(Debug, Display, Error)]
|
||||
enum UserError {
|
||||
#[fail(display = "An internal error occurred. Please try again later.")]
|
||||
#[display(fmt = "An internal error occurred. Please try again later.")]
|
||||
InternalError,
|
||||
}
|
||||
|
||||
impl error::ResponseError for UserError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
ResponseBuilder::new(self.status_code())
|
||||
HttpResponseBuilder::new(self.status_code())
|
||||
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
||||
.body(self.to_string())
|
||||
}
|
||||
@ -22,22 +24,21 @@ impl error::ResponseError for UserError {
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> Result<&'static str, UserError> {
|
||||
do_thing_that_failes().map_err(|_e| UserError::InternalError)?;
|
||||
do_thing_that_fails().map_err(|_e| UserError::InternalError)?;
|
||||
Ok("success!")
|
||||
}
|
||||
// </recommend-two>
|
||||
|
||||
fn do_thing_that_failes() -> Result<(), std::io::Error> {
|
||||
fn do_thing_that_fails() -> Result<(), std::io::Error> {
|
||||
Err(std::io::Error::new(std::io::ErrorKind::Other, "some error"))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <form>
|
||||
use actix_web::{web, Result};
|
||||
use actix_web::{post, web, App, HttpServer, Result};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -10,17 +10,16 @@ struct FormData {
|
||||
/// extract form data using serde
|
||||
/// this handler gets called only if the content type is *x-www-form-urlencoded*
|
||||
/// and the content of the request could be deserialized to a `FormData` struct
|
||||
#[post("/")]
|
||||
async fn index(form: web::Form<FormData>) -> Result<String> {
|
||||
Ok(format!("Welcome {}!", form.username))
|
||||
}
|
||||
// </form>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <json-one>
|
||||
use actix_web::{web, Result};
|
||||
use actix_web::{get, web, App, HttpServer, Result};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -8,17 +8,16 @@ struct Info {
|
||||
}
|
||||
|
||||
/// deserialize `Info` from request's body
|
||||
#[get("/")]
|
||||
async fn index(info: web::Json<Info>) -> Result<String> {
|
||||
Ok(format!("Welcome {}!", info.username))
|
||||
}
|
||||
// </json-one>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
// <json-two>
|
||||
use actix_web::{error, web, FromRequest, HttpResponse, Responder};
|
||||
use actix_web::{error, web, App, FromRequest, HttpResponse, HttpServer, Responder};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -12,28 +14,24 @@ async fn index(info: web::Json<Info>) -> impl Responder {
|
||||
format!("Welcome {}!", info.username)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
let json_config = web::JsonConfig::default()
|
||||
.limit(4096)
|
||||
.error_handler(|err, _req| {
|
||||
// create custom error response
|
||||
error::InternalError::from_response(err, HttpResponse::Conflict().finish()).into()
|
||||
});
|
||||
|
||||
App::new().service(
|
||||
web::resource("/")
|
||||
// change json extractor configuration
|
||||
.app_data(web::Json::<Info>::configure(|cfg| {
|
||||
cfg.limit(4096).error_handler(|err, _req| {
|
||||
// create custom error response
|
||||
error::InternalError::from_response(
|
||||
err,
|
||||
HttpResponse::Conflict().finish(),
|
||||
)
|
||||
.into()
|
||||
})
|
||||
}))
|
||||
.app_data(json_config)
|
||||
.route(web::post().to(index)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -18,17 +18,18 @@ struct MyInfo {
|
||||
}
|
||||
|
||||
// <option-one>
|
||||
async fn index(
|
||||
path: web::Path<(String, String)>,
|
||||
json: web::Json<MyInfo>,
|
||||
) -> impl Responder {
|
||||
async fn index(path: web::Path<(String, String)>, json: web::Json<MyInfo>) -> impl Responder {
|
||||
let path = path.into_inner();
|
||||
format!("{} {} {} {}", path.0, path.1, json.id, json.username)
|
||||
}
|
||||
// </option-one>
|
||||
|
||||
// <option-two>
|
||||
async fn extract(req: HttpRequest) -> impl Responder {
|
||||
let params = web::Path::<(String, String)>::extract(&req).await.unwrap();
|
||||
let params = web::Path::<(String, String)>::extract(&req)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_inner();
|
||||
|
||||
let info = web::Json::<MyInfo>::extract(&req)
|
||||
.await
|
||||
@ -38,14 +39,14 @@ async fn extract(req: HttpRequest) -> impl Responder {
|
||||
}
|
||||
// </option-two>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/{name}/{id}", web::post().to(index))
|
||||
.route("/{name}/{id}/extract", web::post().to(extract))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <multi>
|
||||
use actix_web::web;
|
||||
use actix_web::{get, web};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -7,27 +7,24 @@ struct Info {
|
||||
username: String,
|
||||
}
|
||||
|
||||
#[get("/users/{user_id}/{friend}")] // <- define path parameters
|
||||
async fn index(
|
||||
(path, query): (web::Path<(u32, String)>, web::Query<Info>),
|
||||
web::Path((user_id, friend)): web::Path<(u32, String)>,
|
||||
query: web::Query<Info>,
|
||||
) -> String {
|
||||
format!(
|
||||
"Welcome {}, friend {}, userid {}!",
|
||||
query.username, path.1, path.0
|
||||
"Welcome {}, friend {}, user_id {}!",
|
||||
query.username, friend, user_id
|
||||
)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </multi>
|
||||
|
@ -1,25 +1,21 @@
|
||||
// <path-one>
|
||||
use actix_web::{web, Result};
|
||||
use actix_web::{get, web, Result};
|
||||
|
||||
/// extract path info from "/users/{userid}/{friend}" url
|
||||
/// {userid} - - deserializes to a u32
|
||||
/// extract path info from "/users/{user_id}/{friend}" url
|
||||
/// {user_id} - deserializes to a u32
|
||||
/// {friend} - deserializes to a String
|
||||
async fn index(info: web::Path<(u32, String)>) -> Result<String> {
|
||||
Ok(format!("Welcome {}, userid {}!", info.1, info.0))
|
||||
#[get("/users/{user_id}/{friend}")] // <- define path parameters
|
||||
async fn index(web::Path((user_id, friend)): web::Path<(u32, String)>) -> Result<String> {
|
||||
Ok(format!("Welcome {}, user_id {}!", friend, user_id))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </path-one>
|
||||
|
@ -1,26 +1,21 @@
|
||||
use actix_web::{web, HttpRequest, Result};
|
||||
use actix_web::{get, HttpRequest, Result};
|
||||
|
||||
// <path-three>
|
||||
#[get("/users/{userid}/{friend}")] // <- define path parameters
|
||||
async fn index(req: HttpRequest) -> Result<String> {
|
||||
let name: String =
|
||||
req.match_info().get("friend").unwrap().parse().unwrap();
|
||||
let name: String = req.match_info().get("friend").unwrap().parse().unwrap();
|
||||
let userid: i32 = req.match_info().query("userid").parse().unwrap();
|
||||
|
||||
Ok(format!("Welcome {}, userid {}!", name, userid))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </path-three>
|
||||
|
@ -1,30 +1,29 @@
|
||||
// <path-two>
|
||||
use actix_web::{web, Result};
|
||||
use actix_web::{get, web, Result};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Info {
|
||||
userid: u32,
|
||||
user_id: u32,
|
||||
friend: String,
|
||||
}
|
||||
|
||||
/// extract path info using serde
|
||||
#[get("/users/{user_id}/{friend}")] // <- define path parameters
|
||||
async fn index(info: web::Path<Info>) -> Result<String> {
|
||||
Ok(format!("Welcome {}, userid {}!", info.friend, info.userid))
|
||||
Ok(format!(
|
||||
"Welcome {}, user_id {}!",
|
||||
info.friend, info.user_id
|
||||
))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </path-two>
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <query>
|
||||
use actix_web::web;
|
||||
use actix_web::{get, web, App, HttpServer};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -8,17 +8,16 @@ struct Info {
|
||||
}
|
||||
|
||||
// this handler get called only if the request's query contains `username` field
|
||||
#[get("/")]
|
||||
async fn index(info: web::Query<Info>) -> String {
|
||||
format!("Welcome {}!", info.username)
|
||||
}
|
||||
// </query>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,6 +4,5 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
serde = "1.0"
|
||||
|
@ -15,14 +15,14 @@ async fn current_temperature() -> impl Responder {
|
||||
web::Json(Measurement { temperature: 42.3 })
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(web::resource("/").to(hello_world))
|
||||
.service(web::resource("/temp").to(current_temperature))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -5,5 +5,4 @@ edition = "2018"
|
||||
workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0.0"
|
||||
actix-web = "3"
|
||||
|
@ -1,33 +1,31 @@
|
||||
// <setup>
|
||||
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
|
||||
// <handlers>
|
||||
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
|
||||
|
||||
async fn index() -> impl Responder {
|
||||
#[get("/")]
|
||||
async fn hello() -> impl Responder {
|
||||
HttpResponse::Ok().body("Hello world!")
|
||||
}
|
||||
|
||||
async fn index2() -> impl Responder {
|
||||
HttpResponse::Ok().body("Hello world again!")
|
||||
#[post("/echo")]
|
||||
async fn echo(req_body: String) -> impl Responder {
|
||||
HttpResponse::Ok().body(req_body)
|
||||
}
|
||||
// </setup>
|
||||
|
||||
// <macro-attributes>
|
||||
use actix_web::get;
|
||||
|
||||
#[get("/hello")]
|
||||
async fn index3() -> impl Responder {
|
||||
async fn manual_hello() -> impl Responder {
|
||||
HttpResponse::Ok().body("Hey there!")
|
||||
}
|
||||
// </macro-attributes>
|
||||
// </handlers>
|
||||
|
||||
// <main>
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/", web::get().to(index))
|
||||
.route("/again", web::get().to(index2))
|
||||
.service(hello)
|
||||
.service(echo)
|
||||
.route("/hey", web::get().to(manual_hello))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,6 +4,5 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = { version = "2.0", features = ["openssl"] }
|
||||
actix-rt = "1.0"
|
||||
actix-web = { version = "3", features = ["openssl"] }
|
||||
openssl = { version = "0.10", features = ["v110"] }
|
||||
|
@ -6,7 +6,7 @@ async fn index(_req: HttpRequest) -> impl Responder {
|
||||
"Hello."
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
// load ssl keys
|
||||
// to create a self-signed temporary cert for testing:
|
||||
@ -18,7 +18,7 @@ async fn main() -> std::io::Result<()> {
|
||||
builder.set_certificate_chain_file("cert.pem").unwrap();
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind_openssl("127.0.0.1:8088", builder)?
|
||||
.bind_openssl("127.0.0.1:8080", builder)?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,5 +4,4 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
|
@ -6,14 +6,14 @@ async fn greet(req: HttpRequest) -> impl Responder {
|
||||
format!("Hello {}!", &name)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/", web::get().to(greet))
|
||||
.route("/{name}", web::get().to(greet))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,9 +4,8 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-service = "1.0"
|
||||
actix-session = "0.3"
|
||||
futures = "0.3.1"
|
||||
env_logger = "0.6"
|
||||
actix-web = "3"
|
||||
actix-service = "1"
|
||||
actix-session = "0.4"
|
||||
futures = "0.3"
|
||||
env_logger = "0.7"
|
||||
|
@ -1,7 +1,7 @@
|
||||
// <default-headers>
|
||||
use actix_web::{http, middleware, HttpResponse};
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
@ -17,7 +17,7 @@ async fn main() -> std::io::Result<()> {
|
||||
),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
// <error-handler>
|
||||
use actix_web::middleware::errhandlers::{ErrorHandlerResponse, ErrorHandlers};
|
||||
use actix_web::{dev, http, HttpResponse, Result};
|
||||
@ -10,7 +12,7 @@ fn render_500<B>(mut res: dev::ServiceResponse<B>) -> Result<ErrorHandlerRespons
|
||||
Ok(ErrorHandlerResponse::Response(res))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
@ -26,7 +28,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.route(web::head().to(|| HttpResponse::MethodNotAllowed())),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
use actix_web::middleware::Logger;
|
||||
use env_logger::Env;
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
@ -13,7 +13,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.wrap(Logger::default())
|
||||
.wrap(Logger::new("%a %{User-Agent}i"))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ where
|
||||
}
|
||||
// </simple>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
@ -86,7 +86,7 @@ async fn main() -> std::io::Result<()> {
|
||||
}),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
// <user-session>
|
||||
use actix_session::{CookieSession, Session};
|
||||
use actix_web::{web, App, Error, HttpResponse, HttpServer};
|
||||
@ -16,7 +18,7 @@ async fn index(session: Session) -> Result<HttpResponse, Error> {
|
||||
)))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
@ -26,7 +28,7 @@ async fn main() -> std::io::Result<()> {
|
||||
)
|
||||
.service(web::resource("/").to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
#![allow(dead_code, unused_variables)]
|
||||
|
||||
// <wrap-fn>
|
||||
use actix_service::Service;
|
||||
use actix_web::{web, App};
|
||||
use futures::future::FutureExt;
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() {
|
||||
let app = App::new()
|
||||
.wrap_fn(|req, srv| {
|
||||
|
@ -1,7 +0,0 @@
|
||||
[package]
|
||||
name = "og_databases"
|
||||
version = "0.7.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "0.7"
|
@ -1,98 +0,0 @@
|
||||
// <actor>
|
||||
use actix::prelude::*;
|
||||
|
||||
struct DbExecutor(SqliteConnection);
|
||||
|
||||
impl Actor for DbExecutor {
|
||||
type Context = SyncContext<Self>;
|
||||
}
|
||||
// </actor>
|
||||
|
||||
// <message>
|
||||
struct CreateUser {
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl Message for CreateUser {
|
||||
type Result = Result<User, Error>;
|
||||
}
|
||||
// </message>
|
||||
|
||||
// <handler>
|
||||
impl Handler<CreateUser> for DbExecutor {
|
||||
type Result = Result<User, Error>;
|
||||
|
||||
fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) -> Self::Result {
|
||||
use self::schema::users::dsl::*;
|
||||
|
||||
// Create insertion model
|
||||
let uuid = format!("{}", uuid::Uuid::new_v4());
|
||||
let new_user = models::NewUser {
|
||||
id: &uuid,
|
||||
name: &msg.name,
|
||||
};
|
||||
|
||||
// normal diesel operations
|
||||
diesel::insert_into(users)
|
||||
.values(&new_user)
|
||||
.execute(&self.0)
|
||||
.expect("Error inserting person");
|
||||
|
||||
let mut items = users
|
||||
.filter(id.eq(&uuid))
|
||||
.load::<models::User>(&self.0)
|
||||
.expect("Error loading person");
|
||||
|
||||
Ok(items.pop().unwrap())
|
||||
}
|
||||
}
|
||||
// </handler>
|
||||
|
||||
// <main>
|
||||
/// This is state where we will store *DbExecutor* address.
|
||||
struct State {
|
||||
db: Addr<DbExecutor>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let sys = actix::System::new("diesel-example");
|
||||
|
||||
// Start 3 parallel db executors
|
||||
let addr = SyncArbiter::start(3, || {
|
||||
DbExecutor(SqliteConnection::establish("test.db").unwrap())
|
||||
});
|
||||
|
||||
// Start http server
|
||||
HttpServer::new(move || {
|
||||
App::new::data(State { db: addr.clone() })
|
||||
.resource("/{name}", |r| r.method(Method::GET).a(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")
|
||||
.unwrap()
|
||||
.start()
|
||||
.unwrap();
|
||||
|
||||
println!("Started http server: 127.0.0.1:8080");
|
||||
let _ = sys.run();
|
||||
}
|
||||
// </main>
|
||||
|
||||
// <index>
|
||||
/// Async handler
|
||||
fn index(req: &HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Error>> {
|
||||
let name = &req.match_info()["name"];
|
||||
|
||||
// Send message to `DbExecutor` actor
|
||||
req.state()
|
||||
.db
|
||||
.send(CreateUser {
|
||||
name: name.to_owned(),
|
||||
})
|
||||
.from_err()
|
||||
.and_then(|res| match res {
|
||||
Ok(user) => Ok(HttpResponse::Ok().json(user)),
|
||||
Err(_) => Ok(HttpResponse::InternalServerError().into()),
|
||||
})
|
||||
.responder()
|
||||
}
|
||||
// </index>
|
@ -4,6 +4,5 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
serde = "1.0"
|
||||
|
@ -31,14 +31,14 @@ async fn index() -> HttpResponse {
|
||||
.body(include_str!("../static/form.html"))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/", web::get().to(index))
|
||||
.route("/event", web::post().to(capture_event))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -18,7 +18,7 @@
|
||||
}
|
||||
|
||||
function submitJson() {
|
||||
fetch('http://localhost:8088/event', {
|
||||
fetch('http://localhost:8080/event', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
|
@ -4,5 +4,4 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <arc>
|
||||
use actix_web::{web, Responder};
|
||||
use actix_web::{get, web, App, HttpServer, Responder};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
@ -8,20 +8,20 @@ struct AppState {
|
||||
count: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn show_count(data: web::Data<AppState>) -> impl Responder {
|
||||
format!("count: {}", data.count.load(Ordering::Relaxed))
|
||||
}
|
||||
|
||||
#[get("/add")]
|
||||
async fn add_one(data: web::Data<AppState>) -> impl Responder {
|
||||
data.count.fetch_add(1, Ordering::Relaxed);
|
||||
|
||||
format!("count: {}", data.count.load(Ordering::Relaxed))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
let data = AppState {
|
||||
count: Arc::new(AtomicUsize::new(0)),
|
||||
};
|
||||
@ -29,10 +29,10 @@ async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.data(data.clone())
|
||||
.route("/", web::to(show_count))
|
||||
.route("/add", web::to(add_one))
|
||||
.service(show_count)
|
||||
.service(add_one)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ async fn add_one(data: web::Data<AppState>) -> impl Responder {
|
||||
format!("count: {}", data.count.get())
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
@ -33,7 +33,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/", web::to(show_count))
|
||||
.route("/add", web::to(add_one))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,5 +4,4 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
|
@ -9,14 +9,14 @@ async fn hello(path: web::Path<String>) -> impl Responder {
|
||||
format!("Hello {}!", &path)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(web::resource("/").to(index))
|
||||
.service(web::resource("/{name}").to(hello))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -6,8 +6,6 @@ edition = "2018"
|
||||
[dependencies]
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
futures = "0.3.1"
|
||||
bytes = "0.4"
|
||||
actix-multipart = "0.2"
|
||||
actix-multipart = "0.3"
|
||||
|
@ -1,21 +0,0 @@
|
||||
// // <json-two>
|
||||
// use actix_web::{error::Error, HttpRequest, HttpResponse};
|
||||
// use futures::Future;
|
||||
// use serde::{Deserialize, Serialize};
|
||||
|
||||
// #[derive(Debug, Serialize, Deserialize)]
|
||||
// struct MyObj {
|
||||
// name: String,
|
||||
// number: i32,
|
||||
// }
|
||||
|
||||
// pub fn index(req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
|
||||
// req.json()
|
||||
// .from_err()
|
||||
// .and_then(|val: MyObj| {
|
||||
// println!("model: {:?}", val);
|
||||
// Ok(HttpResponse::Ok().json(val)) // <- send response
|
||||
// })
|
||||
// .responder()
|
||||
// }
|
||||
// // </json-two>
|
@ -1,4 +1,3 @@
|
||||
pub mod json_two;
|
||||
pub mod manual;
|
||||
pub mod multipart;
|
||||
pub mod streaming;
|
||||
@ -18,10 +17,10 @@ async fn index(info: web::Json<Info>) -> Result<String> {
|
||||
Ok(format!("Welcome {}!", info.username))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
// <json-manual>
|
||||
use actix_web::{error, web, App, Error, HttpResponse};
|
||||
use bytes::BytesMut;
|
||||
use actix_web::{error, post, web, App, Error, HttpResponse};
|
||||
use futures::StreamExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
@ -13,9 +12,10 @@ struct MyObj {
|
||||
|
||||
const MAX_SIZE: usize = 262_144; // max payload size is 256k
|
||||
|
||||
#[post("/")]
|
||||
async fn index_manual(mut payload: web::Payload) -> Result<HttpResponse, Error> {
|
||||
// payload is a stream of Bytes objects
|
||||
let mut body = BytesMut::new();
|
||||
let mut body = web::BytesMut::new();
|
||||
while let Some(chunk) = payload.next().await {
|
||||
let chunk = chunk?;
|
||||
// limit max size of in-memory payload
|
||||
@ -31,12 +31,12 @@ async fn index_manual(mut payload: web::Payload) -> Result<HttpResponse, Error>
|
||||
}
|
||||
// </json-manual>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index_manual)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
HttpServer::new(|| App::new().service(index_manual))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
// <streaming>
|
||||
use actix_web::{web, Error, HttpResponse};
|
||||
use actix_web::{get, web, Error, HttpResponse};
|
||||
use futures::StreamExt;
|
||||
|
||||
#[get("/")]
|
||||
async fn index(mut body: web::Payload) -> Result<HttpResponse, Error> {
|
||||
let mut bytes = web::BytesMut::new();
|
||||
while let Some(item) = body.next().await {
|
||||
@ -14,12 +15,12 @@ async fn index(mut body: web::Payload) -> Result<HttpResponse, Error> {
|
||||
}
|
||||
// </streaming>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <urlencoded>
|
||||
use actix_web::{web, HttpResponse};
|
||||
use actix_web::{post, web, HttpResponse};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -7,17 +7,18 @@ struct FormData {
|
||||
username: String,
|
||||
}
|
||||
|
||||
#[post("/")]
|
||||
async fn index(form: web::Form<FormData>) -> HttpResponse {
|
||||
HttpResponse::Ok().body(format!("username: {}", form.username))
|
||||
}
|
||||
// </urlencoded>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,8 +4,7 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
futures = "0.3.1"
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
|
@ -28,12 +28,12 @@ async fn index() -> impl Responder {
|
||||
}
|
||||
// </responder-trait>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,8 +4,7 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
serde = "1.0"
|
||||
futures = "0.3.1"
|
||||
bytes = "0.5"
|
||||
|
@ -1,20 +1,19 @@
|
||||
// <auto>
|
||||
use actix_web::{http::ContentEncoding, middleware, HttpResponse};
|
||||
use actix_web::{get, http::ContentEncoding, middleware, App, HttpResponse, HttpServer};
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok().body("data")
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Compress::new(ContentEncoding::Br))
|
||||
.route("/", web::get().to(index))
|
||||
.service(index)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,22 +1,23 @@
|
||||
// <brotli>
|
||||
use actix_web::{http::ContentEncoding, dev::BodyEncoding, HttpResponse};
|
||||
use actix_web::{
|
||||
dev::BodyEncoding, get, http::ContentEncoding, middleware, App, HttpResponse, HttpServer,
|
||||
};
|
||||
|
||||
#[get("/")]
|
||||
async fn index_br() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.encoding(ContentEncoding::Br)
|
||||
.body("data")
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{middleware, web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Compress::default())
|
||||
.route("/", web::get().to(index_br))
|
||||
.service(index_br)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ async fn index_br() -> HttpResponse {
|
||||
HttpResponse::Ok().body("data")
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{middleware, web, App, HttpServer};
|
||||
|
||||
@ -14,7 +14,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.wrap(middleware::Compress::new(ContentEncoding::Br))
|
||||
.route("/", web::get().to(index_br))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,20 +1,19 @@
|
||||
// <chunked>
|
||||
use actix_web::{HttpRequest, HttpResponse, Error};
|
||||
use actix_web::{get, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use bytes::Bytes;
|
||||
use futures::future::ok;
|
||||
use futures::stream::once;
|
||||
|
||||
async fn index(req: HttpRequest) -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.streaming(once(ok::<_, Error>(Bytes::from_static(b"data"))))
|
||||
#[get("/")]
|
||||
async fn index(_req: HttpRequest) -> HttpResponse {
|
||||
HttpResponse::Ok().streaming(once(ok::<_, Error>(Bytes::from_static(b"data"))))
|
||||
}
|
||||
// </chunked>
|
||||
|
||||
pub fn main() {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
#[actix_web::main]
|
||||
async fn main() {
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")
|
||||
.unwrap()
|
||||
.run();
|
||||
}
|
||||
|
@ -1,20 +1,19 @@
|
||||
// <compress>
|
||||
use actix_web::{middleware, HttpResponse};
|
||||
use actix_web::{get, middleware, App, HttpResponse, HttpServer};
|
||||
|
||||
#[get("/")]
|
||||
async fn index_br() -> HttpResponse {
|
||||
HttpResponse::Ok().body("data")
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Compress::default())
|
||||
.route("/", web::get().to(index_br))
|
||||
.service(index_br)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
// <identity>
|
||||
use actix_web::{
|
||||
http::ContentEncoding, middleware, dev::BodyEncoding, HttpResponse,
|
||||
dev::BodyEncoding, get, http::ContentEncoding, middleware, App, HttpResponse, HttpServer,
|
||||
};
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
// v- disable compression
|
||||
@ -10,16 +11,14 @@ async fn index() -> HttpResponse {
|
||||
.body("data")
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Compress::default())
|
||||
.route("/", web::get().to(index))
|
||||
.service(index)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
// <identity-two>
|
||||
use actix_web::{
|
||||
http::ContentEncoding, middleware, dev::BodyEncoding, HttpResponse,
|
||||
dev::BodyEncoding, get, http::ContentEncoding, middleware, App, HttpResponse, HttpServer,
|
||||
};
|
||||
|
||||
static HELLO_WORLD: &[u8] = &[
|
||||
0x1f, 0x8b, 0x08, 0x00, 0xa2, 0x30, 0x10, 0x5c, 0x00, 0x03, 0xcb, 0x48, 0xcd, 0xc9,
|
||||
0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf,
|
||||
0x0c, 0x00, 0x00, 0x00,
|
||||
0x1f, 0x8b, 0x08, 0x00, 0xa2, 0x30, 0x10, 0x5c, 0x00, 0x03, 0xcb, 0x48, 0xcd, 0xc9, 0xc9, 0x57,
|
||||
0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00, 0x00, 0x00,
|
||||
];
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.encoding(ContentEncoding::Identity)
|
||||
@ -17,16 +17,14 @@ async fn index() -> HttpResponse {
|
||||
}
|
||||
// </identity-two>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Compress::default())
|
||||
.route("/", web::get().to(index))
|
||||
.service(index)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <json-resp>
|
||||
use actix_web::{web, HttpResponse, Result};
|
||||
use actix_web::{get, web, HttpResponse, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@ -7,18 +7,19 @@ struct MyObj {
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[get("/a/{name}")]
|
||||
async fn index(obj: web::Path<MyObj>) -> Result<HttpResponse> {
|
||||
Ok(HttpResponse::Ok().json(MyObj {
|
||||
name: obj.name.to_string(),
|
||||
}))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route(r"/a/{name}", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -17,12 +17,12 @@ async fn index() -> HttpResponse {
|
||||
}
|
||||
// </builder>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -5,8 +5,6 @@ workspace = "../"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-rt = "1.0"
|
||||
actix-web = { version = "2.0", features = ["openssl"] }
|
||||
futures = "0.3.1"
|
||||
actix-web = { version = "3", features = ["openssl"] }
|
||||
futures = "0.3"
|
||||
openssl = "0.10"
|
||||
actix-http = "1.0"
|
||||
|
@ -1,7 +1,7 @@
|
||||
// <keep-alive>
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let one = HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
@ -18,6 +18,6 @@ async fn main() -> std::io::Result<()> {
|
||||
})
|
||||
.keep_alive(None); // <- Disable keep-alive
|
||||
|
||||
one.bind("127.0.0.1:8088")?.run().await
|
||||
one.bind("127.0.0.1:8080")?.run().await
|
||||
}
|
||||
// </keep-alive>
|
||||
|
@ -8,6 +8,7 @@ async fn index(req: HttpRequest) -> HttpResponse {
|
||||
.finish()
|
||||
}
|
||||
// </example>
|
||||
|
||||
// ConnectionType::Close
|
||||
// ConnectionType::KeepAlive
|
||||
// ConnectionType::Upgrade
|
||||
|
@ -7,12 +7,12 @@ pub mod workers;
|
||||
// <main>
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
// <signals>
|
||||
use actix_rt::System;
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
use actix_web::{web, App, HttpResponse, HttpServer, rt::System};
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
@ -14,7 +13,7 @@ async fn main() {
|
||||
let srv = HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
|
||||
.run();
|
||||
|
||||
|
@ -1,28 +1,28 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
// <ssl>
|
||||
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
|
||||
use actix_web::{get, App, HttpRequest, HttpServer, Responder};
|
||||
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
||||
|
||||
#[get("/")]
|
||||
async fn index(_req: HttpRequest) -> impl Responder {
|
||||
"Welcome!"
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
// load ssl keys
|
||||
// to create a self-signed temporary cert for testing:
|
||||
// `openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'`
|
||||
let mut builder =
|
||||
SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||
builder
|
||||
.set_private_key_file("key.pem", SslFiletype::PEM)
|
||||
.unwrap();
|
||||
builder.set_certificate_chain_file("cert.pem").unwrap();
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind_openssl("127.0.0.1:8088", builder)?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind_openssl("127.0.0.1:8080", builder)?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </ssl>
|
||||
//
|
||||
// sssl rust-tls
|
||||
|
@ -1,7 +1,7 @@
|
||||
// <workers>
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() {
|
||||
HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
|
@ -4,7 +4,6 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-files = "0.2"
|
||||
mime = "*"
|
||||
actix-web = "3"
|
||||
actix-files = "0.3"
|
||||
mime = "0.3"
|
||||
|
@ -1,8 +1,9 @@
|
||||
// <config-one>
|
||||
use actix_files as fs;
|
||||
use actix_web::http::header::{ContentDisposition, DispositionType};
|
||||
use actix_web::{web, App, Error, HttpRequest, HttpServer};
|
||||
use actix_web::{get, App, Error, HttpRequest, HttpServer};
|
||||
|
||||
#[get("/{filename:.*}")]
|
||||
async fn index(req: HttpRequest) -> Result<fs::NamedFile, Error> {
|
||||
let path: std::path::PathBuf = req.match_info().query("filename").parse().unwrap();
|
||||
let file = fs::NamedFile::open(path)?;
|
||||
@ -14,10 +15,10 @@ async fn index(req: HttpRequest) -> Result<fs::NamedFile, Error> {
|
||||
}))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().route("/{filename:.*}", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
use actix_files as fs;
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new().service(
|
||||
@ -11,7 +11,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.use_last_modified(true),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -2,12 +2,12 @@
|
||||
use actix_files as fs;
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new().service(fs::Files::new("/static", ".").show_files_listing())
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -12,12 +12,12 @@ async fn index(req: HttpRequest) -> Result<NamedFile> {
|
||||
Ok(NamedFile::open(path)?)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/{filename:.*}", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,10 +4,12 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
futures = "0.3"
|
||||
futures-util = "0.3"
|
||||
bytes = "0.5"
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "1"
|
||||
|
@ -35,7 +35,6 @@ pub fn main() {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use actix_rt;
|
||||
|
||||
use futures_util::stream::StreamExt;
|
||||
use futures_util::stream::TryStreamExt;
|
||||
|
@ -5,9 +5,8 @@ edition = "2018"
|
||||
workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
actix = "0.9"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "2.0"
|
||||
actix = "0.10"
|
||||
actix-web = "3"
|
||||
futures = "0.3.1"
|
||||
openssl = "0.10"
|
||||
serde = "1.0"
|
||||
|
@ -1,7 +1,7 @@
|
||||
use actix_web::{guard, web, App, HttpResponse};
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::HttpServer;
|
||||
|
||||
@ -17,7 +17,7 @@ App::new().service(
|
||||
)
|
||||
// </cfg>
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
use actix_web::{guard, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn index(_req: HttpRequest) -> impl Responder {
|
||||
"Welcome!"
|
||||
}
|
||||
|
||||
// <default>
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
@ -16,7 +17,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ impl Guard for ContentTypeHeader {
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
@ -21,7 +21,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.to(|| HttpResponse::Ok()),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
// <guard2>
|
||||
use actix_web::{guard, web, App, HttpResponse, HttpServer};
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
@ -11,7 +11,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -20,14 +20,14 @@ async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok().body("Hello")
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/", web::get().to(index))
|
||||
.route("/user", web::post().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user