GithubHelp home page GithubHelp logo

cowboy_revproxy's Introduction

cowboy_revproxy

cowboy_revproxy is a simple TCP routing proxy (layer 7) in erlang and using Cowboy connection handling. It lets you configure the routine logic in Erlang.

If you need to proxy connections to different backend servers depending on the contents of the transmission, then cowboy_revproxy will helps you. It's heavily inspidered from proxymachine.

Build

You need rebar to build cowboy_revproxy:

$ rebar get-deps
$ rebar compile 

Usage

The idea is simple, once a request is coming to the proxy the data is passed to a proxy function until this function return a remote connection or tell to the proxy to close the connection.

Valid returns values are :

  • continue or ok -> wait for next chunk
  • stop -> close the connection
  • {stop, Reply} -> send Reply to the client and close the connection
  • {http, Dispatch} -> Use the HTTP protocol of cowboy with the dispatch rules list Dispatch.
  • {remote, Remote} -> return the address of the remote connection to proxy. Remote can be one of the following:
    • {ip, port}
    • {ssl, Ip, Port, Options}, where options are ssl options (keyfile, certfile & password).
  • [{remote, Remote}, {data, Data}] -> same as above, but data passed to the proxy function is replaced by Data and will be send to the remote connection.
  • [{remote, Remote}, {data, Data}, {reply, Reply}] -> same as above but reply Reply to the client.

Example

Here is a simple example of function proxying the port 8080 to google.com:

-module(cowboy_revproxy_example).
-export([start/0, proxy/1]).

proxy(_Data) ->
    {remote, {"www.google.com", 80}}.

start() ->
    application:start(cowboy),
    application:start(cowboy_revproxy),
        cowboy:start_listener(http, 100,
        cowboy_tcp_transport, [{port, 8080}],
        cowboy_revproxy, [{proxy, {?MODULE, proxy}}]).

To test it do, in the source folder:

$ rebar get-deps compile
$ erl -pa ebin -pa deps/cowboy/ebin

Then in the erlang shell:

1> cowboy_revproxy_example:start().

and go on http://127.0.0.1:8080/ , it should display the google page.

This simple proxy function allows you to handle an HTTP request locally

proxy(Data) ->
    Dispatch = [
        %% {Host, list({Path, Handler, Opts})}
        {'_', [{'_', my_handler, []}]}
    ],
    {http, Dispatch}.

A simple "Hello World" HTTP handler:

-module(my_handler).
-behaviour(cowboy_http_handler).
-export([init/3, handle/2, terminate/2]).

init({tcp, http}, Req, Opts) ->
    {ok, Req, undefined_state}.

handle(Req, State) ->
    {ok, Req2} = cowboy_http_req:reply(200, [], <<"Hello World!">>, Req),
    {ok, Req2, State}.

terminate(Req, State) ->
    ok.

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.