Skip to content

Rust Actix REST API application with MongoDB

License

Notifications You must be signed in to change notification settings

maxdhn/actix-api

 
 

Repository files navigation

actix-api

GitHub GitHub release (latest SemVer including pre-releases) GitHub Workflow Status GitHub top language GitHub last commit

Rust Actix REST API application with MongoDB

Minimize Binary size

  • To minimize the size of binaries, I followed - article. It help me reduce the final binary size from 22M to 6.8M
  • Also used cargo-unused-features to cleanup unused features. This helped me reduce from 6.8M to 5.3M.
    • Run unused-features analyze followed by unused-features prune --input report.json. More info in the github link above.

Todo

  • Basic CRUD calls using MongoDB.
  • Environment Config.
  • Logging.
  • Add JSON validator.
  • Add security may be JWT.
  • Add role based JWT.
  • Global and Local api Error Handling.
  • Form bean and Entity bean mapping.
  • Add Unit testing.
  • Add integration testing.
  • Docker build... with minimal base image.
  • Pagination & Sorting of get list.
  • CORS Support for API.
  • Custom validation error.

Feature Todo

  • Add events (like, auth event, user event etc...) and persist in a MongoDB table.
  • Implement Kafka or Pulsar event stream. Preferably Apache pulsar.
  • Implement REST API client call using reqwest lib.
  • Redis Cache for frequent calls.

Tools and IDE

  • MscBookPro M1
  • Rustup for managing rustc and plugins
  • IDE used are Intellij IDEA and Helix Editor. Helix editor is developed using Rust 🦀.
  • Use Colima. Its a light weight cli alternative to docker desktop.
  • Use Starship for shell with fish shell. Another tool developed using Rust 🦀.
  • Terminal tool using is Alacritty with tmux. Alacritty is another rust based 🦀.3

Similar Project

  • Working on similar project with axum and sqlx, the srouce code is here

Random Notes.

Api Error response structure.

{
  "status": 404,
  "time": "2022-12-25T15:25:35.089z",
  "message": "User not found for id - 2893f9283uo2",
  "debugMessage": "User not found for id - 2893f9283uo2",
  "subErrors": [
    {
      "object": "users",
      "field": "email",
      "rejectedValue": "dummyEmailgmail.com",
      "message": "invalid email address"
    }
  ]
}

Paginated data response structure.

  • Sample request - [GET] https://.../users?offset=20&limit=20&sort_by=name
  • Sample response with three elements - data, meta and _links.
{
  "data": [{
    "id": "usr_DwgQxN3gLRX1p0g7bwny1",
    "userName": "john_doe",
    "firstName": "john",
    "lastName": "Doe",
    "email": "john_doe@c12.io"
  },
    {...}
  ],
  "meta": {
    "current_page": 1,
    "page_size": 20,
    "page_count": 12,
    "total_results": 348,
    "search_id": "VE4heV3F5m2Vf0GO_dLhu",
    "search_criteria": "",
    "sort_by": "lastName"
  },
  "_link": {
    "self": {
      "href": "/v1/users?limt=20&offset=40"
    },
    "previous": {
      "href": "/v1/users?limit=20&offset=20"
    },
    "first": {
      "href": "/v1/users?limit=20&offset=0"
    },
    "next": {
      "href": "/v1/users?limit=20&offset=60"
    },
    "last": {
      "href": "/v1/users?limit=20&offset=120"
    }
  }
}

About

Rust Actix REST API application with MongoDB

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 96.4%
  • Dockerfile 3.6%