Hacker News new | past | comments | ask | show | jobs | submit login

    use actix_web::{web, App, HttpServer, Responder};
    
    fn index(info: web::Path<(u32, String)>) -> impl Responder {
        format!("Hello {}! id:{}", info.1, info.0)
    }
    
    fn main() -> std::io::Result<()> {
        HttpServer::new(
            || App::new().service(
                  web::resource("/{id}/{name}/index.html").to(index)))
            .bind("127.0.0.1:8080")?
            .run()
    }

How do you know the HTTP method your path is called with?



that example isn't very good.. here's what the resource-registration for "/products" endpoint would look like (note the http method), where I've lazily omitted "/products" a level above because I wanted you just to see the http methods:

    web::resource("")
           .route(web::get().to_async(products::get_products))
           .route(web::post().to_async(products::add_product))


This syntax makes it look almost like [warp's](https://github.com/seanmonstar/warp) "filters".


What couchand said still applies to 1.0. So this should work:

    use actix_web::{web, App, HttpServer, HttpRequest, Responder};

   fn index(req: HttpRequest, info: web::Path<(u32, String)>) -> impl Responder {
       format!("[{}] Hello {}! id:{}", req.method(), info.1, info.0)
   }


You would route that resource to the appropriate handler: https://docs.rs/actix-web/1.0.0/actix_web/struct.Resource.ht...


You can add multiple routes for a resource.

    App.new().service(
        web::resource("/welcome")
            .route(web::get().to(welcome))
            .route(web::post().to(post_handler))


I haven't dug into the 1.0 API, but in the 0.7 line you specify an alternative handler signature that includes the HttpRequest object.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: