#middleware #proxy #logging #routes #router #environment #port

simple_proxy

Simple proxy with middlewares, easy to customize, easy to use

13 releases (7 stable)

1.3.4 Apr 20, 2021
1.3.1 Jun 18, 2020
1.2.1 Jun 13, 2019
1.1.0 Feb 13, 2019
0.1.0 May 24, 2018

#59 in #router

Download history 11/week @ 2024-06-15 3/week @ 2024-06-22 14/week @ 2024-07-20 99/week @ 2024-07-27 126/week @ 2024-09-28

126 downloads per month

Apache-2.0

27KB
669 lines

Simple proxy

Usage

use simple_proxy::middlewares::{router::*, Logger};
use simple_proxy::{Environment, SimpleProxy};

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
struct Cli {
    port: u16,
}

#[derive(Debug, Clone)]
pub struct Config();

impl RouterConfig for Config {
    fn get_router_filename(&self) -> &'static str {
        "routes.json"
    }
}

#[tokio::main]
async fn main() {
    let args = Cli::from_args();

    let mut proxy = SimpleProxy::new(args.port, Environment::Development);
    let logger = Logger::new();
    let router = Router::new(&Config());

    // Order matters
    proxy.add_middleware(Box::new(router));
    proxy.add_middleware(Box::new(logger));

    // Start proxy
    let _ = proxy.run().await;
}

Custom middleware

You can create your custom middleware by creating a struct implementing Middleware, consisting of 4 callbacks:

  • before_request will be run every time
  • request_failure will be run when the request fails
  • request_success will be run when the request succeeds, you can then handle the response according to the status code or the body
  • after_request will be run every time

For more info, see a default middleware

Dependencies

~7–16MB
~207K SLoC