Giter VIP home page Giter VIP logo

main_proxy's Introduction

MainProxy

Route requests to other Phoenix Endpoints or Plugs with WebSocket support.

This library is useful for Gigalixir, Render, Heroku or other deployments where only one web port is exposed.

Installation

Add MainProxy to your list of dependencies in mix.exs.

If you are running an umbrella project, adding MainProxy as a dependency at the root mix.exs won't work. Instead, either add it to one of your child apps or create a new child app solely for the proxy.

def deps do
  [
    {:main_proxy, "~> 0.1"},
  ]
end

Usage

Configure listening options for MainProxy:

config :main_proxy,
  http: [port: 4080],
  https: [port: 4443]

Create a proxy module which configures backends:

defmodule MyApp.Proxy do
  use MainProxy.Proxy

  @impl MainProxy.Proxy
  def backends do
    [
      %{
        domain: "my-cool-app.com",
        phoenix_endpoint: MyAppWeb.Endpoint
      },
      %{
        domain: "members.my-cool-app.com",
        phoenix_endpoint: MyAppMembersWeb.Endpoint
      },
      %{
        verb: ~r/get/i,
        path: ~r{^/main-proxy-plug-test$},
        plug: MainProxy.Plug.Test,
        opts: [1, 2, 3]
      }
    ]
  end
end

Backends can also be configured via configuration:

config :main_proxy,
  http: [port: 4080],
  https: [port: 4443],
  backends: [
    # ...
  ]

But, it's not the recommended way.

Add above created proxy module to the supervision tree:

children = [
  # ... other children
  MyApp.Proxy,
]

Configure all endpoints to not start a server in order to avoid endpoints bypassing MainProxy:

# ...
config :my_app, MyAppWeb.Endpoint, server: false
config :my_app_members, MyAppMembersWeb.Endpoint, server: false

Available Configuration Options

  • :http - the configuration for the HTTP server. It accepts all options as defined by Plug.Cowboy.
  • :https - the configuration for the HTTPS server. It accepts all options as defined by Plug.Cowboy.
  • :server - true by default. If you are running application with mix phx.server, this option is ignored, and the server will always be started.
  • :backends - the rule for routing requests:
    • :domain
    • :verb
    • :host
    • :path
    • :phoenix_endpoint / :plug
    • :opts - only for :plug
  • :log_requests - true by default. Log the requests or not.

How does MainProxy work?

  1. MainProxy starts a Cowboy server with a single dispatch handler: MainProxy.Cowboy2Handler.
  2. The handler checks the verb, host and path of the request, and compares them to the supplied configuration to determine where to route the request.
  3. If the backend that matched is a phoenix_endpoint, MainProxy delegates to the Phoenix.Endpoint.Cowboy2Handler with your app's Endpoint.
  4. If the backend that matched is a plug, MainProxy calls the plug as normal.
  5. If no backend is matched, a text response with a status code of 404 is returned.

Development

mix run --no-halt
curl -i foo.com.127.0.0.1.xip.io:4080
curl -i localhost:4080

Thanks

This library is based on:

main_proxy's People

Contributors

axelson avatar jesseshieh avatar davidoliver avatar ryanwinchester avatar thefirstavenger avatar c4710n 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.