2 releases
Uses new Rust 2024
| 0.1.1 | Jan 17, 2026 |
|---|---|
| 0.1.0 | Jan 17, 2026 |
#2182 in Web programming
33KB
821 lines
NestRS CLI
NestRS is a lightweight, single-thread async Rust web API starter with a NestJS-like folder structure. This repository contains the CLI (nestrs) used to generate new NestRS projects.
Features
- Single-thread async runtime (Tokio current-thread)
- Strongly inspired on NestJS proven development patterns
- Request/Response primitives with automatic status lines
- Controller-first route declarations (NestJS-like)
- Domain-driven structure: controller, service, repo, DTO
- CLI scaffold to generate new entities
CLI Usage
Generate a new NestRS project:
nestrs new my-app
This will create a new folder with the full base structure and files.
Template Project Structure
src/
bin/
scaffold_entity.rs
domain/
dog/
controller.rs
service.rs
repo.rs
dto.rs
mod.rs
mod.rs
primitives/
http/
request.rs
response.rs
mod.rs
mod.rs
routing/
init.rs
mod.rs
main.rs
Running the Generated Server
cargo run
The server listens on 127.0.0.1:8080.
Routing Flow
- The router is initialized at startup via
init(init_routes()). - Each controller exposes a
routes()method returningRoutedefinitions. - The router matches method/path and invokes the controller handler.
- The controller returns a
Response, which is written back to the client.
Creating a New Entity
Use the scaffold CLI to generate a new domain entity:
cargo run --bin scaffold_entity -- Dog
This will:
- Create
src/domain/dog/with controller/service/repo/dto files. - Add the entity to
src/domain/mod.rs. - Register the controller routes in
src/routing/init.rs.
Adding Controller Routes
Routes are declared inside each controller’s routes() method using the route! macro:
Route::new("GET", &["dog"], route!(DogController::get_all))
Handlers receive:
RequestRouteParams(path params like:idare available viaparams.get("id"))
Example Request
curl http://127.0.0.1:8080/dog
The default Dog controller responds with:
Woof
Notes
- Responses automatically include
Content-LengthandConnection: closeif not provided. - The router is a singleton registry initialized before the server starts listening.
Dependencies
~2.1–5MB
~77K SLoC