#web-server #coroutine #friendly #nano #response

server_nano

A fast and lightweight HTTP server implementation in Rust

8 releases

0.2.1 Apr 4, 2024
0.2.0 Apr 4, 2024
0.1.5 Apr 11, 2023

#213 in HTTP server

Download history 1/week @ 2024-02-22 1/week @ 2024-02-29 42/week @ 2024-03-07 20/week @ 2024-03-14 4/week @ 2024-03-28 162/week @ 2024-04-04 6/week @ 2024-04-11

172 downloads per month

MIT license

34KB
870 lines

server_nano

A tiny, fast, and friendly web server written in rust and inspired by express. It uses may to coroutines and is one of the fastest (unix) servers today.

Usage

First, add this to your Cargo.toml:

[dependencies]
server_nano = "0.1.4"

Now, you can write you server

use server_nano::{json, Server};

fn main() {
    let mut app = Server::new();

    app.get("/", |_, res| res.send("welcome to home page!"));

    app.get("/user/:id", |req, res| {
        let user_id = req.parameter("id").unwrap();
        let json_value = json!({ "username": user_id });
        res.json(&json_value)
    });

    app.get("/product/:name", |req, res| {
        let product_name = req.parameter("name").unwrap();
        let message = &format!("Welcome to product page of product: {}", product_name);
        res.send(message)
    });

    app.post("/test", |_, res| res.send("test!"));

    app.post("/settings", |req, res| {
        let json_body = req.json_body().unwrap();

        let response = json!({
            "success": true,
            "message": "Settings updated successfully",
            "body": json_body
        });
        res.json(&response)
    });

    app.listen("127.0.0.1:8080").unwrap();
}

Dependencies

~4–34MB
~469K SLoC