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

Overview

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;

let mut 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.

fn verify(c: &mut RumContext){
    //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:&mut RumContext|{ 
    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)]
struct Point {
    x: i32,
    y: i32,
}

#[derive(Serialize, Deserialize, Debug)]
struct Test {
    test: String,
}

fn session(_: &mut RumContext){
    println!("called first.");
}

fn verify(c: &mut RumContext){
    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");
    }
}

fn is_admin(_: &mut RumContext){
    println!("verifying permission.");
}

fn index(c: &mut RumContext){
    println!("index executed!");
    c.set_response_header("Access-Control-Allow-Origin", "http://127.0.0.1:8000");
    c.file(201, "test.jpg");
}

fn main() {
    let mut 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:&mut RumContext|{ 
        let point = Point { x: 1, y: 2 };
        c.json(200, serde_json::to_string(&point).unwrap());
     });
    rum_server.get("/test/b1", |c: &mut RumContext|{ 
        let mut 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();
}
You might also like...
Handlebars middleware for Iron web framework

handlebars-iron Handlebars middleware for the Iron web framework. This library, together with handlebars, iron and hyper, works on both stable and nig

The light web framework for Rust.
The light web framework for Rust.

Rusty Web Rusty web is a simple to use, fully customizable lightweight web framework for rust developers. Learn rusty web Installation [dependencies]

A blazingly fast static web server with routing, templating, and security in a single binary you can set up with zero code. :zap::crab:
A blazingly fast static web server with routing, templating, and security in a single binary you can set up with zero code. :zap::crab:

binserve ⚡ 🦀 A blazingly fast static web server with routing, templating, and security in a single binary you can set up with zero code. 🔥 UPDATE: N

Simple and fast web server

see Overview Simple and fast web server as a single executable with no extra dependencies required. Features Built with Tokio and Hyper TLS encryption

A starter template for actix-web projects that feels very Django-esque. Avoid the boring stuff and move faster.

Jelly A.K.A, the actix-web starter you probably wish you had. This is provided as-is, and anyone is free to extend it or rework it as they desire - ju

Add Facebook and Google authentication to your HTTP REST API in Actix-web

I created this project while learning Rust. Project shows how to handle Facebook and Google token verification in Rust using Actix-Web. Hope this help

Actix-extras - A collection of additional crates supporting the actix and actix-web frameworks.

actix-extras A collection of additional crates supporting the actix-web and actix frameworks. Crates by @actix Crate actix-cors Cross-origin resource

Operator is a web server. You provide a directory and Operator serves it over HTTP.
Operator is a web server. You provide a directory and Operator serves it over HTTP.

Operator Operator is a web server. You provide a directory and Operator serves it over HTTP. It serves static files the way you'd expect, but it can a

In-progress extractors and middleware for Actix Web

actix-web-lab Experimental extractors, middleware, and other extras for possible inclusion in Actix Web. Things To Know About This Crate It will never

Owner
Hidetomo Kou(YingZhi "Harrison" Huang)
Hidetomo Kou(YingZhi
Static Web Server - a very small and fast production-ready web server suitable to serve static web files or assets

Static Web Server (or SWS abbreviated) is a very small and fast production-ready web server suitable to serve static web files or assets.

Jose Quintana 496 Jan 2, 2023
Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.

Actix Web Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust Features Supports HTTP/1.x and HTTP/2 Streaming and pipelining

Actix 16.3k Jan 8, 2023
A flexible web framework that promotes stability, safety, security and speed.

A flexible web framework that promotes stability, safety, security and speed. Features Stability focused. All releases target stable Rust. This will n

Gotham 2.1k Jan 3, 2023
Sincere is a micro web framework for Rust(stable) based on hyper and multithreading

The project is no longer maintained! Sincere Sincere is a micro web framework for Rust(stable) based on hyper and multithreading. Style like koa. The

null 94 Oct 26, 2022
Salvo is a powerful and simplest web server framework in Rust world

Salvo is an extremely simple and powerful Rust web backend framework. Only basic Rust knowledge is required to develop backend services.

Salvo 1.2k Jan 5, 2023
An Extensible, Concurrent Web Framework for Rust

Iron Extensible, Concurrency Focused Web Development in Rust. Response Timer Example Note: This example works with the current iron code in this repos

null 6.1k Dec 27, 2022
A web framework for Rust.

Rocket Rocket is an async web framework for Rust with a focus on usability, security, extensibility, and speed. #[macro_use] extern crate rocket; #[g

Sergio Benitez 19.5k Jan 8, 2023
A lightweight web framework built on hyper, implemented in Rust language.

Sapper Sapper, a lightweight web framework, written in Rust. Sapper focuses on ergonomic usage and rapid development. It can work with stable Rust. Sa

Daogang Tang 622 Oct 27, 2022
Web framework in Rust

Rouille, a Rust web micro-framework Rouille is a micro-web-framework library. It creates a listening socket and parses incoming HTTP requests from cli

Pierre Krieger 840 Jan 1, 2023
A fast, boilerplate free, web framework for Rust

Tower Web A web framework for Rust with a focus on removing boilerplate. API Documentation Tower Web is: Fast: Fully asynchronous, built on Tokio and

Carl Lerche 969 Dec 22, 2022