From 53fc2221efc0c4c2b4fd3d480e01d8ff1cac5174 Mon Sep 17 00:00:00 2001 From: Juan Aguilar Date: Tue, 26 Mar 2019 04:29:00 +0100 Subject: [PATCH] Update to master (#90) --- .travis.yml | 2 -- actix_todo/Cargo.toml | 6 +++--- actix_todo/src/api.rs | 2 +- actix_todo/src/main.rs | 12 ++++++------ async_db/Cargo.toml | 2 +- async_db/src/main.rs | 4 ++-- async_ex1/Cargo.toml | 2 +- async_ex1/src/main.rs | 38 +++++++++++++++++++++++--------------- basics/Cargo.toml | 6 +++--- basics/src/main.rs | 4 ++-- cookie-auth/Cargo.toml | 2 +- cookie-auth/src/main.rs | 4 ++-- cookie-session/Cargo.toml | 4 ++-- cookie-session/src/main.rs | 4 ++-- diesel/Cargo.toml | 2 +- diesel/src/main.rs | 28 ++++++++++++++++++---------- error_handling/Cargo.toml | 2 +- form/Cargo.toml | 2 +- form/src/main.rs | 4 ++-- hello-world/Cargo.toml | 2 +- hello-world/src/main.rs | 2 +- json/Cargo.toml | 2 +- json/src/main.rs | 24 ++++++++---------------- juniper/Cargo.toml | 2 +- juniper/src/main.rs | 4 ++-- middleware/Cargo.toml | 2 +- middleware/src/main.rs | 4 ++-- r2d2/Cargo.toml | 2 +- r2d2/src/main.rs | 4 ++-- state/Cargo.toml | 2 +- state/src/main.rs | 2 +- static_index/Cargo.toml | 4 ++-- static_index/src/main.rs | 2 +- template_askama/Cargo.toml | 2 +- template_tera/Cargo.toml | 2 +- template_tera/src/main.rs | 4 ++-- template_yarte/Cargo.toml | 2 +- template_yarte/src/lib.rs | 20 -------------------- template_yarte/src/main.rs | 34 +++++++++++++++++++++++++++++----- template_yarte/yarte.toml | 7 ------- tls/Cargo.toml | 2 +- tls/src/main.rs | 2 +- websocket/Cargo.toml | 6 +++--- websocket/src/main.rs | 13 +++---------- 44 files changed, 139 insertions(+), 143 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4c82f95..8050c10 100644 --- a/.travis.yml +++ b/.travis.yml @@ -40,7 +40,6 @@ script: cd actix_todo && cargo check && cd .. cd basics && cargo check && cd .. cd cookie-auth && cargo check && cd .. - cd cookie-auth-full && cargo check && cd .. cd cookie-session && cargo check && cd .. cd diesel && cargo check && cd .. cd error_handling && cargo check && cd .. @@ -55,7 +54,6 @@ script: cd protobuf && cargo check && cd .. cd r2d2 && cargo check && cd .. cd redis-session && cargo check && cd .. - cd simple-auth-sarver && cargo check && cd .. cd state && cargo check && cd .. cd static_index && cargo check && cd .. cd template_askama && cargo check && cd .. diff --git a/actix_todo/Cargo.toml b/actix_todo/Cargo.toml index 09b9a01..736178d 100644 --- a/actix_todo/Cargo.toml +++ b/actix_todo/Cargo.toml @@ -6,9 +6,9 @@ workspace = ".." edition = "2018" [dependencies] -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } -actix-files = { git="https://github.com/actix/actix-web.git", branch = "1.0" } -actix-session = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } +actix-files = { git="https://github.com/actix/actix-web.git" } +actix-session = { git="https://github.com/actix/actix-web.git" } dotenv = "0.13.0" env_logger = "0.5.10" futures = "0.1.22" diff --git a/actix_todo/src/api.rs b/actix_todo/src/api.rs index 833e2b2..cb3e15a 100644 --- a/actix_todo/src/api.rs +++ b/actix_todo/src/api.rs @@ -1,6 +1,6 @@ use actix_files::NamedFile; use actix_session::Session; -use actix_web::middleware::ErrorHandlerResponse; +use actix_web::middleware::errhandlers::ErrorHandlerResponse; use actix_web::{dev, error, http, web, Error, HttpResponse, Responder, Result}; use futures::future::{err, Either, Future, IntoFuture}; use tera::{Context, Tera}; diff --git a/actix_todo/src/main.rs b/actix_todo/src/main.rs index 9c9664a..aca105e 100644 --- a/actix_todo/src/main.rs +++ b/actix_todo/src/main.rs @@ -11,7 +11,7 @@ use std::{env, io}; use actix_files as fs; use actix_session::CookieSession; -use actix_web::middleware::{ErrorHandlers, Logger}; +use actix_web::middleware::{errhandlers::ErrorHandlers, Logger}; use actix_web::{http, web, App, HttpServer}; use dotenv::dotenv; use tera::Tera; @@ -49,11 +49,11 @@ fn main() -> io::Result<()> { .handler(http::StatusCode::NOT_FOUND, api::not_found); App::new() - .state(templates) - .state(pool.clone()) - .middleware(Logger::default()) - .middleware(session_store) - .middleware(error_handlers) + .data(templates) + .data(pool.clone()) + .wrap(Logger::default()) + .wrap(session_store) + .wrap(error_handlers) .service(web::resource("/").route(web::get().to_async(api::index))) .service(web::resource("/todo").route(web::post().to_async(api::create))) .service( diff --git a/async_db/Cargo.toml b/async_db/Cargo.toml index 9dee0d2..3161c19 100644 --- a/async_db/Cargo.toml +++ b/async_db/Cargo.toml @@ -7,7 +7,7 @@ workspace = ".." [dependencies] actix-rt = "0.2" -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } dotenv = "0.10" env_logger = "0.5" diff --git a/async_db/src/main.rs b/async_db/src/main.rs index e5648b2..b2293fe 100644 --- a/async_db/src/main.rs +++ b/async_db/src/main.rs @@ -78,8 +78,8 @@ fn main() -> io::Result<()> { // Start http server HttpServer::new(move || { App::new() - .state(pool.clone()) - .middleware(middleware::Logger::default()) + .data(pool.clone()) + .wrap(middleware::Logger::default()) .service( web::resource("/asyncio_weather") .route(web::get().to_async(asyncio_weather)), diff --git a/async_ex1/Cargo.toml b/async_ex1/Cargo.toml index 69985d7..01a7976 100644 --- a/async_ex1/Cargo.toml +++ b/async_ex1/Cargo.toml @@ -8,7 +8,7 @@ workspace = ".." [dependencies] actix-rt = "0.2" actix-http = { git="https://github.com/actix/actix-http.git", features=["ssl"] } -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0", features=["ssl"] } +actix-web = { git="https://github.com/actix/actix-web.git", features=["ssl"] } futures = "0.1" serde = "1.0.43" diff --git a/async_ex1/src/main.rs b/async_ex1/src/main.rs index 4e8c94d..8781ce9 100644 --- a/async_ex1/src/main.rs +++ b/async_ex1/src/main.rs @@ -24,8 +24,9 @@ use std::collections::HashMap; use std::io; use actix_http::client; -use actix_web::{web, App, Error, HttpMessage, HttpResponse, HttpServer}; -use futures::future::{ok, Future}; +use actix_web::web::BytesMut; +use actix_web::{web, App, Error, HttpResponse, HttpServer}; +use futures::{Future, Stream}; use validator::Validate; #[derive(Debug, Validate, Deserialize, Serialize)] @@ -54,7 +55,7 @@ struct HttpBinResponse { /// post json to httpbin, get it back in the response body, return deserialized fn step_x_v1(data: SomeData) -> Box> { - let mut connector = client::Connector::default().service(); + let mut connector = client::Connector::new().service(); Box::new( client::ClientRequest::post("https://httpbin.org/post") @@ -62,13 +63,17 @@ fn step_x_v1(data: SomeData) -> Box> { .unwrap() .send(&mut connector) .map_err(Error::from) // <- convert SendRequestError to an Error - .and_then(|mut resp| { - resp.body() // <- this is MessageBody type, resolves to complete body + .and_then(|resp| { + resp // <- this is MessageBody type, resolves to complete body .from_err() // <- convert PayloadError to an Error - .and_then(|body| { - let resp: HttpBinResponse = + .fold(BytesMut::new(), |mut acc, chunk| { + acc.extend_from_slice(&chunk); + Ok::<_, Error>(acc) + }) + .map(|body| { + let body: HttpBinResponse = serde_json::from_slice(&body).unwrap(); - ok(resp.json) + body.json }) }), ) @@ -95,19 +100,22 @@ fn create_something_v1( /// post json to httpbin, get it back in the response body, return deserialized fn step_x_v2(data: SomeData) -> impl Future { - let mut connector = client::Connector::default().service(); + let mut connector = client::Connector::new().service(); client::ClientRequest::post("https://httpbin.org/post") .json(data) .unwrap() .send(&mut connector) .map_err(Error::from) // <- convert SendRequestError to an Error - .and_then(|mut resp| { - resp.body() // <- this is MessageBody type, resolves to complete body - .from_err() // <- convert PayloadError to an Error - .and_then(|body| { - let resp: HttpBinResponse = serde_json::from_slice(&body).unwrap(); - ok(resp.json) + .and_then(|resp| { + resp.from_err() + .fold(BytesMut::new(), |mut acc, chunk| { + acc.extend_from_slice(&chunk); + Ok::<_, Error>(acc) + }) + .map(|body| { + let body: HttpBinResponse = serde_json::from_slice(&body).unwrap(); + body.json }) }) } diff --git a/basics/Cargo.toml b/basics/Cargo.toml index 71a9883..0147e33 100644 --- a/basics/Cargo.toml +++ b/basics/Cargo.toml @@ -7,9 +7,9 @@ edition = "2018" [dependencies] actix-rt = "0.2" -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } -actix-files = { git="https://github.com/actix/actix-web.git", branch = "1.0" } -actix-session = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } +actix-files = { git="https://github.com/actix/actix-web.git" } +actix-session = { git="https://github.com/actix/actix-web.git" } futures = "0.1.25" env_logger = "0.5" diff --git a/basics/src/main.rs b/basics/src/main.rs index 58eae45..7ab8a72 100644 --- a/basics/src/main.rs +++ b/basics/src/main.rs @@ -83,9 +83,9 @@ fn main() -> io::Result<()> { HttpServer::new(|| { App::new() // enable logger - .middleware(middleware::Logger::default()) + .wrap(middleware::Logger::default()) // cookie session middleware - .middleware(CookieSession::signed(&[0; 32]).secure(false)) + .wrap(CookieSession::signed(&[0; 32]).secure(false)) // register favicon .service(favicon) // register simple route, handle all methods diff --git a/cookie-auth/Cargo.toml b/cookie-auth/Cargo.toml index 81fd8eb..efeb709 100644 --- a/cookie-auth/Cargo.toml +++ b/cookie-auth/Cargo.toml @@ -6,5 +6,5 @@ edition = "2018" workspace = ".." [dependencies] -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } env_logger = "0.6" diff --git a/cookie-auth/src/main.rs b/cookie-auth/src/main.rs index 01ddde2..9d1f857 100644 --- a/cookie-auth/src/main.rs +++ b/cookie-auth/src/main.rs @@ -22,8 +22,8 @@ fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() - .middleware(middleware::Logger::default()) - .middleware(IdentityService::new( + .wrap(middleware::Logger::default()) + .wrap(IdentityService::new( CookieIdentityPolicy::new(&[0; 32]) .name("auth-example") .secure(false), diff --git a/cookie-session/Cargo.toml b/cookie-session/Cargo.toml index 2947b95..d7bee4d 100644 --- a/cookie-session/Cargo.toml +++ b/cookie-session/Cargo.toml @@ -6,8 +6,8 @@ workspace = ".." edition = "2018" [dependencies] -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } -actix-session = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } +actix-session = { git="https://github.com/actix/actix-web.git" } futures = "0.1" time = "0.1" diff --git a/cookie-session/src/main.rs b/cookie-session/src/main.rs index 6e0df12..497f01d 100644 --- a/cookie-session/src/main.rs +++ b/cookie-session/src/main.rs @@ -32,9 +32,9 @@ fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() // enable logger - .middleware(Logger::default()) + .wrap(Logger::default()) // cookie session middleware - .middleware(CookieSession::signed(&[0; 32]).secure(false)) + .wrap(CookieSession::signed(&[0; 32]).secure(false)) .service(web::resource("/").to(index)) }) .bind("127.0.0.1:8080")? diff --git a/diesel/Cargo.toml b/diesel/Cargo.toml index af94c95..4089a87 100644 --- a/diesel/Cargo.toml +++ b/diesel/Cargo.toml @@ -6,7 +6,7 @@ workspace = ".." edition = "2018" [dependencies] -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } bytes = "0.4" env_logger = "0.6" diff --git a/diesel/src/main.rs b/diesel/src/main.rs index 044db37..fdf3cbc 100644 --- a/diesel/src/main.rs +++ b/diesel/src/main.rs @@ -10,7 +10,7 @@ extern crate diesel; extern crate serde_derive; use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer}; -use bytes::{Bytes, BytesMut}; +use bytes::BytesMut; use diesel::prelude::*; use diesel::r2d2::{self, ConnectionManager}; use futures::future::{err, Either}; @@ -61,13 +61,10 @@ struct MyUser { const MAX_SIZE: usize = 262_144; // max payload size is 256k /// This handler manually load request payload and parse json object -fn index_add

( - pl: web::Payload

, +fn index_add( + pl: web::Payload, pool: web::Data, -) -> impl Future -where - P: Stream, -{ +) -> impl Future { pl // `Future::from_err` acts like `?` in that it coerces the error type from // the future into the final error type @@ -132,9 +129,9 @@ fn main() -> std::io::Result<()> { // Start http server HttpServer::new(move || { App::new() - .state(pool.clone()) + .data(pool.clone()) // enable logger - .middleware(middleware::Logger::default()) + .wrap(middleware::Logger::default()) // This can be called with: // curl -S --header "Content-Type: application/json" --request POST --data '{"name":"xyz"}' http://127.0.0.1:8080/add // Use of the extractors makes some post conditions simpler such @@ -142,7 +139,18 @@ fn main() -> std::io::Result<()> { .service( web::resource("/add2").route( web::post() - .config(web::JsonConfig::default().limit(4096)) // <- limit size of the payload + .data( + web::JsonConfig::default() + .limit(4096) // <- limit size of the payload + .error_handler(|err, _| { + // <- create custom error response + error::InternalError::from_response( + err, + HttpResponse::Conflict().finish(), + ) + .into() + }), + ) .to_async(add2), ), ) diff --git a/error_handling/Cargo.toml b/error_handling/Cargo.toml index f1df6a4..c4cb097 100644 --- a/error_handling/Cargo.toml +++ b/error_handling/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" workspace = ".." [dependencies] -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } derive_more = "0.14.0" futures = "0.1.23" diff --git a/form/Cargo.toml b/form/Cargo.toml index 1fd56d4..a849fed 100644 --- a/form/Cargo.toml +++ b/form/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" workspace = ".." [dependencies] -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } serde = "1.0" serde_derive = "1.0" diff --git a/form/src/main.rs b/form/src/main.rs index 3219ccb..ebdddf0 100644 --- a/form/src/main.rs +++ b/form/src/main.rs @@ -12,10 +12,10 @@ struct AppState { fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() - .state(AppState { + .data(AppState { foo: "bar".to_string(), }) - .middleware(middleware::Logger::default()) + .wrap(middleware::Logger::default()) .service(web::resource("/").route(web::get().to(index))) .service(web::resource("/post1").route(web::post().to(handle_post_1))) .service(web::resource("/post2").route(web::post().to(handle_post_2))) diff --git a/hello-world/Cargo.toml b/hello-world/Cargo.toml index cb47b1e..d3d3842 100644 --- a/hello-world/Cargo.toml +++ b/hello-world/Cargo.toml @@ -7,4 +7,4 @@ edition = "2018" [dependencies] env_logger = "0.6" -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } diff --git a/hello-world/src/main.rs b/hello-world/src/main.rs index 3bb9064..ac29406 100644 --- a/hello-world/src/main.rs +++ b/hello-world/src/main.rs @@ -12,7 +12,7 @@ fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() // enable logger - .middleware(middleware::Logger::default()) + .wrap(middleware::Logger::default()) .service(web::resource("/index.html").to(|| "Hello world!")) .service(web::resource("/").to(index)) }) diff --git a/json/Cargo.toml b/json/Cargo.toml index ff724d2..c5d0982 100644 --- a/json/Cargo.toml +++ b/json/Cargo.toml @@ -6,7 +6,7 @@ workspace = ".." edition = "2018" [dependencies] -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } bytes = "0.4" futures = "0.1" diff --git a/json/src/main.rs b/json/src/main.rs index 6c9d7a5..9b0d9a5 100644 --- a/json/src/main.rs +++ b/json/src/main.rs @@ -4,7 +4,7 @@ extern crate json; use actix_web::{ error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer, }; -use bytes::{Bytes, BytesMut}; +use bytes::BytesMut; use futures::{Future, Stream}; use json::JsonValue; use serde_derive::{Deserialize, Serialize}; @@ -32,12 +32,9 @@ fn extract_item(item: web::Json, req: HttpRequest) -> HttpResponse { const MAX_SIZE: usize = 262_144; // max payload size is 256k /// This handler manually load request payload and parse json object -fn index_manual

( - payload: web::Payload

, -) -> impl Future -where - P: Stream, -{ +fn index_manual( + payload: web::Payload, +) -> impl Future { // payload is a stream of Bytes objects payload // `Future::from_err` acts like `?` in that it coerces the error type from @@ -64,12 +61,7 @@ where } /// This handler manually load request payload and parse json-rust -fn index_mjsonrust

( - pl: web::Payload

, -) -> impl Future -where - P: Stream, -{ +fn index_mjsonrust(pl: web::Payload) -> impl Future { pl.concat2().from_err().and_then(|body| { // body is loaded, now we can deserialize json-rust let result = json::parse(std::str::from_utf8(&body).unwrap()); // return Result @@ -90,18 +82,18 @@ fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() // enable logger - .middleware(middleware::Logger::default()) + .wrap(middleware::Logger::default()) .service( web::resource("/extractor").route( web::post() - .config(web::JsonConfig::default().limit(4096)) // <- limit size of the payload + .data(web::JsonConfig::default().limit(4096)) // <- limit size of the payload .to(index), ), ) .service( web::resource("/extractor2").route( web::post() - .config(web::JsonConfig::default().limit(4096)) // <- limit size of the payload + .data(web::JsonConfig::default().limit(4096)) // <- limit size of the payload .to_async(extract_item), ), ) diff --git a/juniper/Cargo.toml b/juniper/Cargo.toml index e1295d2..07ac636 100644 --- a/juniper/Cargo.toml +++ b/juniper/Cargo.toml @@ -6,7 +6,7 @@ workspace = ".." edition = "2018" [dependencies] -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } env_logger = "0.6" futures = "0.1" serde = "1.0" diff --git a/juniper/src/main.rs b/juniper/src/main.rs index 433688e..2615d34 100644 --- a/juniper/src/main.rs +++ b/juniper/src/main.rs @@ -49,8 +49,8 @@ fn main() -> io::Result<()> { // Start http server HttpServer::new(move || { App::new() - .state(schema.clone()) - .middleware(middleware::Logger::default()) + .data(schema.clone()) + .wrap(middleware::Logger::default()) .service(web::resource("/graphql").route(web::post().to_async(graphql))) .service(web::resource("/graphiql").route(web::get().to(graphiql))) }) diff --git a/middleware/Cargo.toml b/middleware/Cargo.toml index 2d533c8..7905b56 100644 --- a/middleware/Cargo.toml +++ b/middleware/Cargo.toml @@ -7,6 +7,6 @@ workspace = ".." [dependencies] actix-service = "0.3.3" -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } futures = "0.1.25" env_logger = "0.6" \ No newline at end of file diff --git a/middleware/src/main.rs b/middleware/src/main.rs index 6e1ec82..0496db2 100644 --- a/middleware/src/main.rs +++ b/middleware/src/main.rs @@ -11,8 +11,8 @@ fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() - .middleware(redirect::CheckLogin) - .middleware(simple::SayHi) + .wrap(redirect::CheckLogin) + .wrap(simple::SayHi) .service(web::resource("/login").to(|| { "You are on /login. Go to src/redirect.rs to change this behavior." })) diff --git a/r2d2/Cargo.toml b/r2d2/Cargo.toml index 7046f29..a0b5e03 100644 --- a/r2d2/Cargo.toml +++ b/r2d2/Cargo.toml @@ -7,7 +7,7 @@ workspace = "../" [dependencies] actix-rt = "0.2" -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } futures = "0.1" env_logger = "0.6" diff --git a/r2d2/src/main.rs b/r2d2/src/main.rs index 7b7f91b..c323784 100644 --- a/r2d2/src/main.rs +++ b/r2d2/src/main.rs @@ -45,8 +45,8 @@ fn main() -> io::Result<()> { // start http server HttpServer::new(move || { App::new() - .state(pool.clone()) // <- store db pool in app state - .middleware(middleware::Logger::default()) + .data(pool.clone()) // <- store db pool in app state + .wrap(middleware::Logger::default()) .route("/{name}", web::get().to_async(index)) }) .bind("127.0.0.1:8080")? diff --git a/state/Cargo.toml b/state/Cargo.toml index 7d8e0a0..39bf08a 100644 --- a/state/Cargo.toml +++ b/state/Cargo.toml @@ -6,7 +6,7 @@ workspace = ".." edition = "2018" [dependencies] -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } futures = "0.1" env_logger = "0.6" diff --git a/state/src/main.rs b/state/src/main.rs index 642d34e..3ebb38a 100644 --- a/state/src/main.rs +++ b/state/src/main.rs @@ -35,7 +35,7 @@ fn main() -> io::Result<()> { App::new() .data(counter.clone()) // <- create app with shared state // enable logger - .middleware(middleware::Logger::default()) + .wrap(middleware::Logger::default()) // register simple handler, handle all methods .service(web::resource("/").to(index)) }) diff --git a/static_index/Cargo.toml b/static_index/Cargo.toml index ee3d537..2dcdba3 100644 --- a/static_index/Cargo.toml +++ b/static_index/Cargo.toml @@ -9,5 +9,5 @@ edition = "2018" futures = "0.1" env_logger = "0.5" -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } -actix-files = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } +actix-files = { git="https://github.com/actix/actix-web.git" } diff --git a/static_index/src/main.rs b/static_index/src/main.rs index e4c4fa8..ced7b68 100644 --- a/static_index/src/main.rs +++ b/static_index/src/main.rs @@ -8,7 +8,7 @@ fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() // enable logger - .middleware(middleware::Logger::default()) + .wrap(middleware::Logger::default()) .service( // static files fs::Files::new("/", "./static/").index_file("index.html"), diff --git a/template_askama/Cargo.toml b/template_askama/Cargo.toml index d329742..242087a 100644 --- a/template_askama/Cargo.toml +++ b/template_askama/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" [dependencies] env_logger = "0.6" askama = "0.6" -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } [build-dependencies] askama = "0.6" diff --git a/template_tera/Cargo.toml b/template_tera/Cargo.toml index 2c01582..ff35ad1 100644 --- a/template_tera/Cargo.toml +++ b/template_tera/Cargo.toml @@ -8,4 +8,4 @@ edition = "2018" [dependencies] env_logger = "0.6" tera = "*" -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } diff --git a/template_tera/src/main.rs b/template_tera/src/main.rs index 37c9d93..a594214 100644 --- a/template_tera/src/main.rs +++ b/template_tera/src/main.rs @@ -33,8 +33,8 @@ fn main() -> std::io::Result<()> { compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")); App::new() - .state(tera) - .middleware(middleware::Logger::default()) // enable logger + .data(tera) + .wrap(middleware::Logger::default()) // enable logger .service(web::resource("/").route(web::get().to(index))) }) .bind("127.0.0.1:8080")? diff --git a/template_yarte/Cargo.toml b/template_yarte/Cargo.toml index 4a7f8cb..47a03b2 100644 --- a/template_yarte/Cargo.toml +++ b/template_yarte/Cargo.toml @@ -11,7 +11,7 @@ workspace = ".." env_logger = "0.6" yarte = "0.1" -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } [build-dependencies] yarte = "0.1" diff --git a/template_yarte/src/lib.rs b/template_yarte/src/lib.rs index 83f1f84..8b13789 100644 --- a/template_yarte/src/lib.rs +++ b/template_yarte/src/lib.rs @@ -1,21 +1 @@ -use actix_web::{error::ErrorInternalServerError, web::Query, HttpResponse, Result}; -use yarte::Template; -use std::collections::HashMap; - -#[derive(Template)] -#[template(path = "index.hbs")] -struct IndexTemplate { - query: Query>, -} - -pub fn index(query: Query>) -> Result { - IndexTemplate { query } - .call() - .map(|s| { - HttpResponse::Ok() - .content_type(IndexTemplate::mime()) - .body(s) - }) - .map_err(|_| ErrorInternalServerError("Template parsing error")) -} diff --git a/template_yarte/src/main.rs b/template_yarte/src/main.rs index 6a4a896..cd4434b 100644 --- a/template_yarte/src/main.rs +++ b/template_yarte/src/main.rs @@ -1,7 +1,31 @@ -use actix_web::{middleware, web, App, HttpServer}; +#[macro_use] +extern crate actix_web; -#[path = "lib.rs"] -mod template; +use std::collections::HashMap; + +use actix_web::{ + error::ErrorInternalServerError, middleware, web::Query, App, HttpResponse, + HttpServer, Result, +}; +use yarte::Template; + +#[derive(Template)] +#[template(path = "index.hbs")] +struct IndexTemplate { + query: Query>, +} + +#[get("/")] +pub fn index(query: Query>) -> Result { + IndexTemplate { query } + .call() + .map(|s| { + HttpResponse::Ok() + .content_type(IndexTemplate::mime()) + .body(s) + }) + .map_err(|_| ErrorInternalServerError("Template parsing error")) +} fn main() -> std::io::Result<()> { std::env::set_var("RUST_LOG", "actix_web=info"); @@ -11,8 +35,8 @@ fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() // enable logger - .middleware(middleware::Logger::default()) - .service(web::resource("/").to(template::index)) + .wrap(middleware::Logger::default()) + .service(index) }) .bind("127.0.0.1:8080")? .run() diff --git a/template_yarte/yarte.toml b/template_yarte/yarte.toml index 19a6367..bcf8920 100644 --- a/template_yarte/yarte.toml +++ b/template_yarte/yarte.toml @@ -1,14 +1,7 @@ # root dir of templates [main] dir = "templates" -debug = "code" # Alias for partials. In call, change the start of partial path with one of this, if exist. [partials] alias = "./deep/more/deep" - -[debug] -# prettyprint themes, put anything for options -theme = "zenburn" -number_line = true -grid = true diff --git a/tls/Cargo.toml b/tls/Cargo.toml index b18ff5a..0442f20 100644 --- a/tls/Cargo.toml +++ b/tls/Cargo.toml @@ -14,4 +14,4 @@ env_logger = "0.5" openssl = { version="0.10" } actix-rt = "0.2" -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0", features=["ssl"] } +actix-web = { git="https://github.com/actix/actix-web.git", features=["ssl"] } diff --git a/tls/src/main.rs b/tls/src/main.rs index f8c8961..7e95309 100644 --- a/tls/src/main.rs +++ b/tls/src/main.rs @@ -27,7 +27,7 @@ fn main() -> io::Result<()> { HttpServer::new(|| { App::new() // enable logger - .middleware(middleware::Logger::default()) + .wrap(middleware::Logger::default()) // register simple handler, handle all methods .service(web::resource("/index.html").to(index)) // with path parameters diff --git a/websocket/Cargo.toml b/websocket/Cargo.toml index a3f662c..a68b058 100644 --- a/websocket/Cargo.toml +++ b/websocket/Cargo.toml @@ -15,9 +15,9 @@ path = "src/main.rs" [dependencies] actix = { git="https://github.com/actix/actix.git" } -actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } -actix-web-actors = { git="https://github.com/actix/actix-web.git", branch = "1.0" } -actix-files = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web = { git="https://github.com/actix/actix-web.git" } +actix-web-actors = { git="https://github.com/actix/actix-web.git" } +actix-files = { git="https://github.com/actix/actix-web.git" } env_logger = "0.6" futures = "0.1" bytes = "0.4" \ No newline at end of file diff --git a/websocket/src/main.rs b/websocket/src/main.rs index 25aac6a..67d5791 100644 --- a/websocket/src/main.rs +++ b/websocket/src/main.rs @@ -7,12 +7,8 @@ use std::time::{Duration, Instant}; use actix::prelude::*; use actix_files as fs; -use actix_web::{ - error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer, -}; +use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer}; use actix_web_actors::ws; -use bytes::Bytes; -use futures::Stream; /// How often heartbeat pings are sent const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5); @@ -20,10 +16,7 @@ const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5); const CLIENT_TIMEOUT: Duration = Duration::from_secs(10); /// do websocket handshake and start `MyWebSocket` actor -fn ws_index(r: HttpRequest, stream: web::Payload) -> Result -where - S: Stream + 'static, -{ +fn ws_index(r: HttpRequest, stream: web::Payload) -> Result { println!("{:?}", r); let res = ws::start(MyWebSocket::new(), &r, stream); println!("{:?}", res.as_ref().unwrap()); @@ -103,7 +96,7 @@ fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() // enable logger - .middleware(middleware::Logger::default()) + .wrap(middleware::Logger::default()) // websocket route .service(web::resource("/ws/").route(web::get().to(ws_index))) // static files