1 unstable release

0.1.9 May 29, 2022
0.1.8 May 29, 2022

#25 in #response-headers

23 downloads per month

MIT/Apache

12KB
213 lines

Velen

Just another REST framework

How to use

Import

use velen::server::create_server;
use velen::models::server_models::{Request, Response};

Create server instance

let mut server = create_server();

Register endpoints

server.get("/get", get_handler);
server.post("/add", post_handler);

Start listening

server.listen("127.0.0.1", 3333, server_start_handler);

Example handlers

fn get_handler(_: Request, mut res: Response) {
    res.set_status_code(200);
    res.set_header("Content-Type", "application/json");
    struct GoodResponse {
        message: String,
    }
    impl Display for GoodResponse {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{{\"message\": \"{}\"}}", self.message)
        }
    }
    let good_response = GoodResponse {
        message: "I'm good, thank you very much Sir!".to_string(),
    };
    res.send(good_response.to_string());
}

Read Query Params

req.query_params.get("user_id").unwrap();

Read Request Header

req.headers.get("x-custom-header").unwrap();

Read Request Body

 /* Velen does not deserialize request payload. Use of Serde is recommended. */

 req.body;

Set Response Status

res.set_status_code(200);

Set Response header

res.set_header("Content-Type", "application/json");

Set Response payload

res.send("{\"status\":\"ok\"}");

Limitations

  • OPTIONS not implemented
  • Only application/json is supported, so don't try to upload files
  • During sending a response set_status_code has to be called before any set_header. That is how response is actually sent to client.
  • No multithreading

TODO

  • Fix above limitations

Dependencies

~2.2–3MB
~55K SLoC