Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

29 Commits

Repository files navigation

Rum Framework

A toy web framework inspired by gin-gonic/gin and expressjs/express.

Installation

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.

Usage

Initialization

use rum_framework::rum;letmut rum_server = rum::new("127.0.0.1",3000);

Defining Controller and Middleware

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:

  • file read file and return binary data.
  • text return plain text.
  • html render html and return html response.
  • json return json data.

You can also use RumContext to communicate with following controllers, or getting parameters from requests. (e.g. get_request_body)

Binding controller with route and http methods

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());});

Binding middlewares to routes

rum_server.global_middleware(vec![session, verify]);
rum_server.middleware("/test",vec![is_admin]);

Sample Code

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();}

About

A simple web framework.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages