1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-18 22:01:50 +01:00
actix-web/examples/basic.rs

93 lines
2.8 KiB
Rust
Raw Normal View History

2017-10-21 19:35:50 -07:00
#![allow(unused_variables)]
2017-11-26 17:30:35 -08:00
#![cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))]
2017-10-21 19:35:50 -07:00
extern crate actix;
extern crate actix_web;
extern crate env_logger;
2017-11-03 13:35:34 -07:00
extern crate futures;
2017-10-21 19:35:50 -07:00
use actix_web::*;
2017-11-26 19:00:57 -08:00
use actix_web::error::{Error, Result};
2017-11-26 17:30:35 -08:00
use actix_web::middlewares::RequestSession;
2017-11-03 13:35:34 -07:00
use futures::stream::{once, Once};
2017-10-21 19:35:50 -07:00
/// somple handle
2017-11-26 21:18:38 -08:00
fn index(mut req: HttpRequest) -> Result<HttpResponse> {
2017-10-21 19:35:50 -07:00
println!("{:?}", req);
2017-11-26 19:00:57 -08:00
if let Ok(ch) = req.payload_mut().readany() {
2017-11-06 01:24:49 -08:00
if let futures::Async::Ready(Some(d)) = ch {
println!("{}", String::from_utf8_lossy(d.0.as_ref()));
}
}
2017-11-26 17:30:35 -08: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(httpcodes::HTTPOk.into())
2017-10-21 19:35:50 -07:00
}
2017-11-03 13:35:34 -07:00
/// somple handle
2017-11-26 21:18:38 -08:00
fn index_async(req: HttpRequest) -> Once<actix_web::Frame, Error>
2017-11-03 13:35:34 -07:00
{
println!("{:?}", req);
2017-11-26 22:31:29 -08:00
once(Ok(HttpResponse::build(StatusCode::OK)
2017-11-03 13:35:34 -07:00
.content_type("text/html")
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))
.unwrap()
.into()))
}
2017-11-08 16:44:23 -08:00
/// handle with path parameters like `/user/{name}/`
2017-11-26 21:18:38 -08:00
fn with_param(req: HttpRequest) -> Result<HttpResponse>
2017-10-29 21:39:59 -07:00
{
2017-10-21 19:35:50 -07:00
println!("{:?}", req);
2017-11-26 22:31:29 -08:00
Ok(HttpResponse::build(StatusCode::OK)
2017-10-29 21:39:59 -07:00
.content_type("test/plain")
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))?)
2017-10-21 19:35:50 -07:00
}
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
let _ = env_logger::init();
2017-10-21 19:35:50 -07:00
let sys = actix::System::new("ws-example");
HttpServer::new(
Application::default("/")
// enable logger
2017-11-23 15:17:16 -08:00
.middleware(middlewares::Logger::default())
2017-11-26 17:30:35 -08:00
// cookie session middleware
.middleware(middlewares::SessionStorage::new(
middlewares::CookieSessionBackend::build(&[0; 32])
.secure(false)
.finish()
))
2017-11-09 22:08:54 -08:00
// register simple handle r, handle all methods
2017-10-21 19:35:50 -07:00
.handler("/index.html", index)
// with path parameters
.resource("/user/{name}/", |r| r.handler(Method::GET, with_param))
2017-11-03 13:35:34 -07:00
// async handler
.resource("/async/{name}", |r| r.async(Method::GET, index_async))
2017-10-21 19:35:50 -07:00
// redirect
2017-11-26 21:18:38 -08:00
.resource("/", |r| r.handler(Method::GET, |req| {
2017-10-21 19:35:50 -07:00
println!("{:?}", req);
2017-10-29 21:39:59 -07:00
Ok(httpcodes::HTTPFound
2017-11-26 22:31:29 -08:00
.build()
2017-10-29 21:39:59 -07:00
.header("LOCATION", "/index.html")
.body(Body::Empty)?)
2017-10-21 19:35:50 -07:00
}))
// static files
2017-10-26 06:12:23 -07:00
.route_handler("/static", StaticFiles::new("examples/static/", true)))
2017-10-21 19:35:50 -07:00
.serve::<_, ()>("127.0.0.1:8080").unwrap();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
}