A Plug adapter for the Common Gateway Interface, allowing you to use all your favorite middleware and tools with the simplicity of CGI web servers.
See the Plug documentation for more information.
- https://datatracker.ietf.org/doc/html/rfc3875
- https://en.wikipedia.org/wiki/Common_Gateway_Interface
There are more examples in the
cgi-bin
folder.
#!/usr/bin/env elixir
# Install plug_cgi as a dependency
Mix.install([
:plug_cgi
])
# Define your plug
# You can use middleware, routing, or anything else
defmodule MyPlug do
import Plug.Conn
def init(options) do
# initialize options
options
end
def call(conn, _opts) do
conn
|> put_resp_content_type("text/plain")
|> send_resp(200, "Hello world")
end
end
# Run your plug with plug_cgi
Plug.CGI.run MyPlug
The package can be installed by adding plug_cgi
to your list of dependencies
in mix.exs
:
def deps do
[
{:plug_cgi, "~> 0.1.0"}
]
end
You can use plug_cgi
in
single-file Elixir scripts
by adding it to your Mix.install/2
call:
Mix.install([
:plug_cgi,
])
When using single-file scripts with CGI you may need to set your HOME
environment variable. This can be done by editing the shebang line to:
#!/usr/bin/env HOME=/home/user elixir