Skip to content

Instantly share code, notes, and snippets.

@mkhan45
Last active December 10, 2021 21:31
Show Gist options
  • Select an option

  • Save mkhan45/ce5317cd59a25bcb317474866b20b3fb to your computer and use it in GitHub Desktop.

Select an option

Save mkhan45/ce5317cd59a25bcb317474866b20b3fb to your computer and use it in GitHub Desktop.
simple HTTP RustScript endpoint
FROM hexpm/elixir:1.13.0-erlang-24.0.3-alpine-3.14.0
RUN mix local.hex --force
RUN mix local.rebar --force
COPY --from=mkhan45/rustscript /bin/rustscript /bin/rustscript
COPY server.exs .
# hacky way to get elixir deps
RUN sh -c "timeout 30s elixir server.exs"
CMD elixir server.exs
Mix.install([
{:plug_cowboy, "~> 2.0"},
{:temp, "~> 0.4"}
])
defmodule Server do
use Plug.Router
plug(:match)
plug(:dispatch)
post "/" do
case Plug.Conn.read_body(conn) do
{:ok, rsc_str, _} ->
try do
{:ok, path} = Temp.path
{:ok, file_handle} = File.open(path, [:write])
IO.puts(file_handle, rsc_str)
{result, _err_code} = System.cmd("rustscript", [path])
send_resp(conn, 200, result)
rescue
_ -> send_resp(conn, 400, "Server Error")
end
res ->
IO.inspect(res)
send_resp(conn, 400, "Send a RustScript string in the request body")
end
end
match _ do
send_resp(conn, 404, "404")
end
end
Plug.Cowboy.http(Server, [], port: 4000)
System.no_halt(true)
@mkhan45
Copy link
Author

mkhan45 commented Dec 10, 2021

Run with docker run --memory=128m --cpu-shares=256 -p 4000:4000 -t rsc_endpoints

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment