Skip to content

cybrown/rust-example-1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust sample api

Example api project using Rust, with warp and sqlx.

File architecture

/domain
    Implementation of the business rules

/db
    Sqlx database adapter

    /migrations
        sql schema to run with sqlx-cli

/api
    Api entry points calling the domain

/commands
    Command line utilities calling the domain

/main
    /adapters
        Adapters wrapping external libraries to implement traits provided by the domain

    /main.rs
        Entry point of the application

    /service_registry.rs
        Factories to create the concrete types of many dependencies

    /util.rs
        Some utility functions

/features
    Scenarios to run with karate

/atomic_counter
    A simple thread safe counter

/println_logger
    A simple logger based on println!

/simple_counter
    A counter which is not thread safe.
    Used to illustrate how to use an adapter with an Arc and a Mutex

/uppercaser
    A simple stateless library

Goals

Dependency injection

  • Create services and wire them together
  • Use injection through generic
  • Use injection through dyn Trait
  • Each services can be injected in many consumers
  • Each services can hold a mutable state
  • Service consumers must expose a trait
  • Services should not explicitely implement their consumers traits
  • Adapters wrap services into a struct implementing the expected trait

Unit testing

  • Run unit tests with equality assertions
  • Create mocks implementing a trait returning a predefined value
  • Generate a coverage report
  • Inject mocks with spies to test wether a dependency was called

Project architecture

  • Split one project in multiple files
  • Split in multiple projects
  • Use cargo workspaces

Error handling

  • Seperate error between services and consumers
  • Try anyhow
  • Try thiserror
  • Enrich errors with origin
  • Enrich errors with a message

Logging

  • Add logs

Database (sqlx)

  • Setup sqlx
  • Setup migrations
  • Run at least one data query with sqlx
  • Run at least one data update query with sqlx
  • Transactions

HTTP Server

  • Setup a web framework to return json
  • Route to get all posts
  • Route to create a post
  • Route to publish a post
  • Route to unpublish a post
  • Investigate open api v3
  • Return meaningfull errors for 4** status codes
  • Return 500 on internal errors
  • Return a request id in a header

Configuration

  • Environment variables
  • Read a configuration file in many formats
  • Read cli args
  • Use secrets from a cloud provider or vault

Continuous integration

  • Check formating
  • Run additional static analysis tools
  • Investigate how to get standard unit tests reports
  • Investigate how to get standard coverage reports for unit tests
  • Investigate how to get standard integration tests reports
  • Investigate how to get standard coverage reports for integration tests

HTTP Client

Sockets

File

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages