Ally with an Elixir Problem

I am using elli https://github.com/knutin/elli in my Elixir app.

An example code is given below. It is quite simple, every time it starts, I get only "Internal server error". I'm not sure what I'm doing wrong. Can anyone help?

defmodule Client.TestHttp do
  @behaviour :elli_handler

  def handle(_Req,_Args) do
    handle(:elli_request.method(Req), :elli_request.path(Req),Args)
  end

  def handle_event(_,_,_)do
    :ok
  end

  def handle(:GET,[],_)do
    {:ok, [], "OkGet"}
  end
end

This is how I perform

{:ok,pid}=:elli.start_link [callback: Client.TestHttp, port: 2020]
+4
source share
3 answers

It looks like you haven't completely translated the Erlang code from the example and still use variables starting with an uppercase letter.

Change handle to:

  def handle(req, args) do
    handle(:elli_request.method(req), :elli_request.path(req), args)
  end

. Elixir snake_case CamelCase. (_), _var , , .

Req Args - Elixir, :

Req == :'Elixir.Req' # true
+4

@Gazier ( , ), -. :

def handle(_Req,_Args) do
    handle(:elli_request.method(Req), :elli_request.path(Req),Args)
end

:elli_request.method _Req, :elli_request.path _Req _Args. , , ( ), :

 def handle(_Req,_Args) do
        handle(:elli_request.method(_Req), :elli_request.path(_Req),_Args)
    end

, .

+1

, knutin/elli Elixir. , , elixir.

  • @behaviour :elli_handler

    /. :

    # lib/elli_handler.ex
    defmodule ElliHandler do
      @behaviour :elli_handler
      alias :elli_request, as: Request
    
      def handle(req, args) do
        handle(Request.method(req), Request.path(req), req, args)
      end
    
      def handle(:GET, _, req, args) do
        # do something with the request.
        # e.g. you can use Request.get_arg/2 to fetch a query param
        say = Request.get_arg("say", req)
        # Return a tuple with 3 elements
        # status code, list of header tuples, response body
        {200, [], "echo, #{say}"}
      end
    
      # Here you can react to all kinds of events of the full connection/request/response cycle
      # You must implement this, otherwise elli wont function properly, as it evaluates
      # the return value of handle_event/3.
      def handle_event(event, args, config) do
        # Here would be a good point to handle logging.
        # IO.inspect([event, args, config])
        :ok
      end
    
    end
    
  • , elli

    # lib/elli_supervisor.ex
    defmodule ElliSupervisor do
      use Supervisor
    
      def start_link(ref, options) do
        Supervisor.start_link(__MODULE__, options, name: ref)
      end
    
      def init(options) do
        children = [
          worker(:elli, [options], id: :elli_http_server)
        ]
        supervise(children, strategy: :one_for_one)
      end
    
      def shutdown(ref) do
        case Supervisor.terminate_child(ref, :elli_http_server) do
          :ok -> Supervisor.delete_child(ref, :elli_http_server)
          err -> err
        end
      end
    end
    
    # lib/app.ex
    defmodule App do
      use Application
    
      def start(_type, _args) do
        import Supervisor.Spec, warn: false
        # lets start our elli supervisor, setting its options
        # callback should be set to the elli handler that implements the elli_handler behaviour
        # port will be the port elli is listening on, defaults to 8080
        ElliSupervisor.start_link(__MODULE__, callback: ElliHandler, port: 3000)
      end
    
    end
    
    # in mix.exs
    def application do
      [
        mod: {App, []}
      ]
    end
    
  • elli mix.exs

    mix get.deps, . mix run --no-halt , iex -S mix.

    # in mix.exs
    defp deps do
      [
        # elli is our web server layer
        {:elli, github: "knutin/elli"}
      ]
    end
    
0

Source: https://habr.com/ru/post/1612607/


All Articles