Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
slogsdon committed Aug 9, 2015
0 parents commit 81a7a9a
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/_build
/deps
erl_crash.dump
*.ez
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Shane Logsdon

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ReverseProxy

A Plug based, reverse proxy server.

Upstream servers can be listed per-domain in the following forms:

- List of remote nodes, e.g. `["host:4000", "host:4001"]`
- A `{plug, options}` tuple, useful for umbrella applications

## License

ReverseProxy is released under the MIT License.

See [LICENSE](https://github.com/slogsdon/elixir-reverse-proxy/blob/master/LICENSE) for details.
10 changes: 10 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use Mix.Config

config :logger, :console,
level: :info,
format: "$date $time [$level] $metadata$message\n",
metadata: [:user_id]

config ReverseProxy,
upstreams: %{ "" => ["localhost:4000"],
"api." => ["localhost:4001"] }
19 changes: 19 additions & 0 deletions lib/reverse_proxy.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule ReverseProxy do
use Application

# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec, warn: false

children = [
# Define workers and child supervisors to be supervised
# worker(ReverseProxy.Worker, [arg1, arg2, arg3])
]

# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: ReverseProxy.Supervisor]
Supervisor.start_link(children, opts)
end
end
23 changes: 23 additions & 0 deletions lib/reverse_proxy/router.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule ReverseProxy.Router do
use Plug.Router

plug :match
plug :dispatch

for {host, servers} <- Application.get_env(ReverseProxy, :upstreams, []) do
@servers servers
match _, host: host do
upstream = fn conn ->
ReverseProxy.Runner.retreive(conn, @servers)
end

if Application.get_env(ReverseProxy, :cache, false) do
ReverseProxy.Cache.serve(conn, upstream)
else
upstream.(conn)
end
end
end

match _, do: conn |> send_resp(400, "")
end
47 changes: 47 additions & 0 deletions lib/reverse_proxy/runner.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule ReverseProxy.Runner do
@type upstream :: [String.t] | {Atom.t, Keyword.t}

@spec retreive(Plug.Conn.t, upstream) :: Plug.Conn.t
def retreive(conn, upstream)
def retreive(conn, {plug, opts}) when plug |> is_atom do
options = plug.init(opts)
plug.call(conn, options)
end
def retreive(conn, servers) do
server = upstream_select(servers)
{method, url, body, headers} = prepare_request(server, conn)

HTTPoison.request(method, url, body, headers, timeout: 5_000)
|> process_response(conn)
end

defp prepare_request(server, conn) do
conn = conn |> Plug.Conn.put_req_header("x-forwarded-for", conn.remote_ip)
method = conn.method |> String.downcase |> String.to_atom
url = "#{conn.scheme}://#{server}#{conn.request_path}?#{conn.query_string}"
headers = conn.req_headers
{:ok, body, _conn} = Plug.Conn.read_body(conn)

{method, url, body, headers}
end

defp process_response({:error, _}, conn) do
conn |> Plug.Conn.send_resp(502, "Bad Gateway")
end
defp process_response({:ok, response}, conn) do
conn
|> put_resp_headers(response.headers)
|> Plug.Conn.send_resp(response.status_code, response.body)
end

defp put_resp_headers(conn, []), do: conn
defp put_resp_headers(conn, [{header, value}|rest]) do
conn
|> Plug.Conn.put_resp_header(header, value)
|> put_resp_headers(rest)
end

defp upstream_select(servers) do
servers |> hd
end
end
51 changes: 51 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
defmodule ReverseProxy.Mixfile do
use Mix.Project

def project do
[app: :reverse_proxy,
version: "0.0.1",
elixir: "~> 1.0",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps,
name: "ReverseProxy",
description: description,
package: package,
docs: [readme: "README.md", main: "README"],
test_coverage: [tool: ExCoveralls]]
end

def application do
[applications: [:logger, :plug, :cowboy, :httpoison],
mod: {ReverseProxy, []}]
end

defp deps do
[{:plug, "~> 0.14.0"},
{:cowboy, "~> 1.0.2"},
{:httpoison, "~> 0.7.1"},

{:earmark, "~> 0.1.17", only: :docs},
{:ex_doc, "~> 0.7.3", only: :docs},

{:excoveralls, "~> 0.3.11", only: :test},
{:dialyze, "~> 0.2.0", only: :test}]
end

defp description do
"""
A Plug based, reverse proxy server.
Upstream servers can be listed per-domain in the following forms:
- List of remote nodes, e.g. `["host:4000", "host:4001"]`
- A `{plug, options}` tuple, useful for umbrella applications
"""
end

defp package do
%{contributors: ["Shane Logsdon"],
licenses: ["MIT"],
links: %{"GitHub" => "https://github.com/slogsdon/elixir-reverse-proxy"}}
end
end
8 changes: 8 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%{"cowboy": {:hex, :cowboy, "1.0.2"},
"cowlib": {:hex, :cowlib, "1.0.1"},
"hackney": {:hex, :hackney, "1.3.0"},
"httpoison": {:hex, :httpoison, "0.7.1"},
"idna": {:hex, :idna, "1.0.2"},
"plug": {:hex, :plug, "0.14.0"},
"ranch": {:hex, :ranch, "1.1.0"},
"ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5"}}
7 changes: 7 additions & 0 deletions test/reverse_proxy_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule ReverseProxyTest do
use ExUnit.Case

test "the truth" do
assert 1 + 1 == 2
end
end
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit 81a7a9a

Please sign in to comment.