1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +01:00
actix-extras/examples/basic.rs

98 lines
3.1 KiB
Rust
Raw Normal View History

2017-10-22 04:35:50 +02:00
#![allow(unused_variables)]
2017-11-27 02:30:35 +01:00
#![cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))]
2017-10-22 04:35:50 +02:00
extern crate actix;
extern crate actix_web;
extern crate env_logger;
2017-11-03 21:35:34 +01:00
extern crate futures;
2017-10-22 04:35:50 +02:00
use actix_web::*;
2017-11-27 02:30:35 +01:00
use actix_web::middlewares::RequestSession;
2017-11-30 23:42:20 +01:00
use futures::future::{FutureResult, result};
2017-10-22 04:35:50 +02:00
2017-11-28 01:41:37 +01:00
/// simple handler
2017-11-27 06:18:38 +01:00
fn index(mut req: HttpRequest) -> Result<HttpResponse> {
2017-10-22 04:35:50 +02:00
println!("{:?}", req);
2017-11-27 04:00:57 +01:00
if let Ok(ch) = req.payload_mut().readany() {
2017-11-06 10:24:49 +01:00
if let futures::Async::Ready(Some(d)) = ch {
println!("{}", String::from_utf8_lossy(d.0.as_ref()));
}
}
2017-11-27 02:30:35 +01:00
// session
if let Some(count) = req.session().get::<i32>("counter")? {
println!("SESSION value: {}", count);
req.session().set("counter", count+1)?;
} else {
req.session().set("counter", 1)?;
}
Ok(HttpResponse::Ok().into())
2017-10-22 04:35:50 +02:00
}
2017-11-28 01:41:37 +01:00
/// async handler
2017-11-30 23:42:20 +01:00
fn index_async(req: HttpRequest) -> FutureResult<HttpResponse, Error>
2017-11-03 21:35:34 +01:00
{
println!("{:?}", req);
2017-11-30 23:42:20 +01:00
result(HttpResponse::Ok()
.content_type("text/html")
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))
.map_err(|e| e.into()))
2017-11-03 21:35:34 +01:00
}
2017-11-28 01:41:37 +01:00
/// handler with path parameters like `/user/{name}/`
2017-11-27 06:18:38 +01:00
fn with_param(req: HttpRequest) -> Result<HttpResponse>
2017-10-30 05:39:59 +01:00
{
2017-10-22 04:35:50 +02:00
println!("{:?}", req);
Ok(HttpResponse::Ok()
2017-10-30 05:39:59 +01:00
.content_type("test/plain")
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))?)
2017-10-22 04:35:50 +02:00
}
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
let _ = env_logger::init();
2017-10-22 04:35:50 +02:00
let sys = actix::System::new("ws-example");
HttpServer::new(
Application::default("/")
// enable logger
2017-11-24 00:17:16 +01:00
.middleware(middlewares::Logger::default())
2017-11-27 02:30:35 +01:00
// cookie session middleware
.middleware(middlewares::SessionStorage::new(
middlewares::CookieSessionBackend::build(&[0; 32])
.secure(false)
.finish()
))
2017-11-10 07:08:54 +01:00
// register simple handle r, handle all methods
2017-10-22 04:35:50 +02:00
.handler("/index.html", index)
2017-12-02 08:32:15 +01:00
// with path parameters
2017-12-04 22:32:05 +01:00
.resource("/user/{name}/", |r| r.route().method(Method::GET).handler(with_param))
2017-11-03 21:35:34 +01:00
// async handler
2017-12-04 22:32:05 +01:00
.resource("/async/{name}", |r| r.route().method(Method::GET).async(index_async))
2017-10-22 04:35:50 +02:00
// redirect
2017-12-04 22:32:05 +01:00
.resource("/", |r| r.route().method(Method::GET).handler(|req| {
2017-10-22 04:35:50 +02:00
println!("{:?}", req);
2017-11-28 21:42:53 +01:00
httpcodes::HTTPFound
.build()
.header("LOCATION", "/index.html")
.body(Body::Empty)
2017-10-22 04:35:50 +02:00
}))
2017-11-29 22:26:55 +01:00
.handler("/test", |req| {
match *req.method() {
Method::GET => httpcodes::HTTPOk,
Method::POST => httpcodes::HTTPMethodNotAllowed,
_ => httpcodes::HTTPNotFound,
}
})
2017-10-22 04:35:50 +02:00
// static files
.route("/static", fs::StaticFiles::new("examples/static/", true)))
2017-10-22 04:35:50 +02:00
.serve::<_, ()>("127.0.0.1:8080").unwrap();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
}