From 665f4edf6b3b5c2eb5a349b19e24cef70ca27a13 Mon Sep 17 00:00:00 2001 From: ami44 Date: Sat, 6 Jan 2018 16:43:59 +0100 Subject: [PATCH] upd examples basics --- examples/basics/README.md | 4 +-- examples/basics/src/main.rs | 59 ++++++++++++++++++++++++++++++---- examples/static/actixLogo.png | Bin 0 -> 8898 bytes examples/static/favicon.ico | Bin 0 -> 1150 bytes 4 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 examples/static/actixLogo.png create mode 100644 examples/static/favicon.ico diff --git a/examples/basics/README.md b/examples/basics/README.md index 45772e915..154fad9de 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 2c7b714f4..68dc17f30 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 0000000000000000000000000000000000000000..142e4e8d57704b6779d54f979dcf50764906d9fe GIT binary patch literal 8898 zcmWk!1vngT7{~lEJ9Er1rcF)f#OXZU-EF$N+0+b^6T`%5yXl^uo}F&nOn2A+o9A&J zj_K&s1qwBZmH*Z!pZ^5K~?|L@3#2TVdEXN}o9Hmvw zUgf#X=c4CLW5-7P(dLiEYCnfkdHxq1&b@I+FI^}v=OR>bOBZ=7zG5f>?Bc6L_m{&GyACz|@LN%e+%6KCs(tB)_}c-l-% zwY0R1jdwRT8hrLmR8>{WOT4_ia>PP{ot;O!x>k2)k>xs-u`w~V{(nwRPSQ<-1$lXi zo;*Rmo@OVGv)*7di+@3fEG{k{6TKep>dG%Hw6wGPTQ_ZjB>HGjDH?Re%g%|7jh&<7 zb9J)0vSOC6X6H{9Z4NIwioh4Ob8+!;b^W`ycXoIUHMxAR^G`njM0LqUVq2)u?-(Ybh-Zi{`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32zM8C`V+(Ir4ul|0b|VFTb1#PnK3 zQ_eByy?IRGU&9BU#l^?>adT5pQVIl>2ie3hai1Bf4RSV3ol%5xAJ~5&WeyajGF3q~ zPjf)J!GNBrF@KQQ54LuEbaVxFz*-ruDsmX9$I7q$+MM+4+RM5{vez>N&YKP&C!$}o>AWTVugcwU}ThiX!`gK5-@D1 z`TERydElwIEQ^MToB7kE4=o~X6F2SpEo!;BxzF~OUHqMZh?|YV=TVfh0S5F!Bf&Jz zjN{oT0EL|y^I29f}YX6YbI^W%eHH&ISBC%Kd%hF@{l**g7fr22DXmMY>ATEc|~unt+o4 literal 0 HcmV?d00001 diff --git a/examples/static/favicon.ico b/examples/static/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..03018db5b5de10571c2038b583fd329f9533bb58 GIT binary patch literal 1150 zcmbVMOGukR5dI6j^&&m=++!~m6f`LYjY?EB2*#pCZPZ#cMl6Vri=?8pAgNlbIzn?717ZzMGfz9aBv`E zl%lw}7-wf^n4FwMYHBLiO94F|FIrkYaqR2sYhDSjRK8+gQc%9>>FFTOC=cR#eSMvM zDW9UEw_HP4*Ee)`cZ>XTk(>J(q0n#knVz0z%+%Br->X)uv9`9xHWDV2N#HwiczB5Z z{(dMFO0>1LMeKwp%EljE{Wo>FHto($Z4IZnN2tnVHGl9v>g0 zprC-?FC6~cgO!y=?Ck77uh%0#{|%@wY0z3&Scr;>3S3@ZvW_^2i;IiA2`McWE1H{I z1Wy_0=;&ZS5_Y>C@$vE8)3P!Y3Zb#FQE;rp;NT#ucXxM@m8BIum4VI8U#zoOEPRjI zY{u#7DeL6^r5B_-(X?L}Q(J*uleh+HgOqe7uTdwV6Q>)c-Z~A;b5MMN83?J^#)aTSQ#F5|s6LWKOSX^Ah#>NJ7MK<#J7c2h< H{&)QYdQcr1 literal 0 HcmV?d00001