1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

update web api

This commit is contained in:
Nikolay Kim 2019-03-06 15:51:56 -08:00
parent 1edbb348d9
commit 51860a4f2a
3 changed files with 35 additions and 36 deletions

View File

@ -81,12 +81,14 @@ fn main() -> io::Result<()> {
.state(pool.clone()) .state(pool.clone())
// enable logger // enable logger
// .middleware(middleware::Logger::default()) // .middleware(middleware::Logger::default())
.resource("/asyncio_weather", |r| { .service(
r.route(web::get().to_async(asyncio_weather)) web::resource("/asyncio_weather")
}) .route(web::get().to_async(asyncio_weather)),
.resource("/parallel_weather", |r| { )
r.route(web::get().to_async(parallel_weather)) .service(
}) web::resource("/parallel_weather")
.route(web::get().to_async(parallel_weather)),
)
}) })
.bind("127.0.0.1:8080")? .bind("127.0.0.1:8080")?
.start(); .start();

View File

@ -83,45 +83,42 @@ fn main() -> io::Result<()> {
// cookie session middleware // cookie session middleware
.middleware(CookieSession::signed(&[0; 32]).secure(false)) .middleware(CookieSession::signed(&[0; 32]).secure(false))
// register favicon // register favicon
.resource("/favicon", |r| r.to(favicon)) .service(web::resource("/favicon").to(favicon))
// register simple route, handle all methods // register simple route, handle all methods
.resource("/welcome", |r| r.to(welcome)) .service(web::resource("/welcome").to(welcome))
// with path parameters // 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 // async handler
.resource("/async/{name}", |r| { .service(
r.route(web::get().to_async(index_async)) web::resource("/async/{name}").route(web::get().to_async(index_async)),
}) )
// async handler // async handler
.resource("/async-body/{name}", |r| { .service(
r.route(web::get().to(index_async_body)) web::resource("/async-body/{name}")
}) .route(web::get().to(index_async_body)),
.resource("/test", |r| { )
r.to(|req: HttpRequest| match *req.method() { .service(
web::resource("/test").to(|req: HttpRequest| match *req.method() {
Method::GET => HttpResponse::Ok(), Method::GET => HttpResponse::Ok(),
Method::POST => HttpResponse::MethodNotAllowed(), Method::POST => HttpResponse::MethodNotAllowed(),
_ => HttpResponse::NotFound(), _ => HttpResponse::NotFound(),
}) }),
}) )
.resource("/error", |r| { .service(web::resource("/error").to(|| {
r.to(|| { error::InternalError::new(
error::InternalError::new( io::Error::new(io::ErrorKind::Other, "test"),
io::Error::new(io::ErrorKind::Other, "test"), StatusCode::INTERNAL_SERVER_ERROR,
StatusCode::INTERNAL_SERVER_ERROR, )
) }))
})
})
// static files // static files
.service(fs::StaticFiles::new("/static", "static").show_files_listing()) .service(fs::StaticFiles::new("/static", "static").show_files_listing())
// redirect // redirect
.resource("/", |r| { .service(web::resource("/").route(web::get().to(|req: HttpRequest| {
r.route(web::get().to(|req: HttpRequest| { println!("{:?}", req);
println!("{:?}", req); HttpResponse::Found()
HttpResponse::Found() .header(header::LOCATION, "static/welcome.html")
.header(header::LOCATION, "static/welcome.html") .finish()
.finish() })))
}))
})
// default // default
.default_resource(|r| { .default_resource(|r| {
// 404 for GET request // 404 for GET request

View File

@ -48,7 +48,7 @@ fn main() -> io::Result<()> {
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.state(pool.clone()) // <- store db pool in app state .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")? .bind("127.0.0.1:8080")?
.start(); .start();