diff --git a/examples/basic/src/main.rs b/examples/basic/src/main.rs index f3f519807..0c52c1fed 100644 --- a/examples/basic/src/main.rs +++ b/examples/basic/src/main.rs @@ -24,14 +24,75 @@ 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)?; } - Ok("Welcome!".into()) + let html = format!(r#" + + + actix - basic + + + + subpage +

Welcome

+ counter = {} + +"#, counter); + + Ok(HttpResponse::build(StatusCode::OK) + .content_type("text/html; charset=utf-8") + .body(&html).unwrap()) + + +} + +/// favicon handler +fn favicon(req: HttpRequest) -> Result { + Ok(fs::NamedFile::open("../static/favicon.ico")?) +} + +/// subpage handler +fn subpage(req: HttpRequest) -> Result { + let html = format!(r#" + + + actix - basic + + + + index + sub page with image
+ + +"#); + Ok(HttpResponse::build(StatusCode::OK) + .content_type("text/html; charset=utf-8") + .body(&html).unwrap()) +} + +/// 404 handler +fn p404(req: HttpRequest) -> Result { + let html = format!(r#" + + + actix - basic + + + + index +

404

+ +"#); + Ok(HttpResponse::build(StatusCode::NOT_FOUND) + .content_type("text/html; charset=utf-8") + .body(&html).unwrap()) } /// async handler @@ -70,8 +131,13 @@ fn main() { .secure(false) .finish() )) + // register favicon + .resource("/favicon.ico", |r| r.f(favicon)) + // register assets + .resource("/assets/{tail:.*}", |r| r.h(fs::StaticFiles::new("tail", "../static/", false))) // register simple route, handle all methods .resource("/index.html", |r| r.f(index)) + .resource("/subpage.html", |r| r.f(subpage)) // with path parameters .resource("/user/{name}/", |r| r.method(Method::GET).f(with_param)) // async handler @@ -93,7 +159,13 @@ fn main() { HttpResponse::Found() .header("LOCATION", "/index.html") .finish() - }))) + })) + // default + .default_resource(|r| { + r.method(Method::GET).f(p404); + r.route().p(pred::Not(pred::Get())).f(|req| httpcodes::HTTPMethodNotAllowed); + }) + ) .bind("0.0.0.0:8080").unwrap() .start(); diff --git a/examples/static/favicon.ico b/examples/static/favicon.ico new file mode 100644 index 000000000..1cec3801a Binary files /dev/null and b/examples/static/favicon.ico differ diff --git a/examples/static/font-awesome_4-7-0_rocket_256_0_007dff_none.png b/examples/static/font-awesome_4-7-0_rocket_256_0_007dff_none.png new file mode 100644 index 000000000..d824eb110 Binary files /dev/null and b/examples/static/font-awesome_4-7-0_rocket_256_0_007dff_none.png differ