Skip to content

Latest commit

 

History

History
130 lines (102 loc) · 5.21 KB

README.md

File metadata and controls

130 lines (102 loc) · 5.21 KB

rust-microservice-template

GitHub release (latest SemVer) license GitHub Workflow Status (with branch)

Template for microservice based on Domain Driven Design principles with Rust

The purpose of this project is to provide a means for building microservices with the last version of Rust that follows basic Domain Driven Design principles

⭐ Give a star

If you're using this repository for your learning, samples or your project, please give a star. Thanks 👍

Table of Contents

Usage

Prerequisites

Before you can install and configure the microservice, you will need to ensure that you have the following prerequisites installed:

  • Docker and Docker Compose

Installation

Once you have all the prerequisites installed, you can follow these steps to install and configure your microservice:

  1. Clone the repository for your microservice using Git:
git clone https://github.com/nkz-soft/rust-microservice-template.git
  1. Change into the directory for your microservice:
cd rust-microservice-template/deployment/docker
  1. You can use the included Dockerfile and docker-compose.yml files to build and deploy the microservice. Simply run the following command in the root directory of your project:
./docker-compose.sh up --build -d
  1. Verify that the microservice is running correctly by visiting the endpoint in your web browser or using a tool like curl:
curl -v  http://localhost:8181/to-do-items

Configuration

To configure the microservice, you will need to modify the configuration file: config.app.toml.

[service]
http_url = '127.0.0.1:8181'
service_name = 'rust_template_service'

[database]
pg.user = 'postgres'
pg.password = 'postgres'
pg.host = '127.0.0.1'
pg.port = 5432
pg.dbname = 'rust_template_db'
pg.pool.max_size = 16

You can also configure via environment variables.

export MICROSERVICE__SERVICE__HTTP_URL="127.0.0.1:8181"
export MICROSERVICE__SERVICE__SERVICE_NAME="rust_template_service"
export MICROSERVICE__DATABASE__PG.USER="postgres"
export MICROSERVICE__DATABASE__PG.PASSWORD="postgres"
export MICROSERVICE__DATABASE__PG.HOST="127.0.0.1"
export MICROSERVICE__DATABASE__PG.PORT=5432
export MICROSERVICE__DATABASE__PG.DBNAME="rust_template_db"
export MICROSERVICE__DATABASE__PG.POOL.MAX_SIZE=16

Architecture

The microservice is divided into three layers: the Domain Layer, the Application Layer, and the Infrastructure Layer.

Domain Layer

The Domain Layer is the heart of the Domain Driven Design (DDD) approach. It contains the business logic and rules that drive the application. In Rust, the Domain Layer consists of structs, traits, enums, and functions that model the problem domain in a way that is easy to understand and maintain.

Application Layer

The Application Layer is responsible for coordinating the Domain Layer and the Infrastructure Layer. It translates user requests and external events into actions that the Domain Layer can understand, and communicates the results back to the user or external systems.

Infrastructure Layer

The Infrastructure Layer is responsible for providing the necessary infrastructure to run the application. This can include things like databases, message queues, and external APIs.

Presentation Layer

The presentation layer is responsible for handling user interactions and presenting information to users. This layer typically includes user interfaces such as web applications, desktop applications, mobile apps, or APIs.

CQRS

CQRS (Command Query Responsibility Segregation) is a pattern that separates the read and write responsibilities of an application into separate models.

Deployment

Plan

  • New blank microservice solution
  • Basic Github actions
  • Docker compose
  • REST API
  • CQRS (Command Query Responsibility Segregation)
  • PostgreSQL storage
    • Add support for migrations
    • CRUD Operation
  • Integration tests
  • Configuration
    • Configuration file
    • Environment variables
  • OpenAPI documentation
  • Advanced error handling
  • Coming soon :)

Technologies used

  • Rust: The Rust Programming Language.
  • Actix: Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.
  • Refinery: Refinery strives to make running migrations for different databases as easy as possible.
  • rust-postgres: PostgreSQL support for Rust.
  • testcontainers-rs: Testcontainers-rs is the official Rust language fork of http://testcontainers.org.
  • utoipa: Code first and compile time generated OpenAPI documentation for Rust APIs.