Rust, Software Development

Rust REST API, Actix-web, PostgreSQL – Part 2: The Project

Previously, we had a bit of an overview and prerequisites of the stuff needed to start creating a REST API example in Rust using Actix-web and PostgreSQL project. This post is the second part and gives an overview of the project structure.

Actix-Web, PostgreSQL, Rust Project Structure

Our Actix-web PostgreSQL project structure looks like the following with only three source code files.

File – main.rs

This file is the main file that has the main()  function. It uses the person module.

Module – person module

File – common.rs

This file contains the structs for our example.

File – mod.rs

This Rust module contains our essential CRUD operations functions against the PostgreSQL instance.

Actix-Web REST API URLs

The URIs (or URLs) we defined with Actix-web represent basic CRUD operations – Create, Retrieve, Update, and Delete.

Actix-Web and PostgreSQL (persistence layer) CRUD operations

We have the following endpoint to represent the create operation, which makes a new person in the database.

  • HTTP POST   /persons that accept a JSON with a single field person_name.

Next, we have the following endpoints to retrieve a specific person (by person ID) or all persons from our PostgreSQL database. These URLs retrieve a list of persons or a single person based on person_id.

  • HTTP GET   /persons
  • HTTP GET  /persons/{person_id}

To retrieve a specific person by person Id, we need to know the person’s Id beforehand. We can choose from the list of persons we retrieved from the database using the first endpoint.

Then, we have the update operation, which updates an existing person’s name. Similarly, to retrieve a specific person for updating it, we need to know its Id beforehand.

  • HTTP PUT   /persons/{person_id}  that accepts a JSON with person_name

Finally, we have the delete operation, which deletes an existing person.

  • HTTP DELETE  /persons/{person_id}

What’s Next

Next, we will talk about Rust codes that implement our Actix-web and PostgreSQL project.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like