Rust, Software Development

Rust REST API, Actix-web, PostgreSQL – Part 3: The Codes

This is part 3 of our Rust REST API Actix-web PostgreSQL post with sample codes that does CRUD operations on a database from Actix-web. We referred to these codes in the previous post.

Rust Main Function – main.rs

The main.rs  file has the following main()  function codes that define endpoints in Actix-web.

Each service has a unique resource that has routes. For instance, the resource /persons/{person_id}  allows for HTTP GET , HTTP DELETE , and HTTP PUT  methods. Also, each of these methods triggers a handler function.

Handler Functions For Rust REST API (Web Layer)

These are “web controller”-level functions with codes that delegate calls to our “persistent layer” (person module).

Function – get_person_list

First, we have the get_person_list function, which retrieves all the person records from the database through the person module.

Function – get_person

Second, we have the get_person function. This function retrieves a particular person record from the database by person_id.

Function – create_person

Next, we have the create_person function. This function creates a person record in the database.

Function – update_person

Next, we have a function to update a person’s name in the database.

Function – delete_person

Lastly, we have a function to delete a person record in the database by person_id.

Person Module – Struct and DB codes for Actix-Web and PostgreSQL

Common Structs both for Actix-Web and PostgreSQL codes – common.rs

This file contains the structs for our Rust codes. Note that we use the same structs in the controller layer in main.rs.

The struct Person also represents a person record in the database. Meanwhile, the others automatically map the JSONs from incoming requests to their instances.

Persistence Module for Actix-web / PostgreSQL – mod.rs

The first three lines make the common mod available and use the extern and use statements for postgres crate.

Here, we store the connection string for PostgreSQL in a global static variable.

The rest of the codes are functions that directly access the database.

Function – get_person_all

First, we have the get_person_all function. This function retrieves all person records from the database and puts them in a Vec<common::Person> instance.

Function – get_person_by_id

Second, we have a function that retrieves a person’s record by person_id. The function pus the record in a Vec<common::Person> instance.

Function – create_person

This function creates a person record in the database.

Function – delete_person

This function deletes a person record in the database by person_id.

Function – update_person

This function updates a person’s name in the database by person_id.

That’s all for this Rust REST API Actix-web PostgreSQL and its codes.

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