Last active
October 18, 2017 16:30
-
-
Save thbar/704ccdf36f5cbcee6b45ece9ab35fb21 to your computer and use it in GitHub Desktop.
Start a small Elixir server from a script, then query it from inside the same script
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
| # | |
| # to run and restart continuously when the code changes: | |
| # | |
| # find scripts lib mix.* | entr -rc mix run --no-halt scripts/plug_demo.exs | |
| # | |
| defmodule MySimpleServerPlug do | |
| # https://hexdocs.pm/plug/Plug.Conn.html | |
| import Plug.Conn | |
| # https://hexdocs.pm/plug/Plug.Conn.html | |
| def init(options) do | |
| options | |
| end | |
| # https://hexdocs.pm/plug/Plug.Conn.html | |
| def call(conn, _options) do | |
| conn | |
| # https://hexdocs.pm/plug/Plug.Conn.html#put_resp_content_type/3 | |
| |> put_resp_content_type("text/plain") | |
| # https://hexdocs.pm/plug/Plug.Conn.html#send_resp/1 | |
| |> send_resp(200, "Hello!") | |
| end | |
| end | |
| # https://hexdocs.pm/plug/Plug.Adapters.Cowboy.html | |
| {:ok, _pid} = Plug.Adapters.Cowboy.http MySimpleServerPlug, [] | |
| # https://hexdocs.pm/httpotion/HTTPotion.html#get/2 | |
| # https://hexdocs.pm/httpotion/HTTPotion.html#request/3 | |
| response = HTTPotion.get! "http://localhost:4000" | |
| 200 = response.status_code | |
| "Hello!" = response.body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment