Crate poem_proxy

Source
Expand description

Poem-proxy is a simple and easy-to-use proxy Endpoint compatible with the Poem Web Framework. It supports the forwarding of http get and post requests as well as websockets right out of the box!

§Table of Contents

§Quickstart

use poem::{get, handler, listener::TcpListener, web::Path, IntoResponse, Route, Server, EndpointExt};
use poem_proxy::{proxy, ProxyConfig};
 
let pconfig = ProxyConfig::new( "localhost:5173" )
    .web_insecure()   // Enables proxy-ing web requests, sets the proxy to use http instead of https
    .ws_insecure()    // Enables proxy-ing web sockets, sets the proxy to use ws instead of wss
    .enable_nesting() // Sets the proxy to support nested routes
    .finish();        // Finishes constructing the configuration
 
let app = Route::new().nest( "/", proxy.data( pconfig ) ); // Set the endpoint and pass in the configuration
 
Server::new(TcpListener::bind("127.0.0.1:3000")).run(app); // Start the server

§Configuration

Configuration of this endpoint is done through the ProxyConfig builder-struct. There are lots of configuration options available, so click that link to learn more about all of them! Below is a brief overview:

use poem_proxy::ProxyConfig;
     
// Configure proxy endpoint, pass in the target server address and port number
let proxy_config = ProxyConfig::new( "localhost:5173" ) // 5173 is for Sveltekit
     
    // One of the following lines is required to proxy web requests (post, get, etc)
    .web_insecure() // http from proxy to server
    .web_secure()   // https from proxy to server
 
    // One of the following lines is required to proxy websockets
    .ws_insecure()  // ws from proxy to server
    .ws_secure()    // wss from proxy to server
 
    // The following option is required to support nesting
    .enable_nesting()
 
    // This returns a concrete ProxyConfig struct to be passed into the endpoint data
    .finish();

§Endpoint

This Endpoint is a very basic but capable proxy. It works by simply accepting web/socket requests and sending its own request to the target. Then, it sends everything it receives from the target to the connected client.

This can be used with poem’s built-in routing. You can apply specific request types, or even use at and nest.

The Quickstart section shows a working example, so this section doesn’t.

Structs§

ProxyConfig
A configuration object that allows for fine-grained control over a proxy endpoint.
proxy
The websocket-enabled proxy handler