Skip to main content

Crate rambl_rs

Crate rambl_rs 

Source
Expand description

A rust Web Framework for prototyping and small scale applications

§Simple Hello World HTTP server

use rambl_rs::{Context, HttpResponse, HttpServer, RequestType::Get, Responder};

async fn index(res: Responder, _ctx: Context) {
    res.respond("<h1>Hello World</h1>").await.unwrap();
}

#[tokio::main]
async fn main() {
    let server = HttpServer::new();

    server.on(Get, "/", index);

    server.listen("127.0.0.1:8000").await.unwrap();
}

This shows that some applications don’t need alot of code, so we go for minimun boilerplate.

§Connection Counter

use rambl_rs::{Context, HttpServer, RequestType::Get, Responder};

async fn index(res: Responder, ctx: Context<i32>) {
    let mut guard = ctx.state.lock().await;
    let counter = guard.as_mut().unwrap();

    *counter += 1;

   res.respond(
        &format!("<h1>You are the {counter} person to view this content</h1>")
   ).await.unwrap();
}

#[tokio::main]
async fn main() {
    let server = HttpServer::new();

    server.manage(0);
    server.on(Get, "/", index);

    server.listen("127.0.0.1:8000").await.unwrap();
}

§Real-Time application

this is where rambl_rs shines.

use rambl_rs::RequestType::Post;
use rambl_rs::{Context, HttpServer, RequestType::Get, Responder};
use rambl_rs::{prelude::*, tokio, SSEResponse};
use tokio::sync::broadcast::{channel, Sender};
use std::sync::Arc;

async fn messages(res: Responder, ctx: Context<Arc<Sender<String>>>) {
    let mut rx = Arc::clone(&ctx.state.unwrap_ref()).subscribe();
    let mut sse_res = SSEResponse::new();

    while let Ok(message) = rx.recv().await {
        sse_res.body(message);
        res.respond(&sse_res).await.unwrap();
    }
}

async fn send(res: Responder, ctx: Context<Arc<Sender<String>>>) {
    let tx = Arc::clone(&ctx.state.unwrap_ref());
    let message = ctx.request.body;

    res.respond("").await.unwrap();
    let _ = tx.send(message);
}

#[tokio::main]
async fn main() {
    let server = HttpServer::new();
    let (tx, _) = channel(1);

    server.manage(Arc::new(tx));
    server.on(Get, "/messages", messages);
    server.on(Post, "/send", send);

    server.listen("127.0.0.1:8000").await.unwrap();
}

Re-exports§

pub use tokio;

Modules§

prelude

Structs§

BinaryResponse
Binary protocol response, can be used for efficient communication Uses the first 8 bytes to encode length as le bytes.
Context
Contains the current context of the request and state.
HttpResponse
Creates a valid http response when being responded with.
HttpServer
The main struct in the crate. Allows you to create HTTP web servers.
RawResponse
Raw response, no hand holding. Not recommended
Request
Contains all the information about the current request
Responder
Responder is the only way to respond to a request. You respond by using the respond method.
SSEResponse
Great for SSE’s, auto appends required headers. However, for custom headers, an HttpResponse is required.

Enums§

Handler
RequestType
Crucial part of the request signature, Explains which type of request should be handled

Traits§

HandlerFunc
Response
If a type has implemented this, it can be thrown down the responder respond method, without having to do any modifications.