Rust HTTP API Template using PostgreSQL, Redis, RabbitMQ, and Hexagonal Architecture

Overview

Rust Template HTTP API

Rust API Template using PostgreSQL, Redis, RabbitMQ, and Hexagonal Architecture

The following template provides a basic structure for developing a Rust API, utilizing the powerful combination of PostgreSQL as a database, Redis as a caching system, and RabbitMQ for asynchronous communication. This template follows the principles of Hexagonal Architecture, also known as Ports and Adapters, which promotes loose coupling, separation of concerns, and modularity.

By adopting the Hexagonal Architecture, this template separates the core business logic from the external dependencies, enabling easier testing, maintainability, and flexibility. The API's core functionalities are encapsulated within the hexagon, while the adapters handle the integration with external services such as databases, caching systems, and message queues.

Note: Before using this template, ensure that you have installed and properly configured PostgreSQL, Redis, RabbitMQ, and the necessary dependencies.

Requirements

  • Rust (>=1.66.0)

Postgresql, redis and rabbitmq

Use docker-compose to start requirements resources

docker-compose up -d

Environments

Create a .env file with this default envs

Key Value
ADDR 0.0.0.0:5000
RUST_LOG debug
RUST_BACKTRACE 1
PAGE_SIZE_DEFAULT 12
PAGE_SIZE_MAX 120
DATABASE_USER postgres
DATABASE_PASSWORD postgres
DATABASE_NAME postgres
DATABASE_HOST localhost
DATABASE_POOL_MAX 16
AMQP_ADDR amqp://rabbitmq:[email protected]:5672
REDIS_URL redis://localhost/0

How to execute

cargo run

Documentation

Swagger documentation

http://127.0.0.1:5000/docs

Folder structure

amqp

In this step, the resources such as exchanges and queues of RabbitMQ are configured to start listening to events received in the queue. When a message is received, it is processed by the domain layer where the business rules of the application reside.

api

In this section, the HTTP access for the application is configured, where routes, middlewares, HTTP error handling, authentication, and other common features in HTTP APIs are defined.

domain

In the proposed architecture, all modules containing the application's business rules are grouped together to be used by adapters (API, AMQP). Within each module folder, there is a file that defines the repository implementation (Traits), a file that defines the data models, and a resources folder that contains files with module-specific business rules. This structure helps maintain a clear and modular organization, facilitating code comprehension and maintenance.

repository

In this folder, connections with the database, access to external services such as HTTP APIs using Reqwest, connections with Redis and RabbitMQ, among others, are defined. Additionally, the repositories required by the domain layer are implemented, following the trait defined in each module or submodule of the domain.

This allows for centralizing and organizing the configurations and integrations with the different services and external resources used by the application, ensuring a more modular, reusable, and easily maintainable code.

You might also like...
Starter template for use with the Leptos web framework and Axum.

Leptos Axum Starter Template This is a template for use with the Leptos web framework and the cargo-leptos tool using Axum. Creating your template rep

Sōzu HTTP reverse proxy, configurable at runtime, fast and safe, built in Rust. It is awesome! Ping us on gitter to know more

Sōzu · Sōzu is a lightweight, fast, always-up reverse proxy server. Why use Sōzu? Hot configurable: Sozu can receive configuration changes at runtime

Scratch-Containerised Rust GraphQL-API using Dataloaders

Dockerize Graphql Rust More current version at https://github.com/jayy-lmao/rust-cult-graphql-server This project is currently for demonstrating the u

HTTP Proxy based solution for real-time interception and prioritization of SQL queries.
HTTP Proxy based solution for real-time interception and prioritization of SQL queries.

starproxy ⚠️ starproxy is a prototype: Not currently used in production, but will likely be some day. Table of Contents starproxy Table of Contents Ba

👦 🗞 Paperboy is a GitHub template that delivers RSS by email.

Paperboy Paperboy is a GitHub template that delivers RSS by email. Why? Free, no trackers, easy to set up, and does the job. Usage Click on "use this

A blazingly fast HTTP client with a magnificent request building syntax, made for humans.
A blazingly fast HTTP client with a magnificent request building syntax, made for humans.

🔗 glue Make requests, select JSON responses, nest them in other requests: A magnificent syntax for blazingly fast cli HTTP calls, made for humans. Ta

An async no_std HTTP server suitable for bare-metal environments, heavily inspired by axum

picoserve An async no_std HTTP server suitable for bare-metal environments, heavily inspired by axum. It was designed with embassy on the Raspberry Pi

Implementation of the RealWorld backend API spec in Actix, Rust's powerful actor system and most fun web framework.
Implementation of the RealWorld backend API spec in Actix, Rust's powerful actor system and most fun web framework.

Actix codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API. ❗ (2021/05/13) This cod

An API to track various stats written in Rust. Tracking Github, Wakatime, Spotify, and Duolingo
An API to track various stats written in Rust. Tracking Github, Wakatime, Spotify, and Duolingo

Null API API For collecting data Explore the docs » View Demo · Report Bug · Request Feature Table of Contents About The Project Built With Getting St

Owner
Paulo Bressan
Software Engineer
Paulo Bressan
A simple authentication flow using Rust and Actix-web, with a PostgreSQL database and a sveltekit frontend.

Rust-auth-example This repository aims to represent a simple authentication flow using Rust and Actix-web, with a PostgreSQL database and a sveltekit

Kival Mahadew 4 Feb 19, 2023
Seed is a Rust front-end framework for creating fast and reliable web apps with an Elm-like architecture.

Seed is a Rust front-end framework for creating fast and reliable web apps with an Elm-like architecture.

null 3.6k Jan 6, 2023
This is a simple Api template for Rust ( Axum framework )

Axum-Rust-Rest-Api-Template This project is an open source Rest Api Template built with Rust's Axum framework. Why this project? I have been learning

Thani 20 Jun 16, 2023
Quick demo of a REST frontend with a Redis session store.

axum-rest-starter-example Important Tasks Ensure session UUID is unique Protect /api/ with JWT Add CSRF CORS? Dev Setup (1) Run docker compose up to f

Michael de Silva 23 Dec 31, 2022
The goal of this repository is to have a real world template of a Rust backend using Axum, Diesel, etc.

Axum Diesel Real-World Example A modular Rust backend template based on the Domain-Driven Design (DDD) architecture, utilizing the Axum and Diesel fra

Quentin Piot 11 Sep 22, 2023
Code template for a production Web Application using Axum: The AwesomeApp Blueprint for Professional Web Development.

AwesomeApp rust-web-app More info at: https://awesomeapp.dev/rust-web-app/ rust-web-app YouTube episodes: Episode 01 - Rust Web App - Course to Produc

null 45 Sep 6, 2023
A secure and efficient gateway for interacting with OpenAI's API, featuring load balancing, user request handling without individual API keys, and global access control.

OpenAI Hub OpenAI Hub is a comprehensive and robust tool designed to streamline and enhance your interaction with OpenAI's API. It features an innovat

Akase Cho 30 Jun 16, 2023
REST API server that abstracts the need to write CRUD methods by exposing a standardized API to interact with a Postgres database

Basiliq Exposing a Postgres database via a REST API that follows the JSON:API specs. All in all, a tasty API. What is Basiliq Quickstart Ready to use

Basiliq 54 Apr 21, 2022
An API project using Rust, Actix Web and JWT. *WIP*

Actix-web REST API with JWT (WIP) A simple CRUD backend app using Actix-web, Diesel and JWT Require Rust Stable Postgres Or using Docker How to run Ma

Akhil Sharma 4 Sep 21, 2023
Layers, extractors and template engine wrappers for axum based Web MVC applications

axum-template Layers, extractors and template engine wrappers for axum based Web MVC applications Getting started Cargo.toml [dependencies] axum-templ

Altair Bueno 11 Dec 15, 2022