added proc-macros for route registration

This commit is contained in:
Nikolay Kim 2019-03-07 11:09:42 -08:00
parent 1151b5bf7c
commit 22708e78a9
9 changed files with 221 additions and 42 deletions

View file

@ -1,9 +1,9 @@
use futures::IntoFuture;
use actix_web::{
http::Method, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
};
use actix_web::macros::get;
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
#[get("/resource1/index.html")]
fn index(req: HttpRequest) -> &'static str {
println!("REQ: {:?}", req);
"Hello world!\r\n"
@ -14,6 +14,7 @@ fn index_async(req: HttpRequest) -> impl IntoFuture<Item = &'static str, Error =
Ok("Hello world!\r\n")
}
#[get("/")]
fn no_params() -> &'static str {
"Hello world!\r\n"
}
@ -27,7 +28,8 @@ fn main() -> std::io::Result<()> {
App::new()
.middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
.middleware(middleware::Compress::default())
.service(web::resource("/resource1/index.html").route(web::get().to(index)))
.service(index)
.service(no_params)
.service(
web::resource("/resource2/index.html")
.middleware(
@ -36,10 +38,9 @@ fn main() -> std::io::Result<()> {
.default_resource(|r| {
r.route(web::route().to(|| HttpResponse::MethodNotAllowed()))
})
.route(web::method(Method::GET).to_async(index_async)),
.route(web::get().to_async(index_async)),
)
.service(web::resource("/test1.html").to(|| "Test\r\n"))
.service(web::resource("/").to(no_params))
})
.bind("127.0.0.1:8080")?
.workers(1)