velen

Crates.iovelen
lib.rsvelen
version0.1.9
sourcesrc
created_at2022-05-20 16:54:41.310989
updated_at2022-05-29 07:46:30.681478
descriptionA simple package for making REST APIs
homepagehttps://github.com/iamparnab/velen
repository
max_upload_size
id590380
size11,258
Parnab Sanyal (iamparnab)

documentation

https://github.com/iamparnab/velen/blob/master/README.md

README

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
Commit count: 0

cargo fmt