Rust Web Coursework
This repository has been archived on 2025-04-28. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
2024-06-02 18:19:39 -07:00
.cargo checkpoint for postgres. 70% there 2024-05-27 18:14:12 -07:00
migrations fix add/fetch all questions 2024-06-02 18:08:22 -07:00
src fix delete question 2024-06-02 18:19:39 -07:00
.gitignore cargo init/ hello world 2024-04-10 16:54:54 -07:00
answers.json add full support for POST /answer 2024-04-27 13:52:26 -07:00
Cargo.lock add DB env 2024-05-07 14:24:28 -07:00
Cargo.toml add DB env 2024-05-07 14:24:28 -07:00
LICENSE.txt add license 2024-05-02 14:50:05 -07:00
questions.json support pagination 2024-04-27 12:20:45 -07:00
README.md begin on associate/unassociate tags function 2024-05-31 15:15:49 -07:00

David Westgate

Rust Axum Question and Answer Server

Background

This is a simple Rust Web Axum server, so far serving a handful of REST API endpoints as the basis of a "Q&A" or question and answer application. The APIs supported offer basic CRUD functionality. Specifically, this example mirrors that which is found in the early chapters of the Rust Web Development textbook by Bastian Gruber (except using Axum and not Warp)

Contents

I'll do my best to keep these contents up-to-date as changes are made to the source code

  • Setup
  • Dependency overview
  • Source overview
  • Looking ahead

Setup

Rustup

First install Rust. I suggest using the rustup toolchain

Database

A postgres database is required. At this time, the database must be setup and run manually.

It is convient to run postgres in docker with the postgres:latest docker image. This can be done in a few easy clicks on docker desktop, or as follows with the docker CLI (be sure to pick a password and update .cargo/config.toml)

docker pull postgres:latest
docker run --name my-postgres-container -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=mypassword -e -p 5432:5432 -d postgres:latest

Build and Run

cargo run

Dependency overview

std

We utilize std library crates for file input/output, hash maps, and network sockets

Axum

Axum is the rust web framework doing much of the heavy lifting of this application. It is used here for its core functionality of routing, and abstracting http concepts like methods, request/response handling, and status codes.

Tokio

The tokio framework allows rust to operate asynchoronously. By axums asychonous web nature, it is required by axum.

Serde/serde_json

A useful package for serializing and deserializing data.

sqlx

Manages connections and queries to our postgres database

Source overview

src/main.rs

Our applications main entry point, here we declare the use of the applications external packages for easier management. main is then responsible for creating the new question store (behind the necessary tokio RwLock). main creates the axum router, and specifies all of the routes our application supports by pointing them to handlers in the src/api.rs file via the appropriate http methods, before serving the axum service

src/api.rs

Five handlers are defined here for our five API endpoints, supporting the basic CRUD operations regarding questions. Care was taken to handle most common possible error cases, such as attempting to get a question by an id which does not exist, or attempting to create a question with the same ID as one that already exists.

Some minor derivations were taken here from the exact routes specified in the text

  • GET /question/:id is included to return a specific question by id
  • GET /questions is programmed to use pagination
  • PUT /question does not include the question ID in its path param, but rather just the body
  • DELETE /question/:id returns in its body the deleted content

src/question.rs

This is the definition of our struct Question. An interesting potential for confilct arose here, as our question struct is supposed to keep track of the question id in addition of the in-memory hashmap of our store (next section). This two-sources-of-truth situation would lead to various error cases that we would need to handle, and would cause concerns with scalability.

To address this, I seperated the Question 'entity' from the Question 'DTO' (data transfer object) from an ORM philosophy. The result is that the Question entity does not contain a direct reference to the question ID, as it is assumed while we are in program space, the store's hashmap will manage this. to_entity and to_dto function implementions have been provided to make this easier to work with.

src/store.rs

The store is responsible for the management of the questions. It does this by

  • Providing public functions to create, retrieve, update or delete questions
  • Handling possible unpermitted operations by returning errors
  • Handling possible file or I/O errors with some sense of grace before panicing

question_tag.rs

tag.rs

answer.rs

Looking ahead

These are a few things still to be added

Higher priority

  • Add some simple API curl examples to this README
  • API documentation tooling (utoipa)
  • API testing tooling (swagger)
  • Coded API tests with mock data
  • Specific defined Error types for common errors

Lesser priority

  • Serve basic web page(s) to utilize all APIs
  • Optimize the put/update handler to support path or body identification, and to update only individual fields passed.
  • Host application on my raspberry pi on the internet