I was trying to use rust for my backend service, but I couldn't find any template for it. So I stole some code from multiple repositories and managed to create something.
- First of all you need to install
rustandcargoon your machine. - Then you need to install
diesel_clito manage your database. - You need to create
.envfile in the root directory of the project based on example file.
cargo runmigrations: Database migrationssrc: Source codecontrollers: Controllers logic (Path matching, etc.)dtos: Data transfer objectsmiddlewares: Middlewaresmodels: Database modelsrepositories: Database repositoriesservices: Business logicutils: Utilitiesdb.rs: Database connectiondi.rs: Dependency injection (DI) containererror.rs: Error definitionshasher.rs: Password hasher
main.rs: Main fileapp_state.rs: App state object definition (DI registration)constants.rs: Global constants (Env keys, etc.)schema.rs: Auto-generated database schema definition
All controllers are defined in src/controllers/mod.rs file.
You can create a new controller for each entity in your system.
All objects that are coming into the system
should be defined as an DTO in src/dtos/mod.rs file.
All middleware should be defined in src/middlewares folder
and registered in the src/main.rs file.
All database models should be defined in src/models directory.
Every interaction with the diesel database should be defined in src/repositories folder.
Every new repository should be registered in src/utils/di.rs file.
All the business logic should be defined in the service file. Services should be also registered in DI container.
Utilities should be located in src/utils folder.
This template uses diesel with Postgres as an ORM.
Be aware that diesel uses DATABASE_URL environment variable saved in .env file to connect to the database.
diesel migration generate <migration_name>diesel migration run