DEV Community

Practical Rust Web Development - API Rest

Werner Echezuría on May 14, 2019

Update: According to this issue async does not work with Diesel, so, the method to_async from web::get might not work as expected, it will work but...
Collapse
 
deciduously profile image
Ben Lovy

This is a really awesome write-up, thank you!

Have you attempted to port an application from actix_web 0.7 to 1.0 yet? If so, how did it go? There are some significant API changes so I haven't tried yet.

Collapse
 
werner profile image
Werner Echezuría

Hi, thanks.

In the meantime I'm learning about the migration from 0.7 to 1.0 and I'm trying to document everything I can. Something I've realized it's that is easier to create handlers and assigning it to a Server.

Collapse
 
saroar profile image
Saroar Khandoker

you don't have models.rs? in ur project so it confuse for newcomer :) prnt.sc/p0bfqb

Collapse
 
werner profile image
Werner Echezuría

Oops!, thanks for pointing this out, fixed now.

Collapse
 
saroar profile image
Saroar Khandoker

thanks also like to know so you know about the swift back end? I am a little bit confuse is Rust is ready for the production type of API app for a mobile app? i have my own API written in swift that's working good but i am also interested in RUST so i start to reading you post :)

Thread Thread
 
werner profile image
Werner Echezuría

I think as an API backend Rust is ready for production, might need to polish some areas but you could have your app deployed with confident. for example: figma.com/blog/rust-in-production-...

Thread Thread
 
saroar profile image
Saroar Khandoker

do you know swift back end like vapor ? if yes what you think about swift back end?
thanks

Thread Thread
 
werner profile image
Werner Echezuría

To be honest, I don't know swift, so I can't make an informed opinion about it.

Thread Thread
 
saroar profile image
Saroar Khandoker

ok no worry thanks for reply also check this prnt.sc/p0et3l image you don't have still extern crate futures; and you add it so its give us error too

  --> src/main.rs:16:1
   |
16 | extern crate futures;
   | ^^^^^^^^^^^^^^^^^^^^^ can't find crate

Thread Thread
 
saroar profile image
Saroar Khandoker

also, i follow your tutorial till here Finally goes to http://localhost:8088/products.
but and trying to understand why it not showing :( my products list already debugging more then 2 hours :(

Thread Thread
 
saroar profile image
Saroar Khandoker

here is my code please check me if you will have
bitbucket.org/Alifdev/ruststore

Thread Thread
 
werner profile image
Werner Echezuría

Hi, sorry for the late response, I was on vacations. Try to follow these tips and tell me if it works.

  1. git checkout v1.1.
  2. diesel setup.
  3. cargo run.
  4. curl http://127.0.0.1:8088/products -H "Content-Type: application/json" -d '{"name": "socks", "stock": 7, "price": 2}
  5. curl http://localhost:8088/products

Tell me if it works.

Thread Thread
 
saroar profile image
Saroar Khandoker

yes it is working thanks :)

Collapse
 
jtr109 profile image
Ryan Li • Edited

Hi, your tutorial is awesome!

But I find a little issue of diesel that the line

use crate::schema::products::dsl::*;

in ProductList implementation cannot be placed in the top which will cause an unmatched type error of id in find method of Product implementation.

This issue caused by a duplicated id from crate::schema::products::dsl.

Collapse
 
werner profile image
Werner Echezuría

Hi, did you take a look at the last version, sometimes I fix these issues but I forgot to mention them, github.com/practical-rust-web-deve...

Collapse
 
jtr109 profile image
Ryan Li

Thanks for your reply. Your source code can works well.

It is my bad to take the line above to the top of the module. 😄

Collapse
 
meghashyam6 profile image
meghashyam6

Thats a neat tutorial! Would like to see how we can use tokio in conjunction with actix. For eg. making multiple async calls(futures) to db, wait for all the futures, aggregate the resultset and respond to the request. I couldn't find any examples like that anywhere

Collapse
 
autoferrit profile image
Shawn McElroy

I am still going through these and am new to Rust. But being used to frameworks like flask in python, is it easy or possible to abstract out the model methods to not have the boilerplate for the db connection?

some frameworks make it so you can do request.db or maybe move it to a parent model or something? that way the model methods are a lot cleaner?

Collapse
 
werner profile image
Werner Echezuría

Well, I think it's possible, passing the db connection through a Trait, and then implements the trait for every model. Seems like a fun thing to do, maybe in a future post I'll try to implement it.

Collapse
 
autoferrit profile image
Shawn McElroy

that would be great.

Collapse
 
saroar profile image
Saroar Khandoker
impl ProductList {
    pub fn list() -> Self {
        ProductList(result)
    }
}```

 from 

to


```Rust
impl Product {
    pub fn list() -> Self {
        ProductList(result)
    }
}```



then use `Product::list()` look better and don't repeat list two times 
Collapse
 
aistaqiemsy profile image
aistaqiemsy

Big thanks bro....
I'm new at rust and after i read this article my mind has blow.... hha

Collapse
 
werner profile image
Werner Echezuría

No problem, I'm glad I could help.