Crates.io | simple_proxy |
lib.rs | simple_proxy |
version | 1.3.4 |
source | src |
created_at | 2018-05-24 13:29:11.094101 |
updated_at | 2021-04-20 11:20:22.285429 |
description | Simple proxy with middlewares, easy to customize, easy to use. |
homepage | https://github.com/terry90/rs-simple-proxy |
repository | https://github.com/terry90/rs-simple-proxy |
max_upload_size | |
id | 66893 |
size | 37,805 |
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;
}
You can create your custom middleware by creating a struct implementing Middleware, consisting of 4 callbacks:
before_request
will be run every timerequest_failure
will be run when the request failsrequest_success
will be run when the request succeeds, you can then handle the response according to the status code or the bodyafter_request
will be run every time