RustMVC
A lightweight MVC web framework for Rust, built on top of Actix Web and Askama templates.
rustmvc helps you organize your Rust web applications using the familiar Model–View–Controller pattern while keeping it fast, simple, and extensible.
Features
- Actix Web-based routing and async HTTP handling
- Askama templating support (for safe, fast server-side rendering)
- Built-in middleware system
- Simple request context
- Route-based rules like authorization, size limits, and role restrictions
- Extensible actions and responses
Installation
[]
= "4.11.0"
= "0.14.0"
= "2.0.5"
= { = "./rustmvc" } # adjust path based on your workspace
Quick Start Example
Below is a minimal example that defines a small MVC app using rustmvc.
use ;
async
When you open http://127.0.0.1:8080, it will render the Askama template index.html with your message.
Core Concepts
1. RequestContext
Represents the incoming HTTP request. You receive this as a parameter in every controller action.
You can access things like headers, query params, and body easily:
2. ActionResult
Every controller returns an ActionResult, which defines the HTTP response.
Examples:
Html;
Redirect;
File;
3. Server
Acts as the main entry point to your application. It stores the list of registered routes, middlewares, and authentication configuration.
Create a new instance
let mut server = new;
Add a route
Each route maps a path and HTTP method to an action function.
server.add_route;
server.add_route;
Start the server
server.start.await?;
The server automatically matches routes, applies middlewares, and handles results.
4. Middleware
Middlewares are executed in order before the controller action runs. They can modify the request, log activity, or even return responses directly.
server.add_middleware;
You can stack multiple middlewares for logging, authentication, etc. For example, you could log timing or enforce a global header.
5. RouteRules
Rules allow attaching security or validation behavior to individual routes.
Usage examples:
// Public route
server.add_route;
// Restricted by payload size
server.add_route;
6. RenderModel Trait
Your view models (Askama templates) must implement the RenderModel trait.
Example with Askama:
7. Authentication (Optional)
The server supports JWT-based authentication via an AuthConfig that can generate and validate tokens.
This part can be extended by enabling the commented-out set_auth_config and validation logic.
Example token generation:
let claims = Claims ;
let token = server.generate_token;
8. File Serving
Static files (like assets) are served from the wwwroot directory automatically when returned via ActionResult::File.
Example Middleware Chain Execution Flow
If you register:
server.add_middleware;
server.add_middleware;
For any request, execution will be:
logging_middleware -> auth_middleware -> route_action -> response
This composition pattern makes feature layering (e.g., auth, logging, request limits) simple and modular.
Example Folder Structure
project/
│
├── src/
│ ├── main.rs
│ ├── controllers/
│ │ └── home.rs
│ └── rustmvc/
│ ├── mod.rs
│ └── authentication.rs
│
├── templates/
│ └── index.html
│
└── wwwroot/
├── css/
├── js/
└── images/
Summary
RustMVC is ideal for:
- Building lightweight, structured web servers in Rust
- Integrating Askama templates for server-rendered pages
- Managing middlewares, routing, and request contexts cleanly within Actix Web
If you already use Actix Web and want structured controllers, type-safe views, and rule-based routing, RustMVC provides a great foundation.