2018-04-13 03:18:42 +02:00
|
|
|
#![allow(unused_variables)]
|
2018-05-08 20:08:43 +02:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
|
2018-04-13 03:18:42 +02:00
|
|
|
|
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
2018-06-13 14:21:41 +02:00
|
|
|
extern crate bytes;
|
2018-04-13 03:18:42 +02:00
|
|
|
extern crate env_logger;
|
|
|
|
extern crate futures;
|
2018-06-13 14:21:41 +02:00
|
|
|
|
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::sync::mpsc;
|
2018-04-13 03:18:42 +02:00
|
|
|
use futures::Stream;
|
|
|
|
|
|
|
|
use actix_web::http::{header, Method, StatusCode};
|
2018-05-08 20:08:43 +02:00
|
|
|
use actix_web::middleware::session::{self, RequestSession};
|
2018-05-21 06:03:29 +02:00
|
|
|
use actix_web::{
|
2018-06-13 14:21:41 +02:00
|
|
|
error, fs, middleware, pred, server, App, Error, HttpRequest, HttpResponse, Path,
|
|
|
|
Result,
|
2018-05-21 06:03:29 +02:00
|
|
|
};
|
2018-05-08 20:08:43 +02:00
|
|
|
use futures::future::{result, FutureResult};
|
|
|
|
use std::{env, io};
|
2018-04-13 03:18:42 +02:00
|
|
|
|
|
|
|
/// favicon handler
|
|
|
|
fn favicon(req: HttpRequest) -> Result<fs::NamedFile> {
|
2018-04-15 04:19:26 +02:00
|
|
|
Ok(fs::NamedFile::open("static/favicon.ico")?)
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// simple index handler
|
2018-04-15 04:19:26 +02:00
|
|
|
fn welcome(mut req: HttpRequest) -> Result<HttpResponse> {
|
2018-04-13 03:18:42 +02:00
|
|
|
println!("{:?}", req);
|
|
|
|
|
|
|
|
// example of ...
|
|
|
|
if let Ok(ch) = req.poll() {
|
|
|
|
if let futures::Async::Ready(Some(d)) = ch {
|
|
|
|
println!("{}", String::from_utf8_lossy(d.as_ref()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// session
|
|
|
|
let mut counter = 1;
|
|
|
|
if let Some(count) = req.session().get::<i32>("counter")? {
|
|
|
|
println!("SESSION value: {}", count);
|
|
|
|
counter = count + 1;
|
|
|
|
req.session().set("counter", counter)?;
|
|
|
|
} else {
|
|
|
|
req.session().set("counter", counter)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
Ok(HttpResponse::build(StatusCode::OK)
|
2018-05-08 20:08:43 +02:00
|
|
|
.content_type("text/html; charset=utf-8")
|
|
|
|
.body(include_str!("../static/welcome.html")))
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 404 handler
|
|
|
|
fn p404(req: HttpRequest) -> Result<fs::NamedFile> {
|
2018-05-08 20:08:43 +02:00
|
|
|
Ok(fs::NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND))
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// async handler
|
2018-05-08 20:08:43 +02:00
|
|
|
fn index_async(req: HttpRequest) -> FutureResult<HttpResponse, Error> {
|
2018-04-13 03:18:42 +02:00
|
|
|
println!("{:?}", req);
|
|
|
|
|
2018-05-08 20:08:43 +02:00
|
|
|
result(Ok(HttpResponse::Ok().content_type("text/html").body(
|
|
|
|
format!("Hello {}!", req.match_info().get("name").unwrap()),
|
|
|
|
)))
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
|
2018-06-13 14:21:41 +02:00
|
|
|
/// async body
|
|
|
|
fn index_async_body(path: Path<String>) -> HttpResponse {
|
|
|
|
let text = format!("Hello {}!", *path);
|
|
|
|
|
|
|
|
let (tx, rx_body) = mpsc::unbounded();
|
|
|
|
let _ = tx.unbounded_send(Bytes::from(text.as_bytes()));
|
|
|
|
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.streaming(rx_body.map_err(|e| error::ErrorBadRequest("bad request")))
|
|
|
|
}
|
|
|
|
|
2018-04-13 03:18:42 +02:00
|
|
|
/// handler with path parameters like `/user/{name}/`
|
2018-05-08 20:08:43 +02:00
|
|
|
fn with_param(req: HttpRequest) -> HttpResponse {
|
2018-04-13 03:18:42 +02:00
|
|
|
println!("{:?}", req);
|
|
|
|
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("test/plain")
|
2018-05-21 06:03:29 +02:00
|
|
|
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
env::set_var("RUST_LOG", "actix_web=debug");
|
|
|
|
env::set_var("RUST_BACKTRACE", "1");
|
|
|
|
env_logger::init();
|
|
|
|
let sys = actix::System::new("basic-example");
|
|
|
|
|
|
|
|
let addr = server::new(
|
|
|
|
|| App::new()
|
|
|
|
// enable logger
|
|
|
|
.middleware(middleware::Logger::default())
|
|
|
|
// cookie session middleware
|
2018-05-08 20:08:43 +02:00
|
|
|
.middleware(session::SessionStorage::new(
|
|
|
|
session::CookieSessionBackend::signed(&[0; 32]).secure(false)
|
2018-04-13 03:18:42 +02:00
|
|
|
))
|
|
|
|
// register favicon
|
2018-04-15 04:19:26 +02:00
|
|
|
.resource("/favicon", |r| r.f(favicon))
|
2018-04-13 03:18:42 +02:00
|
|
|
// register simple route, handle all methods
|
2018-04-15 04:19:26 +02:00
|
|
|
.resource("/welcome", |r| r.f(welcome))
|
2018-04-13 03:18:42 +02:00
|
|
|
// with path parameters
|
2018-04-15 04:19:26 +02:00
|
|
|
.resource("/user/{name}", |r| r.method(Method::GET).f(with_param))
|
2018-04-13 03:18:42 +02:00
|
|
|
// async handler
|
|
|
|
.resource("/async/{name}", |r| r.method(Method::GET).a(index_async))
|
2018-06-13 14:21:41 +02:00
|
|
|
// async handler
|
|
|
|
.resource("/async-body/{name}", |r| r.method(Method::GET).with(index_async_body))
|
2018-04-13 03:18:42 +02:00
|
|
|
.resource("/test", |r| r.f(|req| {
|
|
|
|
match *req.method() {
|
|
|
|
Method::GET => HttpResponse::Ok(),
|
|
|
|
Method::POST => HttpResponse::MethodNotAllowed(),
|
|
|
|
_ => HttpResponse::NotFound(),
|
|
|
|
}
|
|
|
|
}))
|
2018-04-15 04:19:26 +02:00
|
|
|
.resource("/error", |r| r.f(|req| {
|
2018-04-13 03:18:42 +02:00
|
|
|
error::InternalError::new(
|
2018-04-17 19:55:49 +02:00
|
|
|
io::Error::new(io::ErrorKind::Other, "test"), StatusCode::INTERNAL_SERVER_ERROR)
|
2018-04-13 03:18:42 +02:00
|
|
|
}))
|
|
|
|
// static files
|
2018-04-15 04:19:26 +02:00
|
|
|
.handler("/static", fs::StaticFiles::new("static"))
|
2018-04-13 03:18:42 +02:00
|
|
|
// redirect
|
|
|
|
.resource("/", |r| r.method(Method::GET).f(|req| {
|
|
|
|
println!("{:?}", req);
|
|
|
|
HttpResponse::Found()
|
2018-04-15 04:19:26 +02:00
|
|
|
.header(header::LOCATION, "static/index.html")
|
2018-04-13 03:18:42 +02:00
|
|
|
.finish()
|
|
|
|
}))
|
|
|
|
// default
|
|
|
|
.default_resource(|r| {
|
|
|
|
// 404 for GET request
|
|
|
|
r.method(Method::GET).f(p404);
|
|
|
|
|
|
|
|
// all requests that are not `GET`
|
|
|
|
r.route().filter(pred::Not(pred::Get())).f(
|
|
|
|
|req| HttpResponse::MethodNotAllowed());
|
|
|
|
}))
|
|
|
|
|
|
|
|
.bind("127.0.0.1:8080").expect("Can not bind to 127.0.0.1:8080")
|
|
|
|
.shutdown_timeout(0) // <- Set shutdown timeout to 0 seconds (default 60s)
|
|
|
|
.start();
|
|
|
|
|
|
|
|
println!("Starting http server: 127.0.0.1:8080");
|
|
|
|
let _ = sys.run();
|
|
|
|
}
|