Gerust 是一个用于 Rust 后端项目的项目生成器。它负责处理意外的复杂性,让您可以专注于重要的事情。
Gerust provides an architecture and tooling for Rust backend projects. It takes care of the accidental complexity that comes with writing backends with Rust so you can stay focused on the essence of the system you're building:
For now, Gerust is just a project generator that creates the files and structure to get you started. There is no runtime dependency on Gerust – all the code that goes into your project remains under your control.
Gerust projects are based on axum and use SQLx and PostgreSQL for data storage (if data storage is used at all).
[!NOTE] This project has been created by Mainmatter.
Check out our landing page if you're looking for Rust consulting or training!
Gerust can be installed with cargo:
cargo install gerustA new project is created with the gerust command, e.g.:
gerust my-appBy default, Gerust will generate an empty project with the complete project structure as described below but without any actual entities, controllers, etc. If you're just getting started looking at Gerust, creating a full project, complete with example implementations of all concepts via --full might be a better starting point:
gerust my-app --fullFor projects that do not need database access, there is also the --minimal option that will generate a project without any of the concepts and structure related to database access – no db crate, no sqlx dependency.
Gerust uses Cargo workspaces to separate distinct parts of the system into separate crates:
.
├── cli // CLI tools for e.g. running DB migrations or generating project files
├── config // Defines the `Config` struct and handles building the configuration from environment-specific TOML files and environment variables
├── db // Encapsulates database access, migrations, as well as entity definitions and related code (if the project uses a database)
├── macros // Contains macros, e.g. for application tests
└── web // The web interface as well as tests for itLet's see what these crates are resonsible for and how they work in detail:
web crateThe web crate contains the main axum application, providing the web interface of the system. It contains the controllers with the implementations of the exposed endpoints, as well as any middlewares. The web crate also contains the application's main executable, which when starting up, will determine the environment the application runs in, load the configuration, initialize the app state, set up tracing and error handling, and bind the server to the configured interface.
The crate uses a simple folder structure:
web
├── controllers // Controllers implement request handlers for the exposed endpoints
├── middlewares // Tower middlewares for pre-processing requests before they are passed to the request handlers
├── lib.rs // Code for starting up the server
├── main.rs // Main entrypoint of the application
├── routes.rs // Mapping of request handlers to routes
├── state.rs // Definition and construction of the application state
└── tests // Application testsApplication tests that cover the entire stack of the system including middlewares, controller, as well as database access are maintained in the web crate.
Testing backends is typically straight forward: request a particular endpoint with a particular method and potentially query string and/or request body and assert the response is what you expect. However, things become more complicated when the server you're testing uses a database. In your tests, you then need to seed the database with test data to establish a well-defined state for the test. You also need to clean up afterwards or better, use isolated database states for the different tests so they don't interfere with each other. There are several mechanisms for that like transactions, cleanup scripts, etc.
Gerust uses an approach for test isolation that allows parallel execution of tests without adding a ton of complexity: every test runs in its own database. These test-specific databases are automatically created as copies of the main test database and destroyed after the test has completed. All that is made easily available via the [db_test] macro:
…The concept of changesets as well as the database access utilities like create_task, are explained below.
db crateThe db crate only exists for projects that use a database and contains all functionality related to database access from entity definitions, functions for reading and writing data, as well as migrations. Gerust uses SQLx and PostgreSQL without any additional ORM on top. Instead, it defines entities as simple structs along with functions for retrieving and persisting those entities. Validations are implemented via changesets that can get applied to or be converted to entities if they are valid:
…Database queries are checked for correctness at compile time using sqlx's compile-time checked queries.
The crate's folder structure consists of 3 main folders:
db
├── migrations // Database migrations as plain SQL files
├── src
├── entities // Entity structs, changesets and related functions for retrieving and persisting records (see example above)
└── test-helpers // Functions for retrieving and persisting records that are only relevant for tests (these are defined behind the `test-helpers` feature)Test helpers allow to make specific database access functions available only for application tests but not for actual application code. If e.g. the system does not allow for creating new user accounts but tests need to be able to create users, a create_user function could be defined in db/src/test_helpers/users.rs in the db crate.
config crateThe config crate contains the Config struct that holds all configuration values at runtime as well as code for parsing the configuration based on a hierarchy of TOML files and environment variables. The Config struct contains fields for the server and database configuration (if the application uses a database) and can be extended freely:
#[derive(Deserialize, Clone, Debug)]
pub struct Config {
pub server: ServerConfig,
pub database: DatabaseConfig, // The database configuration only exists for projects that use a database
// add your config settings here…
}The values for the server and database configuration are read from the APP_SERVER__IP, APP_SERVER__PORT, and APP_DATABASE__URL environment variables. Any application-specific settings are read from app.toml as well as environment-specific file, e.g. production.toml such that settings in the environment-specific files override those in app.toml.
The main files and folders in the crate are:
…cli crateThe cli crate contains the db binary for running database operations such as executing migrations (this binary only exists for projects that use a database) as well as the generate binary for generating project files such as entities, controllers, tests, or middlewares. The workspace is configured so that those binaries can be executed with just cargo db and cargo generate:
……You would typically not have to make any changes to the cli crate.
macros crateThe macros crate contains the implementation of the db_test macro. You would typically not have to make any changes to the cli crate.
Projects generated by Gerust come with a complete CI setup for GitHub Actions that includes:
When making changes to Gerust itself and you'd like to manually test out your changes, you can generate a sandbox app with:
cargo run -- --full my-appThen edit with your favorite editor:
${EDITOR} my-appAnd:
cargo run -- -f my-new-appRinse:
rm -rf my-app my-new-app[!IMPORTANT] The generated CI configuration uses offline query validation during its Clippy job. On first check-in, and each time the SQL queries in the db crate get updated, ensure
cargo db prepareis run.
"Gerust" is a play on "Gerüst", the German word for "framework" and Rust – thanks to @skade who had the idea originally and allowed us to use it!
Gerust is developed by and © Mainmatter GmbH and contributors. It is released under the MIT License.
暂无开放 Issues,或尚未同步最近议题。