diff --git a/Cargo.lock b/Cargo.lock index a45c63ab..ed7d6340 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1390,11 +1390,11 @@ version = "2.0.0" dependencies = [ "actix-files 0.6.0-beta.16", "actix-session 0.5.0-beta.7", - "actix-utils 3.0.0", "actix-web 4.0.0-rc.1", + "async-stream", "env_logger 0.9.0", + "log", "tokio 1.16.1", - "tokio-stream", ] [[package]] diff --git a/basics/basics/Cargo.toml b/basics/basics/Cargo.toml index 9ff79585..e42e7351 100644 --- a/basics/basics/Cargo.toml +++ b/basics/basics/Cargo.toml @@ -8,7 +8,8 @@ edition = "2018" actix-web = "4.0.0-beta.21" actix-files = "0.6.0-beta.15" actix-session = "0.5.0-beta.7" -actix-utils = "3" -tokio = "1" -tokio-stream = "0.1" + +async-stream = "0.3" env_logger = "0.9" +log = "0.4" +tokio = "1" diff --git a/basics/basics/src/main.rs b/basics/basics/src/main.rs index ef459d47..92c45ad7 100644 --- a/basics/basics/src/main.rs +++ b/basics/basics/src/main.rs @@ -1,23 +1,28 @@ -use actix_files as fs; +use std::convert::Infallible; +use std::io; + +use actix_files::{Files, NamedFile}; use actix_session::{CookieSession, Session}; -use actix_web::http::{header, Method, StatusCode}; use actix_web::{ - error, get, middleware, web, App, Either, Error, HttpRequest, HttpResponse, - HttpServer, Result, + error, get, + http::{ + header::{self, ContentType}, + Method, StatusCode, + }, + middleware, web, App, Either, HttpRequest, HttpResponse, HttpServer, Responder, + Result, }; -use std::{env, io}; -use tokio::sync::mpsc; -use tokio_stream::wrappers::UnboundedReceiverStream; +use async_stream::stream; /// favicon handler #[get("/favicon")] -async fn favicon() -> Result { - Ok(fs::NamedFile::open("static/favicon.ico")?) +async fn favicon() -> Result { + Ok(NamedFile::open("static/favicon.ico")?) } /// simple index handler #[get("/welcome")] -async fn welcome(session: Session, req: HttpRequest) -> Result { +async fn welcome(req: HttpRequest, session: Session) -> Result { println!("{:?}", req); // session @@ -32,16 +37,14 @@ async fn welcome(session: Session, req: HttpRequest) -> Result { // response Ok(HttpResponse::build(StatusCode::OK) - .content_type("text/html; charset=utf-8") + .content_type(ContentType::plaintext()) .body(include_str!("../static/welcome.html"))) } -async fn default_handler( - request: HttpRequest, -) -> Result> { - match *request.method() { - actix_web::http::Method::GET => { - let file = fs::NamedFile::open("static/404.html")? +async fn default_handler(req_method: Method) -> Result { + match req_method { + Method::GET => { + let file = NamedFile::open("static/404.html")? .set_status_code(StatusCode::NOT_FOUND); Ok(Either::Left(file)) } @@ -51,12 +54,13 @@ async fn default_handler( /// response body async fn response_body(path: web::Path) -> HttpResponse { - let text = format!("Hello {}!", *path); + let name = path.into_inner(); - let (tx, rx_body) = mpsc::unbounded_channel(); - let _ = tx.send(Ok::<_, Error>(web::Bytes::from(text))); - - HttpResponse::Ok().streaming(UnboundedReceiverStream::from(rx_body)) + HttpResponse::Ok().streaming(stream! { + yield Ok::<_, Infallible>(web::Bytes::from("Hello ")); + yield Ok::<_, Infallible>(web::Bytes::from(name)); + yield Ok::<_, Infallible>(web::Bytes::from("!")); + }) } /// handler with path parameters like `/user/{name}/` @@ -64,17 +68,20 @@ async fn with_param(req: HttpRequest, path: web::Path<(String,)>) -> HttpRespons println!("{:?}", req); HttpResponse::Ok() - .content_type("text/plain") + .content_type(ContentType::plaintext()) .body(format!("Hello {}!", path.0)) } #[actix_web::main] async fn main() -> io::Result<()> { - env::set_var("RUST_LOG", "actix_web=debug,actix_server=info"); - env_logger::init(); + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + + log::info!("starting HTTP server at http://localhost:8080"); HttpServer::new(|| { App::new() + // enable automatic response compression - usually register this first + .wrap(middleware::Compress::default()) // cookie session middleware .wrap(CookieSession::signed(&[0; 32]).secure(false)) // enable logger - always register actix-web Logger middleware last @@ -103,18 +110,21 @@ async fn main() -> io::Result<()> { ) })) // static files - .service(fs::Files::new("/static", "static").show_files_listing()) + .service(Files::new("/static", "static").show_files_listing()) // redirect - .service(web::resource("/").route(web::get().to(|req: HttpRequest| { - println!("{:?}", req); - HttpResponse::Found() - .insert_header((header::LOCATION, "static/welcome.html")) - .finish() - }))) + .service(web::resource("/").route(web::get().to( + |req: HttpRequest| async move { + println!("{:?}", req); + HttpResponse::Found() + .insert_header((header::LOCATION, "static/welcome.html")) + .finish() + }, + ))) // default - .default_service(web::route().to(default_handler)) + .default_service(web::to(default_handler)) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? + .workers(2) .run() .await }