blink 0.1.1

Fast, micro and reliable microservice library
Documentation
  • Coverage
  • 0%
    0 out of 24 items documented0 out of 16 items with examples
  • Size
  • Source code size: 35.36 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.01 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 49s Average build duration of successful builds.
  • all releases: 49s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • AhmedMostafa16

Blink

Fast, tiny and reliable microservice library.

About

Blink is a small low level wrapper around Hyper. It aims to take in few if any dependencies and acts as a basic handler for incoming requests. It isn't meant to be used for your website, if you need that look into something like Rocket or Gotham. Instead it's used for microservices, where routing would be useful, but large web frameworks would take up to many resources.

This means you can still write code for your microservice to do what they need and not have to worry about setting up all the nuts and bolts for handling incoming requests or mucking around with low level HTTP internals.

Build requirements

You only need a stable version of the Rust compiler. Due to the use of the ? operator only versions 1.15 and up of rustc are supported.

How to use the library

Put the following in Cargo.toml of your project:

[dependencies]

blink = "0.1"

Then import the crate with:

#[macro_use] extern crate blink;
use blink::*;

in your crate root. From here you can set up your routes for dealing with incoming requests.

Example

The following bit of code creates a simple echo server:

#[macro_use] extern crate blink;
// Imports traits and the reexported hyper and futures crates
use blink::*;
use futures::future::ok;
use hyper::header::ContentLength;
static INDEX: &'static [u8] = b"Try POST /echo\n";
fn main() {
    // Set up the routes and run it.
    // You can set the port as well with a function call
    // before run() to port() by default it runs on 7878
    Blink::new().routes(router! {
        ("/", GetEcho)
        ("/echo", GetEcho)
        ("/echo", PostEcho)
    }).run().unwrap();
}
routes!(
    // Each route handler needs 3 things:
    // 1) Takes a request verb needed for routes that use this:
    //    Post, Put, Patch, Get, or Delete
    // 2) A name for the handler type, in this case PostEcho
    // 3) A closure. Closures take a hyper::server::Request type and returns a
    //    futures::future::FutureResult<hyper::server::Response, hyper::Error>;
    //    These types are automatically imported in the routes macro (except for
    //    hyper::Error) to reduce what things you need to import
    (Post, PostEcho, |req: Request| {
        let mut res = Response::new();
        if let Some(len) = req.headers().get::<ContentLength>() {
            res.headers_mut().set(len.clone());
        }
        ok(res.with_body(req.body()))
    })
    (Get, GetEcho, |_| {
        ok(Response::new()
                    .with_header(ContentLength(INDEX.len() as u64))
                    .with_body(INDEX))
    })
);

After starting this server up if you run the following commands you can see that all the routes were implemented!

% curl localhost:6767

Try POST /echo

% curl localhost:6767/echo

Try POST /echo

% curl -X POST -d 'Hello!' localhost:6767/echo

Hello!