· 3 min read

Loco - Ruby on Rails but with Rust

Explore a framework built on top of Axum for backend / fullstack projects

Loco is new. Brand new.

In fact, the first post about it on r/rust is only 10 months old at the time of writing.

I discovered it after standing up a new Axum/Rust project for work, and spent a week carefully selecting the pieces of what I needed to build.

First, it was the framework. Actix-web has a lot going for it, but Axum is my favorite from a purely speed and minimalist setup, and GoLang has never quite captured my heart.

Next, I needed a way to store my data. I was working with graph data, but wanted the reliability of Postgres to go along with a query language I won’t bother talking about here.

I needed a way to talk with my db, and while I’ve used sqlx in the past, I didn’t want to deal with writing pure SQL strings and dealing with input validation, and took a look at the ORM’s available. SeaORM seemed to be well received, so I looked at that.

So I ended up with Rust, Axum, Postgres, and SeaORM. I spent a few days going through docs and putting all the pieces together, and finally had a working foundation for the project.

The following Monday I discovered Loco.

It not only used precisely the stack I so carefully put together, but came with its own CLI that generated the entire stack with a full or backend-only setup, and included authentication endpoints and SMTP configs to boot. And not to mention, the code was clean.

Nearly a year of effort had gone into it, the migrations were easy to understand, and it had great generators to do the hard parts of making migration files that SeaORM relies on for creating its underlying entities.

On the fence because it’s still in development? Here’s a bit more of what you can do…

Key Features

1. Rapid Application Development

Loco enables developers to generate application scaffolding effortlessly. For instance, creating a blog backend can be accomplished with a few simple commands:

cargo install loco-cli
cargo install sea-orm-cli # Only if database support is needed
loco new myapp

This setup generates a new application structure, including user authentication and database configuration.

2. Simple Request Lifecycle

With Loco, handling web requests is straightforward. Here’s an example of a basic controller function that retrieves an item by its ID:

pub async fn get_one(
    Format(respond_to): Format,
    Path(id): Path<i32>,
    State(ctx): State<AppContext>,
) -> Result<Response> {
    let res = items::Entity::find_by_id(id).one(&ctx.db).await?;
    match res {
        Ok(item) => respond_to_json_or_html(item, respond_to),
        Err(err) => Err(err),
    }
}

3. Integrated Background Jobs

Loco includes support for background tasks, allowing developers to offload compute-heavy operations seamlessly. Implementing a worker is as simple as defining a perform function for the Worker trait:

impl worker::Worker<DownloadArgs> for DownloadWorker {
    async fn perform(&self, args: DownloadArgs) -> worker::Result<()> {
        // Your background processing logic here
    }
}

4. Deployment Made Easy

Deploying applications with Loco is streamlined through a guided CLI interface. Choose your preferred deployment method, such as Docker or Nginx, with just a few prompts.

cargo loco generate deployment

5. Built-in Authentication System

Loco comes with a complete authentication suite, including user registration and JWT-based authentication. This allows for secure user management out of the box.

Example User Registration

To register a new user, send a POST request to the /api/auth/register endpoint:

curl --location '127.0.0.1:5150/api/auth/register' \
     --header 'Content-Type: application/json' \
     --data-raw '{ "name": "Loco user", "email": "[email protected]", "password": "12341234" }'

Conclusion

Loco.rs simplifies web development in Rust, allowing developers to focus on building robust applications without getting bogged down by complexity. With features like easy scaffolding, built-in authentication, and seamless deployment, Loco empowers developers to harness the full potential of Rust.

For more information and detailed documentation, visit Loco.rs.

Back to Blog

Related Posts

View All Posts »