Last active
December 10, 2021 21:31
-
-
Save mkhan45/ce5317cd59a25bcb317474866b20b3fb to your computer and use it in GitHub Desktop.
simple HTTP RustScript endpoint
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with
docker run --memory=128m --cpu-shares=256 -p 4000:4000 -t rsc_endpoints