From 7364e088beb2737f840028bb8a5d734a449b183e Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 21 Oct 2017 19:35:50 -0700 Subject: [PATCH] basic example --- examples/basic.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++ src/application.rs | 3 ++- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 examples/basic.rs diff --git a/examples/basic.rs b/examples/basic.rs new file mode 100644 index 000000000..46279498b --- /dev/null +++ b/examples/basic.rs @@ -0,0 +1,48 @@ +#![allow(unused_variables)] +extern crate actix; +extern crate actix_web; +extern crate env_logger; + +use actix_web::*; + +/// somple handle +fn index(req: HttpRequest, payload: Payload, state: &()) -> HttpResponse { + println!("{:?}", req); + httpcodes::HTTPOk.into() +} + +/// handle with path parameters like `/name/{name}/` +fn with_param(req: HttpRequest, payload: Payload, state: &()) -> HttpResponse { + println!("{:?}", req); + + HttpResponse::builder(StatusCode::OK) + .content_type("test/plain") + .body(Body::Binary( + format!("Hello {}!", req.match_info().get("name").unwrap()).into())).unwrap() +} + +fn main() { + let sys = actix::System::new("ws-example"); + + HttpServer::new( + Application::default("/") + // register simple handler, handle all methods + .handler("/index.html", index) + // with path parameters + .resource("/user/{name}/", |r| r.handler(Method::GET, with_param)) + // redirect + .resource("/", |r| r.handler(Method::GET, |req, _, _| { + println!("{:?}", req); + + httpcodes::HTTPFound + .builder() + .header("LOCATION", "/index.html") + .body(Body::Empty) + })) + // static files + .route_handler("/static", StaticFiles::new("static/", true))) + .serve::<_, ()>("127.0.0.1:8080").unwrap(); + + println!("Started http server: 127.0.0.1:8080"); + let _ = sys.run(); +} diff --git a/src/application.rs b/src/application.rs index baa8659fc..1f4295085 100644 --- a/src/application.rs +++ b/src/application.rs @@ -172,7 +172,8 @@ impl ApplicationBuilder where S: 'static { self } - /// This method register handler for specified path. + /// This method register handler for specified path prefix. + /// Any path that starts with this prefix matches handler. /// /// ```rust /// extern crate actix_web;