1 unstable release

0.1.2 May 20, 2023

#735 in HTTP server

MIT license

13KB
246 lines

Jast

Jast is an open source minimalist Rust library for building high-performance web servers. Designed to be both easy to use and performant, Jast takes advantage of Rust's strengths in areas such as security and memory usage.

Jast (versión: 0.1.2)

Install

Cargo.toml
[dependencies]
jast = "0.1.2"

Setup

Basic server

fn controller() -> RouteResponse<'static> {
        let response = RouteResponse {
            method: "GET",
            res: Res::json(vec![
                ("name", DataTypes::Str(String::from("Hello"))), 
                ("lastname", DataTypes::Str(String::from("World"))), 
                ("id", DataTypes::Int(i32::from(1)))
            ])
        };

        response
}

let routes = vec![
        Http::route("/", controller())
];

let settings = Builder::new("localhost:8080", routes);

Http::create_server(
   settings
)

Server settings

By default it uses the number of cpu cores to determine the number of worker threads. This can be changed as follows:

let settings = Builder::new("localhost:8080", routes).worker_threads(4);

Usage

new route

Struct of Routes to create new routes

pub struct Routes<'a> {
    pub method: &'a str, 
    pub route: &'a str,
    pub response: &'a str,
}
Http::route(method, route, response)

response

Json:

Res::json(vec![((name, value)))])

struct of DataTypes to value

Res::json(vec![("name", DataTypes::Str(String::from("Hello")))])

File: from the root of the project

Http::route("/html", (|| RouteResponse { method: "GET", res: "src/index.html"})()) 

Example

use jast::{Http, Res, DataTypes, RouteResponse, Builder};

fn main() {
    fn controller() -> RouteResponse<'static> {
        let response = RouteResponse {
            method: "GET",
            res: Res::json(vec![
                ("name", DataTypes::Str(String::from("Juan"))), 
                ("lastname", DataTypes::Str(String::from("Alberto"))), 
                ("id", DataTypes::Int(i32::from(1)))
            ])
        };

        response
    }

    let routes = vec![
        Http::route("/", controller()),
        Http::route("/hola", 
            (|| RouteResponse { method: "GET", res: Res::json(vec![("name", DataTypes::Str("Juan".to_string()))]) })()),
        Http::route("/html", 
            (|| RouteResponse { method: "GET", res: "src/index.html"})())
    ];


    let settings = Builder::new("localhost:8080", routes).worker_threads(4);

    Http::create_server(
       settings
    )
}

Contribute

Dependencies

~1.4–2.3MB
~40K SLoC