Skip to content

Commit

Permalink
Merge pull request #6 from aschiavon91/feat/improve-requests-infos
Browse files Browse the repository at this point in the history
feat: improve requests infos
  • Loading branch information
aschiavon91 committed Nov 6, 2023
2 parents 4c2eba3 + 1025428 commit 6be6db2
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 80 deletions.
1 change: 1 addition & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
start_dev:
docker compose -f docker-compose-dev.yaml up -d
mix setup
iex --dbg pry -S mix phx.server

start_prod:
docker compose -f docker-compose.yaml up -d --build
Expand Down
34 changes: 33 additions & 1 deletion lib/harpoon/models/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@ defmodule Harpoon.Models.Request do

import Ecto.Changeset

@fields ~w(
sid
path
method
headers
body
query_params
cookies
host
remote_ip
remote_port
http_version
body_length
)a

@required_fields ~w(
sid
path
method
headers
host
)a

@timestamps_opts [type: :utc_datetime_usec]

@derive Jason.Encoder
Expand All @@ -14,11 +37,20 @@ defmodule Harpoon.Models.Request do
field :method, :string
field :headers, :map
field :body, :string
field :query_params, :map
field :cookies, :map
field :host, :string
field :remote_ip, :string
field :remote_port, :string
field :http_version, :string
field :body_length, :integer

timestamps()
end

def changeset(struct \\ %__MODULE__{}, params) do
cast(struct, params, ~w(sid path method headers body)a)
struct
|> cast(params, @fields)
|> validate_required(@required_fields)
end
end
2 changes: 1 addition & 1 deletion lib/harpoon_web/live/home/home_live.html.heex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="grid grid-cols-1 px-4 pt-6 mt-12 xl:grid-cols-2 xl:gap-4 dark:bg-gray-900">
<div class="grid grid-cols-1 px-4 pt-10 mt-12 xl:grid-cols-2 xl:gap-4 dark:bg-gray-900">
<.without_current :if={!@current} url={url(~p"/#{@sid}")} />
<.with_current :if={@current} current={@current} />
</div>
35 changes: 35 additions & 0 deletions lib/harpoon_web/live/home/partials/data_table.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<div class="col-span-auto">
<div class="relative overflow-x-auto shadow-md sm:rounded-lg">
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<caption class="p-5 text-lg font-semibold text-left text-gray-900 bg-white dark:text-white dark:bg-gray-800">
<%= @collection_name %>
</caption>
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr>
<th scope="col" class="px-6 py-3">
Name
</th>
<th scope="col" class="px-6 py-3">
Value
</th>
</tr>
</thead>
<tbody>
<tr
:for={{data_field_name, data_value} <- @data}
class="bg-white border-b dark:bg-gray-800 dark:border-gray-700"
>
<th
scope="row"
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white"
>
<code><%= data_field_name %></code>
</th>
<td class="px-6 py-4">
<code><%= data_value %></code>
</td>
</tr>
</tbody>
</table>
</div>
</div>
84 changes: 13 additions & 71 deletions lib/harpoon_web/live/home/partials/with_current.html.heex
Original file line number Diff line number Diff line change
@@ -1,73 +1,13 @@
<div class="col-span-1">
<div class="relative overflow-x-auto shadow-md sm:rounded-lg">
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<caption class="p-5 text-lg font-semibold text-left text-gray-900 bg-white dark:text-white dark:bg-gray-800">
Request Details
</caption>
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr>
<th scope="col" class="px-6 py-3">
Name
</th>
<th scope="col" class="px-6 py-3">
Value
</th>
</tr>
</thead>
<tbody>
<tr
:for={{info_name, info_value} <- Map.take(@current, [:id, :method, :path])}
class="bg-white border-b dark:bg-gray-800 dark:border-gray-700"
>
<th
scope="row"
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white"
>
<code><%= info_name %></code>
</th>
<td class="px-6 py-4">
<code><%= info_value %></code>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col-span-1">
<div class="relative overflow-x-auto shadow-md sm:rounded-lg">
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<caption class="p-5 text-lg font-semibold text-left text-gray-900 bg-white dark:text-white dark:bg-gray-800">
Headers
</caption>
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr>
<th scope="col" class="px-6 py-3">
Name
</th>
<th scope="col" class="px-6 py-3">
Value
</th>
</tr>
</thead>
<tbody>
<tr
:for={{header_name, header_value} <- @current.headers}
class="bg-white border-b dark:bg-gray-800 dark:border-gray-700"
>
<th
scope="row"
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white"
>
<code><%= header_name %></code>
</th>
<td class="px-6 py-4">
<code><%= header_value %></code>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<% request_fields =
~w(id method path host remote_ip remote_port http_version body_length)a %>
<.data_table collection_name="Request Details" data={Map.take(@current, request_fields)} />
<.data_table
:if={@current.query_params != %{}}
collection_name="Query Parameters"
data={@current.query_params}
/>
<.data_table :if={@current.cookies != %{}} collection_name="Cookies" data={@current.cookies} />
<.data_table :if={@current.headers != %{}} collection_name="Headers" data={@current.headers} />
<div :if={@current.body} class="col-span-1">
<div class="relative overflow-x-auto shadow-md sm:rounded-lg">
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
Expand All @@ -85,7 +25,9 @@
class="block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
readonly
disabled
><%= @current.body %></textarea>
>
<%= @current.body %>
</textarea>
</th>
</tr>
</tbody>
Expand Down
32 changes: 25 additions & 7 deletions lib/harpoon_web/plugs/capture_request_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ defmodule HarpoonWeb.Plugs.CaptureRequestPlug do
defp conn_to_request(conn, sid) do
case Plug.Conn.read_body(conn) do
{:ok, body, conn} ->
req = %{
sid: sid,
path: parse_path(conn.request_path, sid),
method: conn.method,
headers: Map.new(conn.req_headers),
body: body
}
req =
Map.merge(
%{
sid: sid,
path: parse_path(conn.request_path, sid),
headers: Map.new(conn.req_headers),
body: body,
method: conn.method,
query_params: conn.query_params,
host: conn.host,
cookies: conn.req_cookies
},
parse_from_adapter_data(conn.adapter)
)

{:ok, req, conn}

Expand All @@ -44,6 +51,17 @@ defmodule HarpoonWeb.Plugs.CaptureRequestPlug do
end
end

defp parse_from_adapter_data(
{Plug.Cowboy.Conn, %{version: version, peer: {peer_ip, peer_port}, body_length: body_length}}
) do
%{
remote_ip: to_string(:inet_parse.ntoa(peer_ip)),
remote_port: to_string(peer_port),
body_length: body_length,
http_version: to_string(version)
}
end

defp parse_path(path, sid) do
path
|> String.replace("/#{sid}", "")
Expand Down
7 changes: 7 additions & 0 deletions priv/repo/migrations/20231103025616_add_requests_table.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ defmodule Harpoon.Repo.Migrations.AddRequestsTable do
add :sid, :uuid, null: false
add :path, :string, null: false
add :method, :string, null: false
add :host, :string, null: false
add :headers, :jsonb, null: false
add :body, :text, null: true
add :remote_ip, :string
add :remote_port, :string
add :http_version, :string
add :query_params, :jsonb
add :cookies, :jsonb
add :body_length, :integer

timestamps(type: :utc_datetime_usec)
end
Expand Down

0 comments on commit 6be6db2

Please sign in to comment.