Giter Site home page Giter Site logo

hyper-fast's Introduction

hyper-fast

Hyper and rust based very fast HTTP Web framework (much faster than actix and other frameworks).

Features

  • Supports brotli, deflate and gzip encoding for request and response
  • In-built access logs and metrics for APIs
  • Simple APIs to get current metrics - in JSON and Prometheus format
  • In-built OOR (Out of rotation API) to take server out of rotation
  • In-built Server Health API
  • Very simple and fast match pattern based routing.
  • Much faster than actix and other web servers out there.
  • Support for optional daemon service that gets started on server start and stopped on server shutdown
  • In-built server shutdown handling.

Example

Look at examples/example_server.rs for a working example. Example can be run with cargo run --example example_server

  1. Define a service class, implement Service trait for api routing.
pub struct ExampleService {
    // any service level properties
}

#[async_trait]
impl Service for ExampleService {
    async fn api_handler<'a>(
        &'a self,
        _: Body,
        route: &HttpRoute<'a>,
        path: &[&str],
    ) -> Result<Response<Body>, ApiError> {
        match path {
            ["test"] if matches!(route.method, &http::Method::GET) => {
                self.get_test(route).await
            }
            _ => HttpResponse::not_found(route.path),
        }
    }
}

impl ExampleService {
    pub async fn get_test(&self, route: &HttpRoute<'_>) -> Result<Response<Body>, ApiError> {
        HttpResponse::string(route, "GET::/api/test - test passed".to_string())
    }
}
  1. Optional service daemon, could be a dummy implementation - if one doesn't need it.
pub struct ExampleServiceDaemon {}

#[async_trait]
impl ServiceDaemon<ExampleService> for ExampleServiceDaemon {
    async fn start(&self, _service: Arc<ExampleService>) {
        //no impl for now.
    }
}
  1. Implement ServiceBuilder trait
pub struct ExampleServiceBuilder {
    // any service builder level properties
}

#[async_trait]
impl ServiceBuilder<ExampleService, ExampleServiceDaemon> for ExampleServiceBuilder {
    async fn build(self) -> anyhow::Result<(ExampleService, Option<ExampleServiceDaemon>)> {
        let service = ExampleService {};

        Ok((service, None))
    }
}
  1. Invoke start_http_server in your main method.
#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<(), anyhow::Error> {
    load_config("examples/config", "dev")?;
    setup_logging("examples/config/log4rs.yml")?;

    start_http_server("127.0.0.1:6464", ExampleServiceBuilder {}).await
}

APIs

  1. /oor - switches the in-rotation status of server
  2. /status - gives in-rotation status of server
  3. /metrics/json - metrics in JSON format
  4. /metrics/prometheus - metrics in Prometheus format
  5. /api/<your-api-routes> - all your api routes are after /api

hyper-fast's People

Contributors

snow01 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.