DEV Community

Cover image for Rust 🦀 CRUD Rest API with Docker 🐳

Rust 🦀 CRUD Rest API with Docker 🐳

Francesco Ciulla on May 04, 2023

Let's create a CRUD Rest API in Rust using: No specific framework Serde to serialize and deserialize JSON Postgres (database) Docker Docker Compo...
Collapse
 
liyasthomas profile image
Liyas Thomas •

We are building an open source {free} Postman alternative: Hoppscotch - API request builder for web.

If you'd like to try an online API development / testing platform - we're building Hoppscotch.io.

GitHub logo hoppscotch / hoppscotch

👽 Open source API development ecosystem - https://hoppscotch.io

Collapse
 
francescoxx profile image
Francesco Ciulla •

where cna I find more about Hoppscotch?

Collapse
 
liyasthomas profile image
Liyas Thomas •
Thread Thread
 
francescoxx profile image
Francesco Ciulla •

thank you. are you looking for collaborations?

Collapse
 
francescoxx profile image
Francesco Ciulla •

I know the project

Collapse
 
teethew profile image
Thiago Crispim •

Great article, it's really good to see how simple it can be to work with Rust even for some applications that we would consider of higher level, like a REST API. I'd just like to point out that as I think this article may be suitable to beginners, you could've mentioned that you didn't follow best practices for REST APIs (like responses in plain text) for the sake of simplicity. Nonetheless, keep up the content!

Collapse
 
francescoxx profile image
Francesco Ciulla •

hey Thiago, thanks for taking time to give feedback. yes the focus here is the connection between the rust app and the db, I didn't see many resources about this so I created one. in the future I might do something more focused on best practices, here I am focused on simplicity

Collapse
 
francescoxx profile image
Francesco Ciulla •

are you currently building something with Rust?

Collapse
 
teethew profile image
Thiago Crispim •

I am building an open source authorization server. First I'm doing it with Typescript (Node) to understand what are the main problems building these. After that I'm going to rewrite it entirely in Rust.

Thread Thread
 
francescoxx profile image
Francesco Ciulla •

I also have an article/video about typescript, if you are interested. IT's the same concept so you might find it useful.

Collapse
 
lucasgazzola profile image
Lucas Gazzola •

Hi! I think the post is great! It's very interesting, and I may try to build it in a near future. However, I have a question. Could someone inject sql code with the request? Sorry for asking, I'm new in this world, and I love security

Collapse
 
francescoxx profile image
Francesco Ciulla •

reply by ChatGPT4

This code does not appear to be vulnerable to SQL injection as it uses prepared statements, which are a security feature that helps prevent SQL injection attacks.

For example, in the handle_post_request function, the SQL query is prepared using placeholders and then executed with the user's inputs as parameters. The execute method of the Postgres client takes care of properly escaping the user inputs and preventing SQL injection attacks. Similarly, all other SQL queries in the code use prepared statements with placeholders.

Overall, it is good practice to use prepared statements or parameterized queries to prevent SQL injection attacks.

Collapse
 
lucasgazzola profile image
Lucas Gazzola •

Thanks a lot. I really appreciate your explanation! I'm new, I started programming with the frontend pack we all know. Although, I find backend, database and talking to the os fascinating, specially Rust.

Thread Thread
 
francescoxx profile image
Francesco Ciulla •

you are welcome! did you already coded something in Rust?

Collapse
 
francescoxx profile image
Francesco Ciulla •

so we should be safe

Collapse
 
kwhandy profile image
Handy Wardhana •

from this article, i found rust very challenging just like approaching a girl

if she single, you're fuckdup
if she had bf, you also fuckdup

Collapse
 
francescoxx profile image
Francesco Ciulla •

this comment is gold ahah! I think it's because I used no frameworks but I really like Rust

Collapse
 
ikehakinyemi profile image
17th_streetCode •

And I truly appreciate you for not using a framework. Not that I don’t appreciate frameworks, but most articles out there are always based on frameworks. But learning without frameworks (most times) provides comprehensive knowledge.

Thread Thread
 
francescoxx profile image
Francesco Ciulla •

you are welcome

Collapse
 
coffeeintocode profile image
LucasZapico •

I came for threads like this. Learning Rust is a challenge for future me. But I can enjoy the memes and I’ll keep a pulse on the sentiment. I’m honestly extremely intimidated, sticking with GoLang for now.

Thread Thread
 
francescoxx profile image
Francesco Ciulla •

For me it's just different but it has some great concepts I like. Why do you find it intimidating?

Collapse
 
ghadzhigeorgiev profile image
Georgi Hadzhigeorgiev •

Articles like this inspire me to look and dig deeper in Rust, thanks!

Collapse
 
francescoxx profile image
Francesco Ciulla •

thank you so much! are you currently building somethign with Rust?

Collapse
 
ghadzhigeorgiev profile image
Georgi Hadzhigeorgiev •

Nothing specific yet, just spending some time to learn it and discover language capabilities.

Thread Thread
 
francescoxx profile image
Francesco Ciulla •

oh good luck then!

Collapse
 
robsongrangeiro profile image
Robson Grangeiro •

It's the perfect post! Clean, precise, have some details... Very nice work. Thanks to sharing.

Collapse
 
francescoxx profile image
Francesco Ciulla •

you are welcome Robson

Collapse
 
madzimai profile image
Netsai •

Great article,it's so helpful.

Collapse
 
francescoxx profile image
Francesco Ciulla •

you are welome

Collapse
 
ibrahmad18_28 profile image
ibrahmad18 •

check this part : All the code is available on GitHub (link in the video description)., we have added a Youtube video not Gihtub link . Else Thank you very good post .

Collapse
 
francescoxx profile image
Francesco Ciulla •

yes, the link is generally available on the youtube description

Collapse
 
shehab_badawy profile image
shehab-badawy •

Great tutorial, i dived in without knowledge of rust and i think i got most of it. It would be wonderful if you could make something like that about implementing authentication. Thank you

Collapse
 
francescoxx profile image
Francesco Ciulla •

you are welcome! We'll see!

Collapse
 
snaptikkim profile image
tomas raul •

very usefull project

Collapse
 
francescoxx profile image
Francesco Ciulla •

you are welcome Tomas. do you know Rust?

Collapse
 
qiaopengjun profile image
Qiao Pengjun 乔鹏军 •

why Error: error connecting to server: failed to lookup address information: nodename nor servname provided, or not known

Collapse
 
francescoxx profile image
Francesco Ciulla •

it might be relative to the configuration in the docker compose file. did you try to clone the repo and do a docker compose build, then docker compose up?

Collapse
 
rd2765 profile image
rd2765 • • Edited

Great work!, am getting an error on build:
error[E0308]: mismatched types
--> src\main.rs:19:22
|
19 | const DB_URL: &str = std::env::var("DATABASE_URL");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &str, found Result<String, VarError>
|
= note: expected reference &'static str
found enum Result<std::string::String, VarError>

Any ideas?

Collapse
 
francescoxx profile image
Francesco Ciulla •

try this line (with the exclamation mark)

const DB_URL: &str = env!("DATABASE_URL");

Collapse
 
balogunmaliko profile image
Balogun Malik O •

Hi, thank you for making this tutorial. however, there are a lot of things i don't understand. I'm following this steps but i get error a lot. For example, I do not get the trick behind settting the postgres db url . Can you kindly help out with that?
do i hve to spin up docker first?