Quick actions

cmd+k|ctrl+k

Navigation

Languages

Servy iex

Snippet info

Language

Elixir

Visibility

public

Author

jsonkody

Created

2024-07-23T10:25:58.880256Z

Updated

2024-07-23T10:25:58.880256Z

defmodule Servy do


    def handle(request) do
        request
        |> parse
        |> rewrite_path
        |> log
    end
    
    def rewrite_path(%{ path: "/wildlife" } = conv) do
        %{ conv | path: "wildthings" }
    end
    
    def rewrite_path(conv), do: conv

    def parse(request) do
    [method, path, _] = request
        |> String.split("\n")
        |> List.first
        |> String.split(" ")
        
        %{ method: method,
           path: path,
           resp_body: "",
           status: nil
         }
    end
    
    def log(conv), do: IO.inspect conv
end

request = """
GET /wildlife HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept: */*

"""

request |> Servy.handle
INFO