0

REST API with Rust, Actix-web, and PostgreSQL – Part 2

Previously, we had a bit of overview of the stuff needed to start creating a REST API example. This post is the second part post.

Rust Project Structure

Our project structure looks something like this.

main.rs

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

person module

common.rs

This contains the struct s for our example.

mod.rs

This contains functions for our basic CRUD operations against an instance of PostgreSQL.

REST API URLs

Create

This URL creates a new person in the database.

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

Retrieve – Get All and Get by Person ID

These URLs retrieves a list of persons or a single person based on person_id .

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

Update

This URL updates an existing person’s name.

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

Delete

This URL deletes an existing person.

  • HTTP DELETE  /persons/{person_id}

Karl San Gabriel

Karl San Gabriel

Professional Software Developer