PhoenixCGI v0.1.0 PhoenixCGI View Source

PhoenixCGI allows usage of small CGI scripts as Phoenix plug.

Setup

Add PhoenixCGI to your deps in mix.exs:

defp deps do
  [
    {:phoenix_cgi, "~> 0.1"},
    ...
  ]
end

then, and that's really important, in your endpoint.ex, add the following body_reader in your plug Plug.Parsers:

plug Plug.Parsers,
  body_reader: {PhoenixCGI.CacheRawBody, :read_body, []},
  ...

This will cause the body of your request to be saved in a private field, so that we will be able to pass it to the CGI scripts.

Usage

You will then be able to use PhoenixCGI as any controller. For instance, if you want to serve git-http-backend on a /git/ to serve all repositories in /opt/git/repos/, you can setup as follows:

match :*, "/git/*path", PhoenixCGI,
  binary: "/usr/lib/git-core/git-http-backend",
  extra_env: %{
    GIT_PROJECT_ROOT: "/opt/git/repos/",
    GIT_HTTP_EXPORT_ALL: "1"
  }

You can also define a plug that will define, for instance, which project to serve, with assign/3.

defp set_repo(conn) do
  assign(conn, :extra_env, %{
    GIT_PROJECT_ROOT: "/opt/git/repos/demo.git",
    GIT_HTTP_EXPORT_ALL: "1"
  })
end

and then use it

pipeline :git do
  plug :set_repo
end

scope "/" do
  pipe_through :git

  match :*, "/git/*path", PhoenixCGI,
    binary: "/usr/lib/git-core/git-http-backend"
end

Link to this section Summary

Functions

Serves the CGI script given by :binary. The following paramters are available

Prepares the parameters

Link to this section Functions

Serves the CGI script given by :binary. The following paramters are available:

Field nameDescriptionTypeDefault
binaryThe binary to use as CGI scriptstringrequired
argsThe arguments to pass to the scriptlist of string[]
dirThe path to execute the script instringFile.cwd!()
extra_envSupplementary environment variablesmap%{}
path_infoPATH_INFO supplied to the script. Can be {:set, path} for a fixed path, {:assign, key} if it is assign/3ed by a plug, or {:param, name} for a value that is set in the path parameters.{:set, path}, {:assign, key} or {:param, name}{:param, "path"}
timeoutAfter how long should the script be stoppedinteger or :infinity:infinity

these can either be set when calling the controller or in a plug, with assign/3, with exactly the same constraints.

Prepares the parameters