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-12-19 00:18:57 -08:00
|
|
|
use futures::Stream;
|
2017-10-21 19:35:50 -07:00
|
|
|
|
|
|
|
use actix_web::*;
|
2017-11-26 17:30:35 -08:00
|
|
|
use actix_web::middlewares::RequestSession;
|
2017-11-30 14:42:20 -08:00
|
|
|
use futures::future::{FutureResult, result};
|
2017-10-21 19:35:50 -07:00
|
|
|
|
2017-11-27 16:41:37 -08:00
|
|
|
/// simple handler
|
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-12-19 00:18:57 -08:00
|
|
|
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()));
|
2017-11-06 01:24:49 -08:00
|
|
|
}
|
|
|
|
}
|
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)?;
|
|
|
|
}
|
|
|
|
|
2017-12-05 13:31:06 -08:00
|
|
|
Ok("Welcome!".into())
|
2017-10-21 19:35:50 -07:00
|
|
|
}
|
|
|
|
|
2017-11-27 16:41:37 -08:00
|
|
|
/// async handler
|
2017-11-30 14:42:20 -08:00
|
|
|
fn index_async(req: HttpRequest) -> FutureResult<HttpResponse, Error>
|
2017-11-03 13:35:34 -07:00
|
|
|
{
|
|
|
|
println!("{:?}", req);
|
|
|
|
|
2017-11-30 14:42:20 -08: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 13:35:34 -07:00
|
|
|
}
|
|
|
|
|
2017-11-27 16:41:37 -08:00
|
|
|
/// handler 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-28 14:23:42 -08:00
|
|
|
Ok(HttpResponse::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() {
|
2017-10-21 22:59:09 -07:00
|
|
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
|
|
|
let _ = env_logger::init();
|
2017-12-18 13:41:52 -08:00
|
|
|
let sys = actix::System::new("basic-example");
|
2017-10-21 19:35:50 -07:00
|
|
|
|
|
|
|
HttpServer::new(
|
2017-12-12 07:40:36 -08:00
|
|
|
|| Application::new()
|
2017-10-21 22:59:09 -07:00
|
|
|
// 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-12-04 14:53:40 -08:00
|
|
|
// register simple route, handle all methods
|
2017-12-06 08:03:08 -08:00
|
|
|
.resource("/index.html", |r| r.f(index))
|
2017-12-01 23:32:15 -08:00
|
|
|
// with path parameters
|
2017-12-06 08:03:08 -08:00
|
|
|
.resource("/user/{name}/", |r| r.method(Method::GET).f(with_param))
|
2017-11-03 13:35:34 -07:00
|
|
|
// async handler
|
2017-12-06 08:03:08 -08:00
|
|
|
.resource("/async/{name}", |r| r.method(Method::GET).a(index_async))
|
|
|
|
.resource("/test", |r| r.f(|req| {
|
2017-11-29 13:26:55 -08:00
|
|
|
match *req.method() {
|
|
|
|
Method::GET => httpcodes::HTTPOk,
|
|
|
|
Method::POST => httpcodes::HTTPMethodNotAllowed,
|
|
|
|
_ => httpcodes::HTTPNotFound,
|
|
|
|
}
|
2017-12-04 14:53:40 -08:00
|
|
|
}))
|
2017-10-21 19:35:50 -07:00
|
|
|
// static files
|
2017-12-08 12:29:28 -08:00
|
|
|
.resource("/static/{tail:.*}",
|
|
|
|
|r| r.h(fs::StaticFiles::new("tail", "examples/static/", true)))
|
2017-12-07 22:54:44 -08:00
|
|
|
// redirect
|
|
|
|
.resource("/", |r| r.method(Method::GET).f(|req| {
|
|
|
|
println!("{:?}", req);
|
|
|
|
|
|
|
|
httpcodes::HTTPFound
|
|
|
|
.build()
|
|
|
|
.header("LOCATION", "/index.html")
|
2017-12-20 16:32:31 -08:00
|
|
|
.finish()
|
2017-12-07 22:54:44 -08:00
|
|
|
})))
|
2017-12-17 12:35:04 -08:00
|
|
|
.bind("127.0.0.1:8080").unwrap()
|
2017-12-19 09:08:36 -08:00
|
|
|
.start();
|
2017-10-21 19:35:50 -07:00
|
|
|
|
2017-12-19 09:51:28 -08:00
|
|
|
println!("Starting http server: 127.0.0.1:8080");
|
2017-10-21 19:35:50 -07:00
|
|
|
let _ = sys.run();
|
|
|
|
}
|