A new Rust framework inspired by Rails inspiration

Loco is a brand new framework inspired by Ruby on Rails, developed using the Rust language to implement the MVC pattern for web applications. Rust is known for its concurrency, safety, strong typing, and high performance, and these attributes give Loco a significant advantage over Rails and other derivative frameworks. The main target users of this framework are those Rust programmers who wish to easily build MVC applications. Loco provides a familiar and convenient development experience, without wasting time looking for suitable tools.

Although Rust already has a rich ecosystem of libraries and frameworks such as Axum, Actix, Rocket, Tokio, Warp, and Reqwest, Loco marks the birth of the first framework in the Rust ecosystem that is dedicated to this area. The latest version of Loco is 0.3.1, which has seen 11 minor version iterations since its launch in November 2023. The framework comes with dedicated CLI tools, an application creation wizard, and a local development server. It follows the same design principles as Rails and aims to serve Rust developers.

Loco’s philosophy is derived from David Heinemeier Hansson’s article “The One Person Framework,” emphasizing that a powerful set of single tools can allow developers to independently create modern applications, greatly reducing the time and effort needed for application deployment. This includes the selection of libraries, robustness considerations, and architecture scalability strategies. With the “single-person framework” concept, these complex and time-consuming decisions are integrated into the framework, freeing up developers’ hands. While this may sacrifice some flexibility and rely heavily on the implementation of conventions and patterns, the Rails framework has won widespread acclaim in this way over the years and has inspired the birth of other frameworks like Grails. Grails is a framework based on the Groovy language, which runs on the Java Virtual Machine (JVM).

Loco is committed to bringing similar powerful features as Rails to the Rust developer community. The process of installing Loco follows the usual convention of the Rust ecosystem, using the following command line:

$ cargo install loco-cli

Creating a new Loco application is similar to using “rails new” in Rails:

$ loco new

The definition of controllers and routes also adopts a similar pattern, making full use of the Axiom library. For example, the following function is responsible for returning a JSON-formatted response:

async fn current() -> Result<Json<HomeResponse>> { format::json(HomeResponse::new("loco"))}

And the route definitions can be implemented as follows, reflecting the similarity to defining routes in Rails:

pub fn routes() -> Routes { Routes::new().add("/", get(current)).add("/loco", get(current)).add("/:id", get(get_one))}

One of Rails’ famous features is its ability to easily create models and bind them with REST APIs, front-end views, and controllers. Loco also inherits this functionality.

Loco uses “sea_orm” to easily generate models. For example, the following command can be used to generate a book model with a title and an international standard book number:

$ cargo loco generate model books title:string isbn:string

Using “ActiveModelBehaviour” to define the behavior of models is quite convenient, and it supports custom functions for operations before and after. In addition, Loco also offers database migration functionalities:

$ cargo loco generate migration add_web_url

Loco provides developers with test utilities, which can be enabled in the “Cargo.toml” file:

[dev-dependencies]
loco-rs = { version = "*", features = ["testing"] }

The default provided User entity and the secure API implementation based on JWT allow for a swift setup of the authentication process, including common features such as registration, activation, and password reset.

When creating an application using “loco-cli,” if you opt for a “Saas app,” the program automatically generates a launcher with predefined routes, such as:

$ cargo loco routes
...
[POST] /auth/forgot
[POST] /auth/login
[POST] /auth/register
[POST] /auth/reset
[POST] /auth/verify
[GET] /user/current

Loco combines the development experience of Rails with the powerful features of the Rust language, seamlessly integrating into the Rust ecosystem, offering complete functionalities such as model generation, behavior definition, database migration, and testing. Although it is a relatively new project, it has already become a fresh and vibrant addition to the field of Rust web frameworks.

Share this article
Shareable URL
Prev Post

“Geek’s Appointment” Analyzing New Mobile Technologies in the Era of Large Models

Next Post

If these abnormalities occur when you wake up, it could be your heart crying out for help, beware of heart attacks coming your way!

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next