mirror of
https://github.com/actix/examples
synced 2024-11-23 14:31:07 +01:00
update web api
This commit is contained in:
parent
1edbb348d9
commit
51860a4f2a
@ -81,12 +81,14 @@ fn main() -> io::Result<()> {
|
||||
.state(pool.clone())
|
||||
// enable logger
|
||||
// .middleware(middleware::Logger::default())
|
||||
.resource("/asyncio_weather", |r| {
|
||||
r.route(web::get().to_async(asyncio_weather))
|
||||
})
|
||||
.resource("/parallel_weather", |r| {
|
||||
r.route(web::get().to_async(parallel_weather))
|
||||
})
|
||||
.service(
|
||||
web::resource("/asyncio_weather")
|
||||
.route(web::get().to_async(asyncio_weather)),
|
||||
)
|
||||
.service(
|
||||
web::resource("/parallel_weather")
|
||||
.route(web::get().to_async(parallel_weather)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.start();
|
||||
|
@ -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
|
||||
|
@ -48,7 +48,7 @@ fn main() -> io::Result<()> {
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.state(pool.clone()) // <- store db pool in app state
|
||||
.resource("/{name}", |r| r.route(web::get().to_async(index)))
|
||||
.route("/{name}", web::get().to_async(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.start();
|
||||
|
Loading…
Reference in New Issue
Block a user