2019-03-06 07:16:42 +01:00
|
|
|
use std::{env, io};
|
2018-04-13 03:18:42 +02:00
|
|
|
|
2019-03-07 08:44:46 +01:00
|
|
|
use actix_files as fs;
|
2019-03-06 07:16:42 +01:00
|
|
|
use actix_session::{CookieSession, Session};
|
|
|
|
use actix_web::extract::Path;
|
2018-04-13 03:18:42 +02:00
|
|
|
use actix_web::http::{header, Method, StatusCode};
|
2018-05-21 06:03:29 +02:00
|
|
|
use actix_web::{
|
2019-03-07 04:45:45 +01:00
|
|
|
error, guard, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
|
|
|
|
Result,
|
2018-05-21 06:03:29 +02:00
|
|
|
};
|
2019-03-06 07:16:42 +01:00
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::unsync::mpsc;
|
|
|
|
use futures::{future::ok, Future, Stream};
|
2018-04-13 03:18:42 +02:00
|
|
|
|
|
|
|
/// favicon handler
|
2019-03-06 07:16:42 +01:00
|
|
|
fn favicon() -> 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
|
2019-03-06 07:16:42 +01:00
|
|
|
fn welcome(session: Session, req: HttpRequest) -> Result<HttpResponse> {
|
2018-04-13 03:18:42 +02:00
|
|
|
println!("{:?}", req);
|
|
|
|
|
|
|
|
// session
|
|
|
|
let mut counter = 1;
|
2019-03-06 07:16:42 +01:00
|
|
|
if let Some(count) = session.get::<i32>("counter")? {
|
2018-04-13 03:18:42 +02:00
|
|
|
println!("SESSION value: {}", count);
|
|
|
|
counter = count + 1;
|
|
|
|
}
|
|
|
|
|
2018-09-28 07:35:13 +02:00
|
|
|
// set counter to session
|
2019-03-06 07:16:42 +01:00
|
|
|
session.set("counter", counter)?;
|
2018-09-28 07:35:13 +02:00
|
|
|
|
2018-04-13 03:18:42 +02:00
|
|
|
// 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
|
2019-03-06 07:16:42 +01:00
|
|
|
fn p404() -> 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
|
2019-03-06 07:16:42 +01:00
|
|
|
fn index_async(req: HttpRequest) -> impl Future<Item = HttpResponse, Error = Error> {
|
2018-04-13 03:18:42 +02:00
|
|
|
println!("{:?}", req);
|
|
|
|
|
2019-03-06 07:16:42 +01:00
|
|
|
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()
|
2019-03-06 07:16:42 +01:00
|
|
|
.streaming(rx_body.map_err(|_| error::ErrorBadRequest("bad request")))
|
2018-06-13 14:21:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 03:18:42 +02:00
|
|
|
/// handler with path parameters like `/user/{name}/`
|
2019-03-06 07:16:42 +01:00
|
|
|
fn with_param(req: HttpRequest, path: Path<(String,)>) -> HttpResponse {
|
2018-04-13 03:18:42 +02:00
|
|
|
println!("{:?}", req);
|
|
|
|
|
|
|
|
HttpResponse::Ok()
|
2018-07-04 09:43:54 +02:00
|
|
|
.content_type("text/plain")
|
2019-03-06 07:16:42 +01:00
|
|
|
.body(format!("Hello {}!", path.0))
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
|
2019-03-06 07:16:42 +01:00
|
|
|
fn main() -> io::Result<()> {
|
2018-04-13 03:18:42 +02:00
|
|
|
env::set_var("RUST_LOG", "actix_web=debug");
|
|
|
|
env::set_var("RUST_BACKTRACE", "1");
|
|
|
|
env_logger::init();
|
2019-03-06 07:16:42 +01:00
|
|
|
let sys = actix_rt::System::new("basic-example");
|
2018-04-13 03:18:42 +02:00
|
|
|
|
2019-03-06 07:16:42 +01:00
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
2018-04-13 03:18:42 +02:00
|
|
|
// enable logger
|
2019-03-07 04:45:45 +01:00
|
|
|
.middleware(middleware::Logger::default())
|
2018-04-13 03:18:42 +02:00
|
|
|
// cookie session middleware
|
2019-03-06 07:16:42 +01:00
|
|
|
.middleware(CookieSession::signed(&[0; 32]).secure(false))
|
2018-04-13 03:18:42 +02:00
|
|
|
// register favicon
|
2019-03-07 00:51:56 +01:00
|
|
|
.service(web::resource("/favicon").to(favicon))
|
2018-04-13 03:18:42 +02:00
|
|
|
// register simple route, handle all methods
|
2019-03-07 00:51:56 +01:00
|
|
|
.service(web::resource("/welcome").to(welcome))
|
2018-04-13 03:18:42 +02:00
|
|
|
// with path parameters
|
2019-03-07 00:51:56 +01:00
|
|
|
.service(web::resource("/user/{name}").route(web::get().to(with_param)))
|
2018-04-13 03:18:42 +02:00
|
|
|
// async handler
|
2019-03-07 00:51:56 +01:00
|
|
|
.service(
|
|
|
|
web::resource("/async/{name}").route(web::get().to_async(index_async)),
|
|
|
|
)
|
2018-06-13 14:21:41 +02:00
|
|
|
// async handler
|
2019-03-07 00:51:56 +01:00
|
|
|
.service(
|
|
|
|
web::resource("/async-body/{name}")
|
|
|
|
.route(web::get().to(index_async_body)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/test").to(|req: HttpRequest| match *req.method() {
|
2018-04-13 03:18:42 +02:00
|
|
|
Method::GET => HttpResponse::Ok(),
|
|
|
|
Method::POST => HttpResponse::MethodNotAllowed(),
|
|
|
|
_ => HttpResponse::NotFound(),
|
2019-03-07 00:51:56 +01:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
.service(web::resource("/error").to(|| {
|
|
|
|
error::InternalError::new(
|
|
|
|
io::Error::new(io::ErrorKind::Other, "test"),
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
)
|
|
|
|
}))
|
2018-04-13 03:18:42 +02:00
|
|
|
// static files
|
2019-03-07 08:44:46 +01:00
|
|
|
.service(fs::Files::new("/static", "static").show_files_listing())
|
2018-04-13 03:18:42 +02:00
|
|
|
// redirect
|
2019-03-07 00:51:56 +01:00
|
|
|
.service(web::resource("/").route(web::get().to(|req: HttpRequest| {
|
|
|
|
println!("{:?}", req);
|
|
|
|
HttpResponse::Found()
|
|
|
|
.header(header::LOCATION, "static/welcome.html")
|
|
|
|
.finish()
|
|
|
|
})))
|
2018-04-13 03:18:42 +02:00
|
|
|
// default
|
|
|
|
.default_resource(|r| {
|
|
|
|
// 404 for GET request
|
2019-03-06 07:16:42 +01:00
|
|
|
r.route(web::get().to(p404))
|
|
|
|
// all requests that are not `GET`
|
|
|
|
.route(
|
|
|
|
web::route()
|
|
|
|
.guard(guard::Not(guard::Get()))
|
|
|
|
.to(|| HttpResponse::MethodNotAllowed()),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.bind("127.0.0.1:8080")?
|
|
|
|
.start();
|
2018-04-13 03:18:42 +02:00
|
|
|
|
|
|
|
println!("Starting http server: 127.0.0.1:8080");
|
2019-03-06 19:29:01 +01:00
|
|
|
sys.run()
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|