/rust-web-api-microservice-template

Rust API Server: A versatile template for building RESTful interfaces, designed for a comprehensive production-grade setup and configuration using the Rust programming language.

Primary LanguageRustMIT LicenseMIT

RUST API SERVER

Logo

License Continuous Integration

Introduction

Welcome to the Rust API Server! This server provides a simple REST interface for your applications. This README will guide you through setting up and running the server, as well as configuring its various options.

Deploy

Automated Builds: Builds are automatically generated for each commit to the repository in the main branch and are subsequently pushed to Docker Hub. Tags are applied using the commit SHA, branch name, and the latest tag if released on the main branch. You can find a list of available tags here.

Release Binaries: For every release, separate cli binaries are built. These binaries can be downloaded from here and are available for various operating systems and architectures. You are welcome to use the automated binaries or build your own.

Contributions and PRs: If you submit a pull request, please note that images are not built by default. A maintainer will need to retag them for the build process to take place.

Docker

  1. Pull the docker image
docker pull thuan2172001/rust-server:latest
  1. Run the image
docker run -d -p 8000:8000 thuan2172001/rust-server:latest

How To Run

To get started, execute the following command in your terminal:

./cli --help

This will display the available options for running the server:

Simple REST server

Usage: cli [OPTIONS] [COMMAND]

Commands:
  config  Print config
  help    Print this message or the help of the given subcommand(s)

Options:
  -c, --config-path <CONFIG_PATH>  Config file [default: config/default.toml]
  -v, --version                    Print version
  -h, --help                       Print help

Example

  • Multiple config locations
./cli -c ./config/*.toml -c deploy/local/custom.toml
cargo install bunyan
./cli -c ./config/*.toml -c deploy/local/custom.toml | bunyan

Configuration

Order of apply

Configuration is applied in the following order: config files -> environment variables -> command-line arguments.

If you use -c *.toml to load config files, please be mindful of the order in which the files are applied.

Environment Variable Examples

The server can be configured using environment variables. Below is a table outlining the available configuration options:

Hierarchical child config via env, separated by using __. Specify list values by using , separator

ENV DEFAULT VALUE NOTE
RUST_LOG > LOG__LEVEL "INFO" Log level
SERVER__URL
SERVER__PORT
SERVICE_NAME
EXPORTER_ENDPOINT
DB__PG__URL "localhost"
DB__PG__MAX_SIZE 5432
REDIS__HOST "localhost"
REDIS__PORT 6379

Make sure to set these environment variables according to your needs before running the server.

GitHub Flow CI Configuration

  1. Set Docker Hub Secrets:

    • Go to repository Settings > Secrets.
    • Add DOCKER_USERNAME and DOCKERHUB_TOKEN.
  2. Enable Dependabot Alerts:

    • In repository Insights, enable "Dependabot alerts" and "Security & Analysis".

Checklist

Basic Functionalities

Ensure comprehension and implementation of concepts outlined in the book with attention to detail. Key considerations include:

  1. Incorporating descriptive comments to enhance code readability.
  2. Implementing tracing mechanisms for effective debugging.
  3. Writing comprehensive test cases to validate functionality.
    1. Using https://testcontainers.com for integration tests.
  4. Utilizing version control with Git for code management.
  5. Structuring code in a logical and maintainable manner.
  6. Containerizing the application using Docker for portability and scalability.

Advanced Functionalities

Demonstrate proficiency in advanced development practices including:

  1. CLI Interface.
    1. Embed Git Info, Config Tool.
  2. Load Configuration from a File.
  3. Multiple Implementations.
  4. Advanced Tracing.
  5. CI/CD.
    1. Publish binary artifacts in Github.
    2. Push Docker images.
    3. Build pipeline on amd arch.
    4. Build pipeline on arm arch.
  6. Docker Image Optimization.
  7. Load test using K6.
    1. Use Flamegraph for profiling.
    2. Better UI.
  8. Comprehensive DB query filter for list().
  9. Optimize release binary performance.
  10. Docs on how to use this repo, the design behind the scene.
  11. Dependabot
    1. Update Rust.
    2. Update Docker image.

Feel free to explore and expand upon these functionalities as needed for your project. Happy coding!

Load Testing and Profiling

For load testing and profiling your Rust API server, refer to the Load Testing and Profiling with K6 and Flamegraph guide. This document provides detailed instructions on using K6 and Flamegraph for load testing and profiling purposes.