diff --git a/README.md b/README.md index c9913f2b..dec357a5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Actix web [![Build Status](https://travis-ci.org/actix/actix-web.svg?branch=master)](https://travis-ci.org/actix/actix-web) [![Build status](https://ci.appveyor.com/api/projects/status/kkdb4yce7qhm5w85/branch/master?svg=true)](https://ci.appveyor.com/project/fafhrd91/actix-web-hdy9d/branch/master) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) [![crates.io](http://meritbadge.herokuapp.com/actix-web)](https://crates.io/crates/actix-web) [![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -Actix web is a small, fast, down-to-earth, open source rust web framework. +Actix web is a small, fast, practical, open source rust web framework. ```rust,ignore extern crate actix_web; diff --git a/examples/basics/README.md b/examples/basics/README.md index 45772e91..154fad9d 100644 --- a/examples/basics/README.md +++ b/examples/basics/README.md @@ -1,11 +1,11 @@ -# basic +# basics ## Usage ### server ```bash -cd actix-web/examples/basic +cd actix-web/examples/basics cargo run # Started http server: 127.0.0.1:8080 ``` diff --git a/examples/basics/src/main.rs b/examples/basics/src/main.rs index 2c7b714f..68dc17f3 100644 --- a/examples/basics/src/main.rs +++ b/examples/basics/src/main.rs @@ -11,10 +11,16 @@ use actix_web::*; use actix_web::middleware::RequestSession; use futures::future::{FutureResult, result}; +/// favicon handler +fn favicon(req: HttpRequest) -> Result { + Ok(fs::NamedFile::open("../static/favicon.ico")?) +} -/// simple handler +/// simple index handler fn index(mut req: HttpRequest) -> Result { println!("{:?}", req); + + // example of ... if let Ok(ch) = req.payload_mut().readany().poll() { if let futures::Async::Ready(Some(d)) = ch { println!("{}", String::from_utf8_lossy(d.as_ref())); @@ -22,16 +28,48 @@ fn index(mut req: HttpRequest) -> Result { } // session + let mut counter = 1; if let Some(count) = req.session().get::("counter")? { println!("SESSION value: {}", count); - req.session().set("counter", count+1)?; + counter = count + 1; + req.session().set("counter", counter)?; } else { - req.session().set("counter", 1)?; + req.session().set("counter", counter)?; } - Ok("Welcome!".into()) + // html + let html = format!(r#"actix - basics + +

Welcome

+ session counter = {} + +"#, counter); + + // response + Ok(HttpResponse::build(StatusCode::OK) + .content_type("text/html; charset=utf-8") + .body(&html).unwrap()) + } +/// 404 handler +fn p404(req: HttpRequest) -> Result { + + // html + let html = format!(r#"actix - basics + + back to home +

404

+ +"#); + + // response + Ok(HttpResponse::build(StatusCode::NOT_FOUND) + .content_type("text/html; charset=utf-8") + .body(&html).unwrap()) +} + + /// async handler fn index_async(req: HttpRequest) -> FutureResult { @@ -68,6 +106,8 @@ fn main() { .secure(false) .finish() )) + // register favicon + .resource("/favicon.ico", |r| r.f(favicon)) // register simple route, handle all methods .resource("/index.html", |r| r.f(index)) // with path parameters @@ -90,8 +130,15 @@ fn main() { HttpResponse::Found() .header("LOCATION", "/index.html") .finish() - }))) - .bind("0.0.0.0:8080").unwrap() + })) + // default + .default_resource(|r| { + r.method(Method::GET).f(p404); + r.route().p(pred::Not(pred::Get())).f(|req| httpcodes::HTTPMethodNotAllowed); + })) + + .bind("127.0.0.1:8080").expect("Can not bind to 127.0.0.1:8080") + .shutdown_timeout(0) // <- Set shutdown timeout to 0 seconds (default 60s) .start(); println!("Starting http server: 127.0.0.1:8080"); diff --git a/examples/static/actixLogo.png b/examples/static/actixLogo.png new file mode 100644 index 00000000..142e4e8d Binary files /dev/null and b/examples/static/actixLogo.png differ diff --git a/examples/static/favicon.ico b/examples/static/favicon.ico new file mode 100644 index 00000000..03018db5 Binary files /dev/null and b/examples/static/favicon.ico differ