View Source Proxy (ex_proxy v0.0.1)

Proxy is a Plug module that allows you to forward requests to another URL while preserving subpaths.

Example Usage

defmodule MyRouter do
  use Plug.Router

  import Proxy

  plug :match
  plug :dispatch

  proxy "/path", to: "https://localhost:8080"

  match _ do
    send_resp(conn, 404, "Oops!")
  end
end

The proxy/2 macro is used to forward requests from /path to https://localhost:8080.

Summary

Functions

Handles the forwarding of requests to the specified target URL. It preserves the subpaths of the original request and forwards the request to the target URL, including the request body, headers, and method.

Initializes the Proxy plug with the target URL.

Defines a macro to simplify request forwarding. It forwards all requests matching the specified path to the given target URL, preserving subpaths.

Functions

Handles the forwarding of requests to the specified target URL. It preserves the subpaths of the original request and forwards the request to the target URL, including the request body, headers, and method.

The Content-Type header from the target URL response is also preserved.

Parameters

  • conn: The connection struct (Plug.Conn).
  • to: The target URL to which requests are forwarded.

Example

proxy "/path", to: "https://localhost:8080"

This forwards /path/resource to https://localhost:8080/resource.

Initializes the Proxy plug with the target URL.

Parameters

  • to: The target URL to which requests will be forwarded.

Returns the target URL, which is passed to the call/2 function.

Link to this macro

proxy(path, opts)

View Source (macro)

Defines a macro to simplify request forwarding. It forwards all requests matching the specified path to the given target URL, preserving subpaths.

Parameters

  • path: The path to match and forward (e.g., "/path").
  • opts: Options, including the :to key, which specifies the target URL.

Example

proxy "/api", to: "https://api.example.com"

This forwards requests like /api/resource to https://api.example.com/resource.