2019-03-06 23:44:46 -08:00
|
|
|
use actix_files as fs;
|
2019-03-05 22:16:42 -08:00
|
|
|
use actix_session::{CookieSession, Session};
|
2019-12-07 23:59:24 +06:00
|
|
|
use actix_utils::mpsc;
|
2018-04-13 09:18:42 +08:00
|
|
|
use actix_web::http::{header, Method, StatusCode};
|
2018-05-20 21:03:29 -07:00
|
|
|
use actix_web::{
|
2020-09-12 16:49:45 +01:00
|
|
|
error, get, guard, middleware, web, App, Error, HttpRequest, HttpResponse,
|
|
|
|
HttpServer, Result,
|
2018-05-20 21:03:29 -07:00
|
|
|
};
|
2020-09-12 16:49:45 +01:00
|
|
|
use std::{env, io};
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
/// favicon handler
|
2019-03-07 14:50:29 -08:00
|
|
|
#[get("/favicon")]
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn favicon() -> Result<fs::NamedFile> {
|
2018-04-15 10:19:26 +08:00
|
|
|
Ok(fs::NamedFile::open("static/favicon.ico")?)
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// simple index handler
|
2019-03-07 14:50:29 -08:00
|
|
|
#[get("/welcome")]
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn welcome(session: Session, req: HttpRequest) -> Result<HttpResponse> {
|
2018-04-13 09:18:42 +08:00
|
|
|
println!("{:?}", req);
|
|
|
|
|
|
|
|
// session
|
|
|
|
let mut counter = 1;
|
2019-03-05 22:16:42 -08:00
|
|
|
if let Some(count) = session.get::<i32>("counter")? {
|
2018-04-13 09:18:42 +08:00
|
|
|
println!("SESSION value: {}", count);
|
|
|
|
counter = count + 1;
|
|
|
|
}
|
|
|
|
|
2018-09-28 13:35:13 +08:00
|
|
|
// set counter to session
|
2019-03-05 22:16:42 -08:00
|
|
|
session.set("counter", counter)?;
|
2018-09-28 13:35:13 +08:00
|
|
|
|
2018-04-13 09:18:42 +08:00
|
|
|
// response
|
|
|
|
Ok(HttpResponse::build(StatusCode::OK)
|
2018-05-08 11:08:43 -07:00
|
|
|
.content_type("text/html; charset=utf-8")
|
|
|
|
.body(include_str!("../static/welcome.html")))
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 404 handler
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn p404() -> Result<fs::NamedFile> {
|
2018-05-08 11:08:43 -07:00
|
|
|
Ok(fs::NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND))
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
/// response body
|
|
|
|
async fn response_body(path: web::Path<String>) -> HttpResponse {
|
2018-06-13 05:21:41 -07:00
|
|
|
let text = format!("Hello {}!", *path);
|
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
let (tx, rx_body) = mpsc::channel();
|
2020-09-12 16:49:45 +01:00
|
|
|
let _ = tx.send(Ok::<_, Error>(web::Bytes::from(text)));
|
2018-06-13 05:21:41 -07:00
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
HttpResponse::Ok().streaming(rx_body)
|
2018-06-13 05:21:41 -07:00
|
|
|
}
|
|
|
|
|
2018-04-13 09:18:42 +08:00
|
|
|
/// handler with path parameters like `/user/{name}/`
|
2020-09-12 16:49:45 +01:00
|
|
|
async fn with_param(
|
|
|
|
req: HttpRequest,
|
|
|
|
web::Path((name,)): web::Path<(String,)>,
|
|
|
|
) -> HttpResponse {
|
2018-04-13 09:18:42 +08:00
|
|
|
println!("{:?}", req);
|
|
|
|
|
|
|
|
HttpResponse::Ok()
|
2018-07-04 09:43:54 +02:00
|
|
|
.content_type("text/plain")
|
2020-09-12 16:49:45 +01:00
|
|
|
.body(format!("Hello {}!", name))
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:49:45 +01:00
|
|
|
#[actix_web::main]
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn main() -> io::Result<()> {
|
2019-12-27 02:39:57 +09:00
|
|
|
env::set_var("RUST_LOG", "actix_web=debug,actix_server=info");
|
2018-04-13 09:18:42 +08:00
|
|
|
env_logger::init();
|
|
|
|
|
2019-03-05 22:16:42 -08:00
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
2018-04-13 09:18:42 +08:00
|
|
|
// cookie session middleware
|
2019-03-26 04:29:00 +01:00
|
|
|
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
2019-07-11 15:02:25 +06:00
|
|
|
// enable logger - always register actix-web Logger middleware last
|
2019-06-01 09:24:54 -04:00
|
|
|
.wrap(middleware::Logger::default())
|
2018-04-13 09:18:42 +08:00
|
|
|
// register favicon
|
2019-03-07 14:50:29 -08:00
|
|
|
.service(favicon)
|
2018-04-13 09:18:42 +08:00
|
|
|
// register simple route, handle all methods
|
2019-03-07 14:50:29 -08:00
|
|
|
.service(welcome)
|
2018-04-13 09:18:42 +08:00
|
|
|
// with path parameters
|
2019-03-06 15:51:56 -08:00
|
|
|
.service(web::resource("/user/{name}").route(web::get().to(with_param)))
|
2019-12-07 23:59:24 +06:00
|
|
|
// async response body
|
2019-03-06 15:51:56 -08:00
|
|
|
.service(
|
2019-12-07 23:59:24 +06:00
|
|
|
web::resource("/async-body/{name}").route(web::get().to(response_body)),
|
2019-03-06 15:51:56 -08:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/test").to(|req: HttpRequest| match *req.method() {
|
2018-04-13 09:18:42 +08:00
|
|
|
Method::GET => HttpResponse::Ok(),
|
|
|
|
Method::POST => HttpResponse::MethodNotAllowed(),
|
|
|
|
_ => HttpResponse::NotFound(),
|
2019-03-06 15:51:56 -08:00
|
|
|
}),
|
|
|
|
)
|
2020-01-31 07:21:39 -05:00
|
|
|
.service(web::resource("/error").to(|| async {
|
|
|
|
error::InternalError::new(
|
|
|
|
io::Error::new(io::ErrorKind::Other, "test"),
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
)
|
2019-03-06 15:51:56 -08:00
|
|
|
}))
|
2018-04-13 09:18:42 +08:00
|
|
|
// static files
|
2019-03-06 23:44:46 -08:00
|
|
|
.service(fs::Files::new("/static", "static").show_files_listing())
|
2018-04-13 09:18:42 +08:00
|
|
|
// redirect
|
2019-03-06 15:51:56 -08:00
|
|
|
.service(web::resource("/").route(web::get().to(|req: HttpRequest| {
|
|
|
|
println!("{:?}", req);
|
|
|
|
HttpResponse::Found()
|
|
|
|
.header(header::LOCATION, "static/welcome.html")
|
|
|
|
.finish()
|
|
|
|
})))
|
2018-04-13 09:18:42 +08:00
|
|
|
// default
|
2019-04-14 10:34:41 -07:00
|
|
|
.default_service(
|
2018-04-13 09:18:42 +08:00
|
|
|
// 404 for GET request
|
2019-04-14 10:34:41 -07:00
|
|
|
web::resource("")
|
|
|
|
.route(web::get().to(p404))
|
2019-03-05 22:16:42 -08:00
|
|
|
// all requests that are not `GET`
|
|
|
|
.route(
|
|
|
|
web::route()
|
|
|
|
.guard(guard::Not(guard::Get()))
|
2019-09-05 00:04:57 +09:00
|
|
|
.to(HttpResponse::MethodNotAllowed),
|
2019-04-14 10:34:41 -07:00
|
|
|
),
|
|
|
|
)
|
2019-03-05 22:16:42 -08:00
|
|
|
})
|
|
|
|
.bind("127.0.0.1:8080")?
|
2019-12-25 20:48:33 +04:00
|
|
|
.run()
|
2019-12-07 23:59:24 +06:00
|
|
|
.await
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|