forked from slogsdon/elixir-reverse-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 81a7a9a
Showing
11 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/_build | ||
/deps | ||
erl_crash.dump | ||
*.ez |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ExUnit.start() |