1
0
mirror of https://github.com/actix/examples synced 2025-06-27 01:27:43 +02:00
This commit is contained in:
Rob Ede
2022-02-18 02:44:02 +00:00
parent aca1dab890
commit fbd3b228e9
48 changed files with 103 additions and 261 deletions

View File

@ -9,8 +9,7 @@ use actix_web::{
header::{self, ContentType},
Method, StatusCode,
},
middleware, web, App, Either, HttpRequest, HttpResponse, HttpServer, Responder,
Result,
middleware, web, App, Either, HttpRequest, HttpResponse, HttpServer, Responder, Result,
};
use async_stream::stream;
@ -44,8 +43,7 @@ async fn welcome(req: HttpRequest, session: Session) -> Result<HttpResponse> {
async fn default_handler(req_method: Method) -> Result<impl Responder> {
match req_method {
Method::GET => {
let file = NamedFile::open("static/404.html")?
.set_status_code(StatusCode::NOT_FOUND);
let file = NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND);
Ok(Either::Left(file))
}
_ => Ok(Either::Right(HttpResponse::MethodNotAllowed().finish())),
@ -93,9 +91,7 @@ async fn main() -> io::Result<()> {
// with path parameters
.service(web::resource("/user/{name}").route(web::get().to(with_param)))
// async response body
.service(
web::resource("/async-body/{name}").route(web::get().to(response_body)),
)
.service(web::resource("/async-body/{name}").route(web::get().to(response_body)))
.service(
web::resource("/test").to(|req: HttpRequest| match *req.method() {
Method::GET => HttpResponse::Ok(),
@ -112,14 +108,14 @@ async fn main() -> io::Result<()> {
// static files
.service(Files::new("/static", "static").show_files_listing())
// redirect
.service(web::resource("/").route(web::get().to(
|req: HttpRequest| async move {
.service(
web::resource("/").route(web::get().to(|req: HttpRequest| async move {
println!("{:?}", req);
HttpResponse::Found()
.insert_header((header::LOCATION, "static/welcome.html"))
.finish()
},
)))
})),
)
// default
.default_service(web::to(default_handler))
})