mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
update web api
This commit is contained in:
@ -83,45 +83,42 @@ fn main() -> io::Result<()> {
|
||||
// cookie session middleware
|
||||
.middleware(CookieSession::signed(&[0; 32]).secure(false))
|
||||
// register favicon
|
||||
.resource("/favicon", |r| r.to(favicon))
|
||||
.service(web::resource("/favicon").to(favicon))
|
||||
// register simple route, handle all methods
|
||||
.resource("/welcome", |r| r.to(welcome))
|
||||
.service(web::resource("/welcome").to(welcome))
|
||||
// with path parameters
|
||||
.resource("/user/{name}", |r| r.route(web::get().to(with_param)))
|
||||
.service(web::resource("/user/{name}").route(web::get().to(with_param)))
|
||||
// async handler
|
||||
.resource("/async/{name}", |r| {
|
||||
r.route(web::get().to_async(index_async))
|
||||
})
|
||||
.service(
|
||||
web::resource("/async/{name}").route(web::get().to_async(index_async)),
|
||||
)
|
||||
// async handler
|
||||
.resource("/async-body/{name}", |r| {
|
||||
r.route(web::get().to(index_async_body))
|
||||
})
|
||||
.resource("/test", |r| {
|
||||
r.to(|req: HttpRequest| match *req.method() {
|
||||
.service(
|
||||
web::resource("/async-body/{name}")
|
||||
.route(web::get().to(index_async_body)),
|
||||
)
|
||||
.service(
|
||||
web::resource("/test").to(|req: HttpRequest| match *req.method() {
|
||||
Method::GET => HttpResponse::Ok(),
|
||||
Method::POST => HttpResponse::MethodNotAllowed(),
|
||||
_ => HttpResponse::NotFound(),
|
||||
})
|
||||
})
|
||||
.resource("/error", |r| {
|
||||
r.to(|| {
|
||||
error::InternalError::new(
|
||||
io::Error::new(io::ErrorKind::Other, "test"),
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
})
|
||||
})
|
||||
}),
|
||||
)
|
||||
.service(web::resource("/error").to(|| {
|
||||
error::InternalError::new(
|
||||
io::Error::new(io::ErrorKind::Other, "test"),
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
}))
|
||||
// static files
|
||||
.service(fs::StaticFiles::new("/static", "static").show_files_listing())
|
||||
// redirect
|
||||
.resource("/", |r| {
|
||||
r.route(web::get().to(|req: HttpRequest| {
|
||||
println!("{:?}", req);
|
||||
HttpResponse::Found()
|
||||
.header(header::LOCATION, "static/welcome.html")
|
||||
.finish()
|
||||
}))
|
||||
})
|
||||
.service(web::resource("/").route(web::get().to(|req: HttpRequest| {
|
||||
println!("{:?}", req);
|
||||
HttpResponse::Found()
|
||||
.header(header::LOCATION, "static/welcome.html")
|
||||
.finish()
|
||||
})))
|
||||
// default
|
||||
.default_resource(|r| {
|
||||
// 404 for GET request
|
||||
|
Reference in New Issue
Block a user