RustMVC
A lightweight MVC (Model-View-Controller) framework for Rust, built on top of Actix Web and Askama templates. RustMVC provides a simple, intuitive API for building web applications with routing, middleware support, and template rendering.
Features
- Simple Routing: Register routes with path patterns and controller actions
- Middleware Support: Add custom middleware for logging, authentication, and request processing
- Template Rendering: Built-in support for Askama templates
- Request Context: Easy access to query parameters, headers, and request body
- Multiple Response Types: HTML, views, redirects, static files, and 404 handling
- Built-in Logging: Default middleware for request/response logging
Installation
Add RustMVC to your Cargo.toml:
[]
= "0.1.1"
= "4.11.0"
= "0.14.0"
Quick Start
Here's a simple example to get you started:
use ;
use Arc;
// Define a view model with Askama template
// Controller action
async
Core Concepts
Server
The Server struct is the main entry point. Create a new server instance, register routes and middleware, then start it:
let mut server = new;
server.add_route;
server.start.await?;
Routes
Routes map URL paths to controller actions:
server.add_route;
server.add_route;
Request Context
The RequestContext provides access to request information:
Action Results
Controllers return ActionResult which can be one of several types:
// Return HTML directly
Html
// Render a view/template
View
// Redirect to another URL
Redirect
// Serve a static file from wwwroot/
File
// Return 404
NotFound
Middleware
Add middleware to intercept and process requests:
// Authentication middleware
server.add_middleware;
// Custom logging middleware
server.add_middleware;
Middlewares are executed in the order they are added.
Templates with Askama
Create templates in the templates/ directory:
<!-- templates/home.html -->
{{ title }}
{{ title }}
Define a view model:
Use it in your controller:
Static Files
Place static files in the wwwroot/ directory. They can be served using ActionResult::File:
Project Structure
my-app/
├── Cargo.toml
├── src/
│ └── main.rs
├── templates/
│ └── home.html
└── wwwroot/
├── css/
│ └── style.css
└── js/
└── app.js
Example Application
use ;
use Arc;
async
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.