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-12-07 18:59:24 +01:00
|
|
|
async fn index(req: HttpRequest) -> &'static str {
|
2022-06-08 04:53:38 +02:00
|
|
|
println!("REQ: {req:?}");
|
2018-04-13 03:18:42 +02:00
|
|
|
"Hello world!"
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:49:45 +02:00
|
|
|
#[actix_web::main]
|
2019-12-07 18:59:24 +01:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2019-03-10 07:38:15 +01:00
|
|
|
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-12-07 18:59:24 +01:00
|
|
|
.service(web::resource("/index.html").to(|| async { "Hello world!" }))
|
2019-03-10 07:38:15 +01:00
|
|
|
.service(web::resource("/").to(index))
|
2019-03-10 03:03:09 +01:00
|
|
|
})
|
2022-02-17 21:22:36 +01:00
|
|
|
.bind(("127.0.0.1", 8080))?
|
2019-12-25 17:48:33 +01:00
|
|
|
.run()
|
2019-12-07 18:59:24 +01:00
|
|
|
.await
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
2019-11-13 18:20:24 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-01-30 17:17:18 +01:00
|
|
|
use actix_web::body::to_bytes;
|
2019-11-13 18:20:24 +01:00
|
|
|
use actix_web::dev::Service;
|
2019-12-07 18:59:24 +01:00
|
|
|
use actix_web::{http, test, web, App, Error};
|
2019-11-13 18:20:24 +01:00
|
|
|
|
2022-07-09 22:08:11 +02:00
|
|
|
use super::*;
|
|
|
|
|
2022-01-30 17:17:18 +01:00
|
|
|
#[actix_web::test]
|
2019-12-07 18:59:24 +01:00
|
|
|
async fn test_index() -> Result<(), Error> {
|
2019-11-13 18:20:24 +01:00
|
|
|
let app = App::new().route("/", web::get().to(index));
|
2022-01-30 17:17:18 +01:00
|
|
|
let app = test::init_service(app).await;
|
2019-11-13 18:20:24 +01:00
|
|
|
|
2019-12-07 18:59:24 +01:00
|
|
|
let req = test::TestRequest::get().uri("/").to_request();
|
2022-06-24 19:17:52 +02:00
|
|
|
let resp = app.call(req).await?;
|
2019-11-13 18:20:24 +01:00
|
|
|
|
|
|
|
assert_eq!(resp.status(), http::StatusCode::OK);
|
|
|
|
|
2022-01-30 17:17:18 +01:00
|
|
|
let response_body = resp.into_body();
|
2022-06-24 19:17:52 +02:00
|
|
|
assert_eq!(to_bytes(response_body).await?, r##"Hello world!"##);
|
2019-11-13 18:20:24 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|