Best way to implement "loose architecture" / best practice for building beginner backend rust website?

Hello all,

I am a decently new web developer and I have been trying to build an web backend with a Rust. I have been trying to work on building my applications with what I've heard people call "loose architecture" aka layering the application and modularizing it.

The problem is, I'm not sure if what I am doing is actually what is considered best practice for Rust as I have done almost all my backend work in either Node or Go. So I wanted to quickly summarize my approach and see if anyone had input.

Right now the basic plan I have four basic parts:

main.rs

  • takes in env variables for db login/password and initializes server then passes db login info to a db layer function
  • that function returns self-made wrapper with db connection inside
  • creates server then passes it as well as db-wrapper to controller layer function to add routes

controller layer (directory)->

  • set up route functions that call service layer functions on db-wrapper object (aka a get_all_user() method on the GET route for getting all users, that method would return all users in an array and then the controller sends that as a response in the route)

service layer (directory)->

  • takes db-wrapper object and calls db layer functions on it to get it to return raw data. Then it takes the raw data and preforms business logic on it. Then returns that data up to the controller layer.

Db layer (directroy) ->

  • this is the only layer that actually interacts with postgres. Its methods take postgres client inside the db-wrapper and call the postgres commands on it to get the data. Then it returns this raw data up to the service layer.

I could post the code, but I imagine its easier to read this then try to interpret a bunch of random spread out files. The basic idea is that each layer calls the layer below it to get the data, then preforms its job on the data, then passes that data to the layer above.

The issue is this relies a lot on OOP since I am passing around structs. I imagine I could use traits and get the same basic function (as in impl traits for the postgres struct and then rather than require a specific wrapper struct in parameters, just require an object the impls XYZ traits).

Any thoughts or examples on how a modern rust backend might be built?

Hello @46756E

Sounds good to me - but I'm also a novice in Rust. I layered my project like this

  • main - triggering config parsing, hook in all controllers, prepare all the services
  • controller - takes care of all the HTTP-like handling - or of a route if you like.
  • service - interacts with mongodb where as a service is responsible for one collection - so this is the idea.

A database layer does not exist. Till now it worked for me - it is maybe not a good approach but till now it fits my intentions. The code can be found here.

Stefan

Your plan is sound, you're just missing that the DB layer maps to your domain models, it doesn't provide the raw data to the service layer.

Thanks all, I appreciate the example code too!