A toy web framework inspired by gin-gonic/gin and expressjs/express.
Just add rum_framework to the dependencies of Cargo.toml.
[dependencies]
rum_framework = "0.0.1"
HTML rendering and json serilization relies on Keats/tera and serde-rs/serde. If you need these features, please also install them on your projects.
use rum_framework::rum;letmut rum_server = rum::new("127.0.0.1",3000);The controller can be implemented by using the function or closure that has argument with &mut RumContext type.
When a response is returned from middleware, following middlewares and controllers will not be executed.
fnverify(c:&mutRumContext){//do stuff
...c.file(201,"test.jpg");// return response with status code and contents.}The response can be generated by following methods of context:
fileread file and return binary data.textreturn plain text.htmlrender html and return html response.jsonreturn json data.
You can also use RumContext to communicate with following controllers, or getting parameters from requests. (e.g. get_request_body)
rum_server.post("/test/a1/:param1/b/:param2/c", |c:&mutRumContext|{let point = Point{x:1,y:2};
c.json(200, serde_json::to_string(&point).unwrap());});rum_server.global_middleware(vec![session, verify]);
rum_server.middleware("/test",vec![is_admin]);use rum_framework::rum;use rum_framework::context::RumContext;use serde::{Serialize,Deserialize};use tera::Context;#[derive(Serialize,Deserialize,Debug)]structPoint{x:i32,y:i32,}#[derive(Serialize,Deserialize,Debug)]structTest{test:String,}fnsession(_:&mutRumContext){println!("called first.");}fnverify(c:&mutRumContext){println!("called second.");let ua = c.get_request_header("User-Agent");if ua.is_none(){// Abort Operation if header is invalid.
c.text(400,"bad request");}}fnis_admin(_:&mutRumContext){println!("verifying permission.");}fnindex(c:&mutRumContext){println!("index executed!");
c.set_response_header("Access-Control-Allow-Origin","http://127.0.0.1:8000");
c.file(201,"test.jpg");}fnmain(){letmut rum_server = rum::new("127.0.0.1",3000);
rum_server.use_html_template("templates/*.html");
rum_server.use_static_assets("static");
rum_server.global_middleware(vec![session, verify]);
rum_server.get("", index);
rum_server.post("/test/a1/:param1/b/:param2/c", |c:&mutRumContext|{let point = Point{x:1,y:2};
c.json(200, serde_json::to_string(&point).unwrap());});
rum_server.get("/test/b1", |c:&mutRumContext|{letmut context = Context::new();
context.insert("val1",&"Hello!");
context.insert("val2",&2023);
c.html(200,"index.html",&context);});
rum_server.middleware("/test",vec![is_admin]);
rum_server.start();}