diff --git a/justfile b/justfile index 1ac4723..ddd4fcc 100644 --- a/justfile +++ b/justfile @@ -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 diff --git a/lib/harpoon/models/request.ex b/lib/harpoon/models/request.ex index f3704b3..84ea77e 100644 --- a/lib/harpoon/models/request.ex +++ b/lib/harpoon/models/request.ex @@ -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 @@ -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 diff --git a/lib/harpoon_web/live/home/home_live.html.heex b/lib/harpoon_web/live/home/home_live.html.heex index 988face..97bd976 100644 --- a/lib/harpoon_web/live/home/home_live.html.heex +++ b/lib/harpoon_web/live/home/home_live.html.heex @@ -1,4 +1,4 @@ -
+
<.without_current :if={!@current} url={url(~p"/#{@sid}")} /> <.with_current :if={@current} current={@current} />
diff --git a/lib/harpoon_web/live/home/partials/data_table.html.heex b/lib/harpoon_web/live/home/partials/data_table.html.heex new file mode 100644 index 0000000..0981c6d --- /dev/null +++ b/lib/harpoon_web/live/home/partials/data_table.html.heex @@ -0,0 +1,35 @@ +
+
+ + + + + + + + + + + + + + +
+ <%= @collection_name %> +
+ Name + + Value +
+ <%= data_field_name %> + + <%= data_value %> +
+
+
diff --git a/lib/harpoon_web/live/home/partials/with_current.html.heex b/lib/harpoon_web/live/home/partials/with_current.html.heex index dac5cbb..29b42ae 100644 --- a/lib/harpoon_web/live/home/partials/with_current.html.heex +++ b/lib/harpoon_web/live/home/partials/with_current.html.heex @@ -1,73 +1,13 @@ -
-
- - - - - - - - - - - - - - -
- Request Details -
- Name - - Value -
- <%= info_name %> - - <%= info_value %> -
-
-
-
-
- - - - - - - - - - - - - - -
- Headers -
- Name - - Value -
- <%= header_name %> - - <%= header_value %> -
-
-
+<% 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} />
@@ -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 %> + > + <%= @current.body %> + diff --git a/lib/harpoon_web/plugs/capture_request_plug.ex b/lib/harpoon_web/plugs/capture_request_plug.ex index eb3c679..a973e98 100644 --- a/lib/harpoon_web/plugs/capture_request_plug.ex +++ b/lib/harpoon_web/plugs/capture_request_plug.ex @@ -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} @@ -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}", "") diff --git a/priv/repo/migrations/20231103025616_add_requests_table.exs b/priv/repo/migrations/20231103025616_add_requests_table.exs index 1248a49..5debcad 100644 --- a/priv/repo/migrations/20231103025616_add_requests_table.exs +++ b/priv/repo/migrations/20231103025616_add_requests_table.exs @@ -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