diff --git a/Cargo.lock b/Cargo.lock index e18e7772..8267733c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1687,6 +1687,16 @@ dependencies = [ "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "static_index" +version = "0.1.0" +dependencies = [ + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "string" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 535a0cee..acd834e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ members = [ "r2d2", "redis-session", "state", + "static_index", "template_askama", "template_tera", "tls", diff --git a/async_ex1/src/main.rs b/async_ex1/src/main.rs index 879e7a4d..dda193f9 100644 --- a/async_ex1/src/main.rs +++ b/async_ex1/src/main.rs @@ -12,9 +12,8 @@ // - POSTing json body // 3. chaining futures into a single response used by an asynch endpoint // -// There are 2 versions in this example, one that uses Boxed Futures and the other that uses Impl -// Future, available since rustc v1.26. - +// There are 2 versions in this example, one that uses Boxed Futures and the +// other that uses Impl Future, available since rustc v1.26. extern crate actix; extern crate actix_web; @@ -28,12 +27,14 @@ extern crate env_logger; extern crate futures; extern crate validator; -use actix_web::{client, dev::Handler, http::Method, server, App, AsyncResponder, Body, - Error, HttpMessage, HttpRequest, HttpResponse, Json, Result}; +use actix_web::{ + client, http::Method, server, App, AsyncResponder, Error, HttpMessage, HttpResponse, + Json, +}; use futures::{future::ok as fut_ok, Future}; use std::collections::HashMap; use std::time::Duration; -use validator::{Validate, ValidationError}; +use validator::Validate; #[derive(Debug, Validate, Deserialize, Serialize)] struct SomeData { @@ -55,9 +56,8 @@ struct HttpBinResponse { url: String, } - // ----------------------------------------------------------------------- -// v1 uses Boxed Futures, which were the only option prior to rustc v1.26 +// v1 uses Boxed Futures, which were the only option prior to rustc v1.26 // ----------------------------------------------------------------------- /// post json to httpbin, get it back in the response body, return deserialized @@ -96,9 +96,8 @@ fn create_something_v1( .responder() } - // --------------------------------------------------------------- -// v2 uses impl Future, available as of rustc v1.26 +// v2 uses impl Future, available as of rustc v1.26 // --------------------------------------------------------------- /// post json to httpbin, get it back in the response body, return deserialized @@ -118,37 +117,36 @@ fn step_x_v2(data: SomeData) -> impl Future { ) } -fn create_something_v2(some_data: Json) - -> impl Future { - step_x_v2(some_data.into_inner()) - .and_then(|some_data_2| { - step_x_v2(some_data_2).and_then(|some_data_3| { - step_x_v2(some_data_3).and_then(|d| - Ok(HttpResponse::Ok() +fn create_something_v2( + some_data: Json, +) -> impl Future { + step_x_v2(some_data.into_inner()).and_then(|some_data_2| { + step_x_v2(some_data_2).and_then(|some_data_3| { + step_x_v2(some_data_3).and_then(|d| { + Ok(HttpResponse::Ok() .content_type("application/json") .body(serde_json::to_string(&d).unwrap()) - .into())) + .into()) }) }) + }) } - - - fn main() { env_logger::init(); let sys = actix::System::new("asyncio_example"); server::new(move || { - App::new() + App::new() .resource("/something_v1", |r| { - r.method(Method::POST).with(create_something_v1)}) + r.method(Method::POST).with(create_something_v1) + }) .resource("/something_v2", |r| { - r.method(Method::POST).with_async(create_something_v2)}) - }) - .bind("127.0.0.1:8088") - .unwrap() - .start(); + r.method(Method::POST).with_async(create_something_v2) + }) + }).bind("127.0.0.1:8088") + .unwrap() + .start(); println!("Started http server: 127.0.0.1:8088"); let _ = sys.run(); diff --git a/basics/src/main.rs b/basics/src/main.rs index d75117be..cef4d4a2 100644 --- a/basics/src/main.rs +++ b/basics/src/main.rs @@ -9,8 +9,9 @@ use futures::Stream; use actix_web::http::{header, Method, StatusCode}; use actix_web::middleware::session::{self, RequestSession}; -use actix_web::{error, fs, middleware, pred, server, App, Error, HttpRequest, - HttpResponse, Result}; +use actix_web::{ + error, fs, middleware, pred, server, App, Error, HttpRequest, HttpResponse, Result, +}; use futures::future::{result, FutureResult}; use std::{env, io}; @@ -66,10 +67,7 @@ fn with_param(req: HttpRequest) -> HttpResponse { HttpResponse::Ok() .content_type("test/plain") - .body(format!( - "Hello {}!", - req.match_info().get("name").unwrap() - )) + .body(format!("Hello {}!", req.match_info().get("name").unwrap())) } fn main() { diff --git a/cookie-auth/src/auth.rs b/cookie-auth/src/auth.rs index a110d985..a1477ca9 100644 --- a/cookie-auth/src/auth.rs +++ b/cookie-auth/src/auth.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] use std::rc::Rc; use cookie::{Cookie, CookieJar, Key}; @@ -88,7 +89,8 @@ impl> Middleware for IdentityService { fn start(&self, req: &mut HttpRequest) -> Result { let mut req = req.clone(); - let fut = self.backend + let fut = self + .backend .from_request(&mut req) .then(move |res| match res { Ok(id) => { diff --git a/cookie-auth/src/main.rs b/cookie-auth/src/main.rs index cba7f5a9..6b32bf10 100644 --- a/cookie-auth/src/main.rs +++ b/cookie-auth/src/main.rs @@ -5,7 +5,6 @@ extern crate env_logger; extern crate futures; extern crate time; -use actix_web::middleware::session::RequestSession; use actix_web::{middleware, server, App, HttpRequest, HttpResponse}; mod auth; diff --git a/diesel/src/main.rs b/diesel/src/main.rs index d4e6613f..355f31d2 100644 --- a/diesel/src/main.rs +++ b/diesel/src/main.rs @@ -18,8 +18,10 @@ extern crate r2d2; extern crate uuid; use actix::prelude::*; -use actix_web::{http, middleware, server, App, AsyncResponder, FutureResponse, - HttpResponse, Path, State}; +use actix_web::{ + http, middleware, server, App, AsyncResponder, FutureResponse, HttpResponse, Path, + State, +}; use diesel::prelude::*; use diesel::r2d2::{ConnectionManager, Pool}; diff --git a/http-proxy/src/main.rs b/http-proxy/src/main.rs index bf3b35be..64adfbcf 100644 --- a/http-proxy/src/main.rs +++ b/http-proxy/src/main.rs @@ -3,8 +3,10 @@ extern crate actix_web; extern crate env_logger; extern crate futures; -use actix_web::{client, middleware, server, App, AsyncResponder, Body, Error, - HttpMessage, HttpRequest, HttpResponse}; +use actix_web::{ + client, middleware, server, App, AsyncResponder, Body, Error, HttpMessage, + HttpRequest, HttpResponse, +}; use futures::{Future, Stream}; /// Stream client request response and then send body to a server response diff --git a/json/src/main.rs b/json/src/main.rs index 67c77252..5f320370 100644 --- a/json/src/main.rs +++ b/json/src/main.rs @@ -9,8 +9,10 @@ extern crate serde_derive; #[macro_use] extern crate json; -use actix_web::{error, http, middleware, server, App, AsyncResponder, Error, - HttpMessage, HttpRequest, HttpResponse, Json}; +use actix_web::{ + error, http, middleware, server, App, AsyncResponder, Error, HttpMessage, + HttpRequest, HttpResponse, Json, +}; use bytes::BytesMut; use futures::{Future, Stream}; diff --git a/juniper/src/main.rs b/juniper/src/main.rs index df5ef280..8147d6d3 100644 --- a/juniper/src/main.rs +++ b/juniper/src/main.rs @@ -13,8 +13,10 @@ extern crate env_logger; extern crate futures; use actix::prelude::*; -use actix_web::{http, middleware, server, App, AsyncResponder, Error, FutureResponse, - HttpRequest, HttpResponse, Json, State}; +use actix_web::{ + http, middleware, server, App, AsyncResponder, Error, FutureResponse, HttpRequest, + HttpResponse, Json, State, +}; use futures::future::Future; use juniper::http::graphiql::graphiql_source; use juniper::http::GraphQLRequest; diff --git a/multipart/src/main.rs b/multipart/src/main.rs index dd07d664..f9e8919a 100644 --- a/multipart/src/main.rs +++ b/multipart/src/main.rs @@ -5,8 +5,10 @@ extern crate env_logger; extern crate futures; use actix::*; -use actix_web::{http, middleware, multipart, server, App, AsyncResponder, Error, - HttpMessage, HttpRequest, HttpResponse}; +use actix_web::{ + http, middleware, multipart, server, App, AsyncResponder, Error, HttpMessage, + HttpRequest, HttpResponse, +}; use futures::future::{result, Either}; use futures::{Future, Stream}; diff --git a/protobuf/src/main.rs b/protobuf/src/main.rs index 6da9999f..5b4d9534 100644 --- a/protobuf/src/main.rs +++ b/protobuf/src/main.rs @@ -9,8 +9,9 @@ extern crate prost; #[macro_use] extern crate prost_derive; -use actix_web::{http, middleware, server, App, AsyncResponder, Error, HttpRequest, - HttpResponse}; +use actix_web::{ + http, middleware, server, App, AsyncResponder, Error, HttpRequest, HttpResponse, +}; use futures::Future; mod protobuf; diff --git a/protobuf/src/protobuf.rs b/protobuf/src/protobuf.rs index 1e46a6de..9f1974e9 100644 --- a/protobuf/src/protobuf.rs +++ b/protobuf/src/protobuf.rs @@ -132,7 +132,8 @@ where } let limit = self.limit; - let fut = req.from_err() + let fut = req + .from_err() .fold(BytesMut::new(), move |mut body, chunk| { if (body.len() + chunk.len()) > limit { Err(ProtoBufPayloadError::Overflow) diff --git a/r2d2/src/db.rs b/r2d2/src/db.rs index 340d28b9..5df9b3ff 100644 --- a/r2d2/src/db.rs +++ b/r2d2/src/db.rs @@ -35,10 +35,10 @@ impl Handler for DbExecutor { &[&uuid, &msg.name], ).unwrap(); - Ok(conn.query_row( - "SELECT name FROM users WHERE id=$1", - &[&uuid], - |row| row.get(0), - ).map_err(|_| io::Error::new(io::ErrorKind::Other, "db error"))?) + Ok(conn + .query_row("SELECT name FROM users WHERE id=$1", &[&uuid], |row| { + row.get(0) + }) + .map_err(|_| io::Error::new(io::ErrorKind::Other, "db error"))?) } } diff --git a/r2d2/src/main.rs b/r2d2/src/main.rs index 439ed8ef..dbeb8256 100644 --- a/r2d2/src/main.rs +++ b/r2d2/src/main.rs @@ -11,8 +11,9 @@ extern crate serde_json; extern crate uuid; use actix::prelude::*; -use actix_web::{http, middleware, server, App, AsyncResponder, Error, HttpRequest, - HttpResponse}; +use actix_web::{ + http, middleware, server, App, AsyncResponder, Error, HttpRequest, HttpResponse, +}; use futures::future::Future; use r2d2_sqlite::SqliteConnectionManager; diff --git a/state/src/main.rs b/state/src/main.rs index 309de26d..9bc49879 100644 --- a/state/src/main.rs +++ b/state/src/main.rs @@ -29,10 +29,7 @@ fn index(req: HttpRequest) -> HttpResponse { println!("{:?}", req); req.state().counter.set(req.state().counter.get() + 1); - HttpResponse::Ok().body(format!( - "Num of requests: {}", - req.state().counter.get() - )) + HttpResponse::Ok().body(format!("Num of requests: {}", req.state().counter.get())) } fn main() { diff --git a/static_index/Cargo.toml b/static_index/Cargo.toml index ea69161e..853cbdcc 100644 --- a/static_index/Cargo.toml +++ b/static_index/Cargo.toml @@ -2,14 +2,10 @@ name = "static_index" version = "0.1.0" authors = ["Jose Marinez "] +workspace = "../" [dependencies] futures = "0.1" env_logger = "0.5" -actix = "^0.5.5" -actix-web = { git="https://github.com/actix/actix-web.git" } - -[workspace] -members = [ - "./" -] \ No newline at end of file +actix = "0.5" +actix-web = "0.6" diff --git a/static_index/src/main.rs b/static_index/src/main.rs index f7434702..3b409a0d 100644 --- a/static_index/src/main.rs +++ b/static_index/src/main.rs @@ -2,31 +2,27 @@ extern crate actix; extern crate actix_web; extern crate env_logger; -use actix_web::{fs, App, server, middleware}; +use actix_web::{fs, middleware, server, App}; fn main() { ::std::env::set_var("RUST_LOG", "actix_web=info"); ::std::env::set_var("RUST_BACKTRACE", "1"); env_logger::init(); - + let sys = actix::System::new("static_index"); - - server::new( - || App::new() + server::new(|| { + App::new() // enable logger .middleware(middleware::Logger::default()) .handler( "/", fs::StaticFiles::new("./static/").index_file("index.html") - ) - ) - - .bind("127.0.0.1:8080").expect( "Can not start server on given IP/Port" ) + ) + }).bind("127.0.0.1:8080") + .expect("Can not start server on given IP/Port") .start(); - - println!("Started http server: 127.0.0.1:8080"); - let _ = sys.run(); + println!("Started http server: 127.0.0.1:8080"); + let _ = sys.run(); } - diff --git a/template_tera/src/main.rs b/template_tera/src/main.rs index 3ceb7d53..af8bcae7 100644 --- a/template_tera/src/main.rs +++ b/template_tera/src/main.rs @@ -6,7 +6,9 @@ extern crate tera; use std::collections::HashMap; -use actix_web::{error, http, middleware, server, App, Error, HttpResponse, Query, State}; +use actix_web::{ + error, http, middleware, server, App, Error, HttpResponse, Query, State, +}; struct AppState { template: tera::Tera, // <- store tera template in application state diff --git a/tls/src/main.rs b/tls/src/main.rs index 8e9d7204..ff47dbe5 100644 --- a/tls/src/main.rs +++ b/tls/src/main.rs @@ -27,9 +27,7 @@ fn main() { builder .set_private_key_file("key.pem", SslFiletype::PEM) .unwrap(); - builder - .set_certificate_chain_file("cert.pem") - .unwrap(); + builder.set_certificate_chain_file("cert.pem").unwrap(); server::new(|| { App::new() diff --git a/web-cors/backend/src/main.rs b/web-cors/backend/src/main.rs index 7290eafc..24467b7a 100644 --- a/web-cors/backend/src/main.rs +++ b/web-cors/backend/src/main.rs @@ -7,11 +7,9 @@ extern crate futures; extern crate serde; extern crate serde_json; -use actix_web::{http::{header, Method}, - middleware, - middleware::cors::Cors, - server, - App}; +use actix_web::{ + http::{header, Method}, middleware, middleware::cors::Cors, server, App, +}; use std::env; mod user; diff --git a/websocket-chat/src/main.rs b/websocket-chat/src/main.rs index 6707db7e..496ceecf 100644 --- a/websocket-chat/src/main.rs +++ b/websocket-chat/src/main.rs @@ -83,9 +83,7 @@ impl Actor for WsChatSession { fn stopping(&mut self, ctx: &mut Self::Context) -> Running { // notify chat server - ctx.state() - .addr - .do_send(server::Disconnect { id: self.id }); + ctx.state().addr.do_send(server::Disconnect { id: self.id }); Running::Stop } } diff --git a/websocket-chat/src/server.rs b/websocket-chat/src/server.rs index 41fcb970..53c1b0e6 100644 --- a/websocket-chat/src/server.rs +++ b/websocket-chat/src/server.rs @@ -114,10 +114,7 @@ impl Handler for ChatServer { self.sessions.insert(id, msg.addr); // auto join session to Main room - self.rooms - .get_mut(&"Main".to_owned()) - .unwrap() - .insert(id); + self.rooms.get_mut(&"Main".to_owned()).unwrap().insert(id); // send id back id diff --git a/websocket-tcp-chat/src/client.rs b/websocket-tcp-chat/src/client.rs index afe9b1d4..9712d165 100644 --- a/websocket-tcp-chat/src/client.rs +++ b/websocket-tcp-chat/src/client.rs @@ -121,8 +121,7 @@ impl Handler for ChatClient { } "/join" => { if v.len() == 2 { - self.framed - .write(codec::ChatRequest::Join(v[1].to_owned())); + self.framed.write(codec::ChatRequest::Join(v[1].to_owned())); } else { println!("!!! room name is required"); } @@ -130,8 +129,7 @@ impl Handler for ChatClient { _ => println!("!!! unknown command"), } } else { - self.framed - .write(codec::ChatRequest::Message(m.to_owned())); + self.framed.write(codec::ChatRequest::Message(m.to_owned())); } } } diff --git a/websocket-tcp-chat/src/codec.rs b/websocket-tcp-chat/src/codec.rs index 0670d6a7..55cf0db3 100644 --- a/websocket-tcp-chat/src/codec.rs +++ b/websocket-tcp-chat/src/codec.rs @@ -71,7 +71,7 @@ impl Encoder for ChatCodec { let msg_ref: &[u8] = msg.as_ref(); dst.reserve(msg_ref.len() + 2); - dst.put_u16::(msg_ref.len() as u16); + dst.put_u16_be(msg_ref.len() as u16); dst.put(msg_ref); Ok(()) @@ -114,7 +114,7 @@ impl Encoder for ClientChatCodec { let msg_ref: &[u8] = msg.as_ref(); dst.reserve(msg_ref.len() + 2); - dst.put_u16::(msg_ref.len() as u16); + dst.put_u16_be(msg_ref.len() as u16); dst.put(msg_ref); Ok(()) diff --git a/websocket-tcp-chat/src/main.rs b/websocket-tcp-chat/src/main.rs index be9dfa73..2e12c25d 100644 --- a/websocket-tcp-chat/src/main.rs +++ b/websocket-tcp-chat/src/main.rs @@ -19,7 +19,6 @@ use std::time::Instant; use actix::*; use actix_web::server::HttpServer; -use actix_web::ws::WsWriter; use actix_web::{fs, http, ws, App, Error, HttpRequest, HttpResponse}; mod codec; @@ -88,9 +87,7 @@ impl Actor for WsChatSession { fn stopping(&mut self, ctx: &mut Self::Context) -> Running { // notify chat server - ctx.state() - .addr - .do_send(server::Disconnect { id: self.id }); + ctx.state().addr.do_send(server::Disconnect { id: self.id }); Running::Stop } } diff --git a/websocket-tcp-chat/src/server.rs b/websocket-tcp-chat/src/server.rs index a69c49a4..28e4297a 100644 --- a/websocket-tcp-chat/src/server.rs +++ b/websocket-tcp-chat/src/server.rs @@ -112,10 +112,7 @@ impl Handler for ChatServer { self.sessions.insert(id, msg.addr); // auto join session to Main room - self.rooms - .get_mut(&"Main".to_owned()) - .unwrap() - .insert(id); + self.rooms.get_mut(&"Main".to_owned()).unwrap().insert(id); // send id back id diff --git a/websocket/src/main.rs b/websocket/src/main.rs index c7bc58b6..6d35b768 100644 --- a/websocket/src/main.rs +++ b/websocket/src/main.rs @@ -9,8 +9,9 @@ extern crate actix_web; extern crate env_logger; use actix::prelude::*; -use actix_web::ws::WsWriter; -use actix_web::{fs, http, middleware, server, ws, App, Error, HttpRequest, HttpResponse}; +use actix_web::{ + fs, http, middleware, server, ws, App, Error, HttpRequest, HttpResponse, +}; /// do websocket handshake and start `MyWebSocket` actor fn ws_index(r: HttpRequest) -> Result {