Generators: option to write routes to a single `src/routes.rs`, Rails-style
Feature Request
Is your feature request related to a problem? Please describe.
The README opens with "Loco is Rust on Rails" and says Rails developers will feel at home. One place where they don't is routing: rails g scaffold posts adds resources :posts to config/routes.rb, so the whole route table lives in one file. Loco's generators instead put a routes() function in each controller and wire it up in src/app.rs.
Loco can already do the Rails layout. AppRoutes takes any Routes, so a central file works:
// src/routes.rs
pub fn all() -> AppRoutes {
AppRoutes::with_default_routes()
.add_route(Routes::new().prefix("posts")
.add("/", get(posts::list))
.add("/", post(posts::add))
.add("/{id}", get(posts::get_one)))
}The problem is that the generators don't know about it, so every generate controller / generate scaffold has to be undone by hand.
Describe the solution you'd like
An opt-in mode where controller and scaffold generators:
- emit only the handlers (no
routes()in the controller), and - add the route block to
src/routes.rsinstead of injecting intoapp.rs.
Two possible triggers, open to whichever you prefer:
- a flag at project creation (
loco new --routes-file) that records the choice, or - detection: if
src/routes.rsexists, generators inject there.
Existing projects and the current default are unaffected. If this is something you'd accept, I'm happy to open a PR with the generator changes and docs.
Open question: would you consider it as the default for new projects? A single routes file has been Rails' convention since its early versions, and it's what developers coming from Laravel, Phoenix or Django expect too — which is the audience "Rust on Rails" speaks to.
Describe alternatives you've considered
- Overriding the controller and scaffold templates per project with
cargo loco generate override. It works, but every project has to maintain its own copies of the templates, and they drift from upstream on each Loco upgrade. - Keeping per-controller
routes()and usingcargo loco routesto see the full table. That lists the routes, but doesn't give the single editable file Rails users expect.
Source: loco-rs/loco