diff --git a/cookie-auth/Cargo.toml b/cookie-auth/Cargo.toml index 2e7858d3..81fd8eb9 100644 --- a/cookie-auth/Cargo.toml +++ b/cookie-auth/Cargo.toml @@ -2,8 +2,9 @@ name = "cookie-auth" version = "0.1.0" authors = ["Nikolay Kim "] -workspace = "../" +edition = "2018" +workspace = ".." [dependencies] -actix-web = { git="https://github.com/actix/actix-web.git" } -env_logger = "0.5" +actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +env_logger = "0.6" diff --git a/cookie-auth/src/main.rs b/cookie-auth/src/main.rs index 82db1da3..01ddde23 100644 --- a/cookie-auth/src/main.rs +++ b/cookie-auth/src/main.rs @@ -1,31 +1,26 @@ -extern crate actix; -extern crate actix_web; -extern crate env_logger; - -use actix_web::middleware::identity::RequestIdentity; +use actix_web::middleware::identity::Identity; use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService}; -use actix_web::{middleware, server, App, HttpRequest, HttpResponse}; +use actix_web::{middleware, web, App, HttpResponse, HttpServer}; -fn index(req: &HttpRequest) -> String { - format!("Hello {}", req.identity().unwrap_or("Anonymous".to_owned())) +fn index(id: Identity) -> String { + format!("Hello {}", id.identity().unwrap_or("Anonymous".to_owned())) } -fn login(req: &HttpRequest) -> HttpResponse { - req.remember("user1".to_owned()); +fn login(id: Identity) -> HttpResponse { + id.remember("user1".to_owned()); HttpResponse::Found().header("location", "/").finish() } -fn logout(req: &HttpRequest) -> HttpResponse { - req.forget(); +fn logout(id: Identity) -> HttpResponse { + id.forget(); HttpResponse::Found().header("location", "/").finish() } -fn main() { - ::std::env::set_var("RUST_LOG", "actix_web=info"); +fn main() -> std::io::Result<()> { + std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); - let sys = actix::System::new("cookie-auth"); - server::new(|| { + HttpServer::new(|| { App::new() .middleware(middleware::Logger::default()) .middleware(IdentityService::new( @@ -33,14 +28,10 @@ fn main() { .name("auth-example") .secure(false), )) - .resource("/login", |r| r.f(login)) - .resource("/logout", |r| r.f(logout)) - .resource("/", |r| r.f(index)) + .service(web::resource("/login").route(web::post().to(login))) + .service(web::resource("/logout").to(logout)) + .service(web::resource("/").route(web::get().to(index))) }) - .bind("127.0.0.1:8080") - .unwrap() - .start(); - - println!("Started http server: 127.0.0.1:8080"); - let _ = sys.run(); + .bind("127.0.0.1:8080")? + .run() } diff --git a/form/Cargo.toml b/form/Cargo.toml index 42c314ff..1fd56d4e 100644 --- a/form/Cargo.toml +++ b/form/Cargo.toml @@ -2,10 +2,11 @@ name = "form-example" version = "0.1.0" authors = ["Gorm Casper "] +edition = "2018" +workspace = ".." [dependencies] -actix = "0.7" -actix-web = "0.7" +actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } serde = "1.0" serde_derive = "1.0" diff --git a/form/src/main.rs b/form/src/main.rs index 01f416fb..c0c38690 100644 --- a/form/src/main.rs +++ b/form/src/main.rs @@ -1,48 +1,32 @@ -extern crate actix; -extern crate actix_web; - #[macro_use] extern crate serde_derive; use actix_web::{ - http, middleware, server, App, Form, HttpRequest, HttpResponse, Result, State, + middleware, web, App, HttpRequest, HttpResponse, HttpServer, Responder, Result, }; struct AppState { foo: String, } -fn main() { - let sys = actix::System::new("form-example"); - - let _addr = server::new(|| { - App::with_state(AppState { - foo: "bar".to_string(), - }) - .middleware(middleware::Logger::default()) - .resource("/", |r| { - r.method(http::Method::GET).with(index); - }) - .resource("/post1", |r| { - r.method(http::Method::POST).with(handle_post_1) - }) - .resource("/post2", |r| { - r.method(http::Method::POST).with(handle_post_2) - }) - .resource("/post3", |r| { - r.method(http::Method::POST).with(handle_post_3) - }) +fn main() -> std::io::Result<()> { + HttpServer::new(|| { + App::new() + .state(AppState { + foo: "bar".to_string(), + }) + .middleware(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))) + .service(web::resource("/post3").route(web::post().to(handle_post_3))) }) - .bind("127.0.0.1:8080") - .expect("Can not bind to 127.0.0.1:8080") - .start(); - - println!("Starting http server: 127.0.0.1:8080"); - let _ = sys.run(); + .bind("127.0.0.1:8080")? + .run() } -fn index(_req: HttpRequest) -> Result { - Ok(HttpResponse::build(http::StatusCode::OK) +fn index() -> Result { + Ok(HttpResponse::Ok() .content_type("text/html; charset=utf-8") .body(include_str!("../static/form.html"))) } @@ -53,30 +37,28 @@ pub struct MyParams { } /// Simple handle POST request -fn handle_post_1(params: Form) -> Result { - Ok(HttpResponse::build(http::StatusCode::OK) +fn handle_post_1(params: web::Form) -> Result { + Ok(HttpResponse::Ok() .content_type("text/plain") .body(format!("Your name is {}", params.name))) } /// State and POST Params fn handle_post_2( - (state, params): (State, Form), -) -> Result { - Ok(HttpResponse::build(http::StatusCode::OK) - .content_type("text/plain") - .body(format!( - "Your name is {}, and in AppState I have foo: {}", - params.name, state.foo - ))) + state: web::State, + params: web::Form, +) -> HttpResponse { + HttpResponse::Ok().content_type("text/plain").body(format!( + "Your name is {}, and in AppState I have foo: {}", + params.name, state.foo + )) } /// Request and POST Params -fn handle_post_3( - (req, params): (HttpRequest, Form), -) -> Result { +fn handle_post_3(req: HttpRequest, params: web::Form) -> impl Responder { println!("Handling POST request: {:?}", req); - Ok(HttpResponse::build(http::StatusCode::OK) + + HttpResponse::Ok() .content_type("text/plain") - .body(format!("Your name is {}", params.name))) + .body(format!("Your name is {}", params.name)) }