Re:Aino, an experimental HTTP framework for Elixir
by travisgriggs on Monday September 20, 2021 @12:48AM
(#28590469)
Would this be better/simpler for API/endpoints?
[ Parent
| on HN ]
Re:Aino, an experimental HTTP framework for Elixir
by peoplefromibiza on Monday September 20, 2021 @07:39AM
(#28592673)
I think for simple API endpoint Plug is still the best option
A simple example
defmodule AddressParser.PlugHandler do
import Plug.Conn
require Logger
def init(options) do
Logger.info("Running #{__MODULE__} with Plug using http://0.0.0.0:8080")
options
end
def call(
%Plug.Conn{method: method, request_path: request_path, query_string: query_string} = conn,
_opts
) do
request_id = generate_request_id()
Logger.info("#{request_id} #{method} #{request_path} #{query_string}")
case handle_request(method, request_path, URI.decode_query(query_string)) do
{:ok, response} ->
Logger.info("#{request_id} sent 200 ok")
conn
|> put_resp_content_type("application/json")
|> send_resp(200, Jason.encode!(response))
{:error, status, response} ->
Logger.error("#{request_id} sent #{status} #{response}")
conn
|> put_resp_content_type("text/html")
|> send_resp(status, response)
end
end
defp handle_request("GET", "/parse", %{"address" => address}) do
{:ok, Address.parse(address)}
end
# more handlers
...
end