2019-03-10 07:38:15 +01:00
|
|
|
use actix_web::{middleware, web, App, HttpRequest, HttpServer};
|
2018-04-13 03:18:42 +02:00
|
|
|
|
2019-03-10 07:38:15 +01:00
|
|
|
fn index(req: HttpRequest) -> &'static str {
|
|
|
|
println!("REQ: {:?}", req);
|
2018-04-13 03:18:42 +02:00
|
|
|
"Hello world!"
|
|
|
|
}
|
|
|
|
|
2019-03-10 07:38:15 +01:00
|
|
|
fn main() -> std::io::Result<()> {
|
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
2018-04-13 03:18:42 +02:00
|
|
|
env_logger::init();
|
|
|
|
|
2019-03-10 07:38:15 +01:00
|
|
|
HttpServer::new(|| {
|
2018-05-08 20:08:43 +02:00
|
|
|
App::new()
|
2018-04-13 03:18:42 +02:00
|
|
|
// enable logger
|
2019-03-26 04:29:00 +01:00
|
|
|
.wrap(middleware::Logger::default())
|
2019-03-10 07:38:15 +01:00
|
|
|
.service(web::resource("/index.html").to(|| "Hello world!"))
|
|
|
|
.service(web::resource("/").to(index))
|
2019-03-10 03:03:09 +01:00
|
|
|
})
|
2019-03-10 07:38:15 +01:00
|
|
|
.bind("127.0.0.1:8080")?
|
|
|
|
.run()
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
2019-11-13 18:20:24 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use actix_web::dev::Service;
|
|
|
|
use actix_web::{test, web, App, http, Error};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index() -> Result<(), Error> {
|
|
|
|
let app = App::new().route("/", web::get().to(index));
|
|
|
|
let mut app = test::init_service(app);
|
|
|
|
|
|
|
|
let req = test::TestRequest::get()
|
|
|
|
.uri("/")
|
|
|
|
.to_request();
|
|
|
|
let resp = test::block_on(app.call(req)).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(resp.status(), http::StatusCode::OK);
|
|
|
|
|
|
|
|
let response_body = match resp.response().body().as_ref() {
|
|
|
|
Some(actix_web::body::Body::Bytes(bytes)) => bytes,
|
|
|
|
_ => panic!("Response error"),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(response_body, r##"Hello world!"##);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|