Rust MVC Framework
A lightweight Rust implementation of an MVC (Model-View-Controller) framework, inspired by ASP.NET MVC. This framework allows you to define controllers, actions, and views in Rust and serve HTML pages dynamically. It integrates with Askama templates for compile-time HTML rendering.
Features
- Controllers & Actions: Define Rust structs as controllers and functions as actions.
- Dynamic Routes: Map URL paths to controller actions easily.
- ActionResult Enum: Supports returning
Html,View,Redirect,File, andNotFound. - Askama Templates: Compile-time HTML rendering with Rust logic inside templates.
- Static Files Support: Serve files from
wwwroot. - Lightweight HTTP Server: Built on top of
actix-web.
Installation
Add this crate as a dependency in your project:
[]
= { = "../rustmvc" } # from GitHub
= { = "0.12", = ["macros"] }
= "4"
Getting Started
1. Define a Model
use Template;
use RenderModel;
2. Define a Controller
use HashMap;
use Arc;
use ;
;
3. Define Routes and Start the Server
use ;
async
4. Add a Template
Create a file templates/index.html:
{{ title }}
{{ message }}
Folder Structure
rustmvc/
├─ src/
│ ├─ controllers.rs
│ ├─ lib.rs
│ └─ main.rs
├─ templates/
│ └─ index.html
├─ wwwroot/
│ └─ static files here
├─ Cargo.toml
└─ README.md
License
This project is licensed under the MIT License. See the LICENSE file for details.
Notes
- Use
ActionResult::Viewwith Arc-wrapped models implementingRenderModelfor dynamic rendering. - Static files are served from the
wwwrootfolder. - Askama templates are precompiled at build time for fast rendering.
This framework is a work in progress and serves as a learning project to replicate ASP.NET MVC concepts in Rust.